import Ant from "./Ant/Ant";

export default class PlayerController extends eui.UILayer {
    private touchPointIDList = [];
    // private touchPointIDMap: { [touchPointID: number]: ControllerDirection } = {}

    constructor() {
        super();
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
        this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
    }
    public direction: ControllerDirection = ControllerDirection.None;

    public onLeft: () => void = () => { };
    public onRight: () => void = () => { };

    private onTouchBegin(event: egret.TouchEvent) {
        if (event.stageX >= this.stage.stageWidth / 2) {
            this.touchPointIDList.push({
                touchPointID: event.touchPointID,
                direction: ControllerDirection.Right
            });
            this.direction = ControllerDirection.Right;
        } else {
            this.touchPointIDList.push({
                touchPointID: event.touchPointID,
                direction: ControllerDirection.Left
            });
            this.direction = ControllerDirection.Left;
        }
    }

    private onTouchEnd(event: egret.TouchEvent) {
        this.touchPointIDList = this.touchPointIDList.filter(e => e.touchPointID != event.touchPointID);
        if (this.touchPointIDList.length <= 0)
            this.direction = ControllerDirection.None;
        else {
            this.direction = this.touchPointIDList.pop().direction;
        }
    }
}

export enum ControllerDirection {
    None,
    Left,
    Right
}