import DisplayCollider, { Tag, Contact } from "../DisplayCollider";
import Ant from "./Ant";
// import { MConfigs } from "../../MConfigs";
import Leg from "../Leg";
import SoundMgr from "../../Mgr/SoundMgr";
import { ThroughSubmitData } from "../../../libs/tw/data/custom/throughSubmit/ThroughSubmitData";
const MConfigs: any = window;

export default class AntPart extends DisplayCollider {
    private context: Ant = null;
    private turnMove: TurnMove = null;
    constructor(context: Ant, x: number, y: number, texture: egret.Texture | number, height?: number) {
        super(Tag.Ant, new egret.Rectangle(0, 0,
            typeof texture != "number" ? texture.textureWidth : texture,
            typeof texture != "number" ? texture.textureHeight : height
        ));
        this.context = context;
        this.x = x;
        this.y = y;

        if (typeof texture != "number") {
            this.turnMove = new TurnMove(this, this.x + texture.textureWidth / 2);
            this.addChild(new egret.Bitmap(texture))
        } else {
            this.turnMove = new TurnMove(this, this.x + texture / 2);
        }
    }

    /**@override */
    onUpdate() {
        super.onUpdate();
        this.turnMove.update(this.context);
    }

    onCollisionEnter(other: DisplayCollider, contact: Contact) {
        if (other.tag == Tag.Leg) {
            this.context.contactToLeg = contact.self;
            if ((other as Leg).isFalling) {
                this.context.die();
            } else {

                if (!this.context.isPlayingTouchEffect) {
                    this.context.isPlayingTouchEffect = true;
                    SoundMgr.instance.playEffect("ant_touch_leg_mp3").then(() => {
                        this.context.isPlayingTouchEffect = false;
                    });
                }
            }
        }

        if (other.tag == Tag.Wall) {
            this.context.contactWall = contact.self;
        }
    }

    onCollisionStay(other: DisplayCollider, contact: Contact) {
        if (other.tag == Tag.Leg) {
            this.context.contactToLeg = contact.self;
        }

        if (other.tag == Tag.Wall) {
            this.context.contactWall = contact.self;
        }
    }

    onCollisionExit(other: DisplayCollider) {
        if (other.tag == Tag.Leg) {
            this.context.contactToLeg = null;
        }

        if (other.tag == Tag.Wall) {
            this.context.contactWall = null;
        }
    }
}

class TurnMove {
    protected context: egret.DisplayObject = null;
    private velocityDirection: number = 1;
    private velocityValueCoefficient: number = MConfigs.antTween.turnVelocityCoefficient;
    private iniPosX = 0;

    constructor(context: egret.DisplayObject, limitPosValue: number) {
        this.iniPosX = limitPosValue;
        this.context = context;
    }

    private isOverLimit() {
        let centerX = this.getCenterX();
        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;
            }
        }
    }

    /**@override */
    protected getCenterX() {
        return this.context.x + this.context.width / 2;
    }

    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.context.x += this.velocityDirection * this.velocityValueCoefficient * this.iniPosX;
    }
}

