// import { MConfigs } from "../../MConfigs";
import Ant from "./Ant";
import { MTweenObject } from "../MTween";
const MConfigs: any = window;

export class AntLeg extends egret.Bitmap {
    constructor(textrue: egret.Texture, x: number) {
        super(textrue);
        this.anchorOffsetX = 2.5;
        this.y = 43;
        this.x = x;
    }
}

export class AntLegGroup {
    public data: AntLeg[] = [];
    constructor(...agrs: AntLeg[]) {
        this.data = agrs;
    }

    startTween(id: number) {

        this.data.forEach(e => {
            if (id == 0) {
                this.doTween0(e);
            } else if (id == 1) {
                this.doTween1(e);
            } else {
                console.warn("invalid id");
            }
        });
    }

    public doTween0(leg: AntLeg) {
        let config = MConfigs.antTween.leg;
        egret.Tween.get(leg)
            .to({ rotation: config.maxRotationValue }, config.duration)
            .to({ rotation: -config.maxRotationValue }, 2 * config.duration)
            .to({ rotation: 0 }, config.duration).call(() => this.doTween0(leg));
    }

    public doTween1(leg: AntLeg) {
        let config = MConfigs.antTween.leg;
        egret.Tween.get(leg)
            .to({ rotation: -config.maxRotationValue }, config.duration)
            .to({ rotation: config.maxRotationValue }, 2 * config.duration)
            .to({ rotation: 0 }, config.duration).call(() => this.doTween1(leg));
    }

    public stopTween() {
        this.data.forEach(e => {
            egret.Tween.removeTweens(e);
        });
    }
}

export class LegTurnGroup {
    private iniPosX = 0;
    private left: AntLeg = null;
    private right: AntLeg = null;
    private velocityDirection: number = 1;
    private velocityValueCoefficient: number = MConfigs.antTween.turnVelocityCoefficient;
    private distance = 0;

    private isOverLimit() {
        let centerX = this.centerX;

        if (this.velocityDirection == -1) {
            if (this.iniPosX > 0) {
                return centerX <= -this.iniPosX;
            } else {
                return centerX >= -this.iniPosX;
            }
        } else {
            if (this.iniPosX > 0) {
                return centerX >= this.iniPosX;
            } else {
                return centerX <= this.iniPosX;
            }
        }
    }

    constructor(left: AntLeg, right: AntLeg) {
        this.left = left;
        this.right = right;
        this.distance = Math.abs(this.right.x - this.left.x);
        this.iniPosX = this.left.x + this.distance / 2;
        this.centerX = this.iniPosX;
    }

    private _centerX: number = 0;
    public get centerX() {
        return this._centerX;
    }
    public set centerX(v: number) {
        this.left.x = v - this.distance / 2;
        this.right.x = v + this.distance / 2;
        this._centerX = v;
    }

    public update(ant: Ant) {
        //中心位置为0或蚂蚁速度为0时不移动
        if (this.iniPosX == 0 || ant.velocity.length <= 0) return;

        if (ant.velocity.x > 0) { //蚂蚁速度方向向右
            this.velocityDirection = 1;
        } else {
            this.velocityDirection = -1;
        }

        if (this.isOverLimit()) return; //超过边界，截断
        //移动
        this.centerX += this.velocityDirection * this.velocityValueCoefficient * this.iniPosX;
    }
}
