"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HoverLabelTouch = void 0;
exports.makeHoverLabelTouch = makeHoverLabelTouch;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const TouchController_1 = require("../../TouchController");
const HoverLabelEntity_1 = require("../Entities/HoverLabelEntity");
const TryShowHoverLabel_1 = require("../UseCases/TryShowHoverLabel");
class HoverLabelTouch extends core_1.AppObjectController {
}
exports.HoverLabelTouch = HoverLabelTouch;
HoverLabelTouch.type = "Touch_Hoverlabel_Controller";
function makeHoverLabelTouch(appObject) {
    return new HoverLabelTouchImp(appObject);
}
class HoverLabelTouchImp extends HoverLabelTouch {
    constructor(appObject) {
        super(appObject, HoverLabelTouch.type);
        this.touchLabelDragThreshold = 15;
        this.lastPosition = core_2.Vector2.Zero();
        this.startPosition = core_2.Vector2.Zero();
        this.touchStarted = false;
        this.allow = (e) => {
            return true;
        };
        this.onTouchStart = (e) => {
            if (!this.allow(e))
                return;
            if (!this.hoverLabelEntity)
                return;
            this.hoverLabelEntity.hideHoverLabel();
            this.touchStarted = false;
            if (e.touchCount !== 1)
                return;
            this.startPosition = e.touches[0].position;
            this.lastPosition = e.touches[0].position;
            this.touchStarted = true;
        };
        this.onTouchMove = (e) => {
            if (!this.allow(e))
                return;
            if (!this.touchStarted)
                return;
            this.lastPosition = e.touches[0].position;
        };
        this.onTouchEnd = () => {
            if (!this.touchStarted)
                return;
            if (!this.tryShowHoverLabel)
                return;
            const xDiff = this.lastPosition.x - this.startPosition.x;
            const yDiff = this.lastPosition.y - this.startPosition.y;
            const dist = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
            if (dist < this.touchLabelDragThreshold) {
                this.tryShowHoverLabel.tryShowScreenHoverLabel(this.lastPosition);
            }
        };
        this.hoverLabelEntity = this.appObject.getComponent(HoverLabelEntity_1.HoverLabelEntity.type);
        if (!this.hoverLabelEntity) {
            this.warn("Serializer has been added to an App Object that does not have a HoverLabelEntity. Add the Entity first");
            return;
        }
        this.tryShowHoverLabel = TryShowHoverLabel_1.TryShowHoverLabel.get(this.appObjects);
        if (!this.tryShowHoverLabel) {
            this.warn("Unable to find TryShowHoverLabel UC");
            return;
        }
        const touchInput = TouchController_1.TouchInputEntity.get(this.appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
//# sourceMappingURL=HoverLabelTouch.js.map