import GameObject, { Shadow } from "./GameObject";
import { MConst } from "../MConst";
// import { MConfigs } from "../MConfigs";
import DisplayCollider, { Tag } from "./DisplayCollider";
import DebugMgr from "../Mgr/DebugMgr";
import GameMgr from "../Mgr/GameMgr";
import { Decorator } from "../Decorator/Decorator";
import GameStage from "./GameStage";
import Ant from "./Ant/Ant";
import SoundMgr from "../Mgr/SoundMgr";
import AntPart from "./Ant/AntPart";
import { MTweenObject } from "./MTween";
const MConfigs: any = window;

export default class Leg extends GameObject {
    public isFalling = false;
    public toward: LegToward;
    public get towardCoefficient() { return this.toward == LegToward.Right ? 1 : -1; }

    public colliderOffset: number = 0;
    /**@override */
    public get center(): egret.Point {
        this._center.x = this.x + this.towardCoefficient * this.width / 2;
        this._center.y = this.y + this.towardCoefficient * this.height / 2;
        return this._center;
    }

    constructor(gameStage: GameStage, rect: egret.Rectangle, texture: egret.Texture, toward: LegToward, colliderOffset: number) {
        super(Tag.Leg, rect, gameStage);
        this.gameStage = gameStage;
        let bitmap = new egret.Bitmap(texture);
        this.addChild(bitmap);
        this.toward = toward;

        if (toward == LegToward.Left) {
            bitmap.scaleX = -1;
            this.localRect = new egret.Rectangle(
                -(this.localRect.x + this.localRect.width),
                this.localRect.y,
                this.localRect.width,
                this.localRect.height
            );
        }

        gameStage.addLeg(this);

        this.colliderOffset = colliderOffset;
    }

    protected onCreateShadow() {
        let leg = this as Leg;
        this.shadow = new class extends Shadow {
            constructor(texture: egret.Texture, target?: egret.DisplayObject) {
                super(texture, target);
                this.width = leg.localRect.width;
            }
            public update() {
                super.update();
                if (leg.toward == LegToward.Right) {
                    this.x = leg.x + leg.colliderOffset;
                } else {
                    this.x = leg.x - leg.colliderOffset - leg.localRect.width;
                }
            }
        }(RES.getRes("leg_shadow_png"), this);
    }

    public updateCallback: () => void;
    
    onUpdate() {
        super.onUpdate();
        this.updateCallback && this.updateCallback();
    }

    /**一次行动完毕的回调 */
    public onActed: () => void = () => { }

    /**行动 */
    public act() {
        this.doTween();
    }
    private touchGround() {
        SoundMgr.instance.playEffect("leg_trample_mp3");
        this.playAnim();

        this.isFalling = false;
        MTweenObject.removeTweens(this);
        this.velocity = new egret.Point();

        //震动地面
        this.gameStage.shake();

        this.onActed();
    }

    private playAnim() {
        let clip = GameMgr.instance.particleMgr.get("leg_trample");
        if (this.toward == LegToward.Left) {
            clip.x = this.x - 230;
        } else {
            clip.x = this.x + 30;
        }
        clip.y = this.y + this.height - 60;
        clip.gotoAndPlay(0);
    }

    private doTween() {
        let param = MConfigs.legTweenParam;
        MTweenObject.get(this.velocity)
            .to({ y: -param.rise.maxSpeed }, param.rise.duration.speedUp)
            .wait(param.rise.duration.keep)
            .to({ y: 0 }, param.rise.duration.slowDown)
            .to({ y: param.fall.speedUp1.value }, param.fall.speedUp1.duration)
            .to({ y: param.fall.speedUp2.value }, param.fall.speedUp2.duration);

        let p1Time = param.rise.duration.speedUp
            + param.rise.duration.keep
            + param.rise.duration.slowDown
        let p2Time = param.fall.speedUp1.duration
            + param.fall.speedUp2.duration;
        MTweenObject.get(this.velocity)
            .to({ x: param.horizontal.phase1Speed * this.towardCoefficient }, p1Time)
            .call(() => this.isFalling = true)
            .to({ x: param.horizontal.phase2Speed * this.towardCoefficient }, p2Time);
    }

    /**@override */
    onCollisionEnter(other: DisplayCollider) {
        if (other.tag == Tag.Ground && this.isFalling) {
            this.touchGround();
        }
    }

    /**@override */
    onCollisionStay(other: DisplayCollider) {
    }

    /**@override */
    onCollisionExit(other: DisplayCollider) {

    }

    onDestroy() {
        super.onDestroy();
        this.gameStage.removeLeg(this);
    }
}

export enum LegToward {
    Left,
    Right
}