import GameObject, { Shadow } from "../GameObject";
import { ControllerDirection } from "../PlayerController";
import { Tag } from "../DisplayCollider";
import DebugMgr from "../../Mgr/DebugMgr";
import GameMgr from "../../Mgr/GameMgr";
// import { MConfigs } from "../../MConfigs";
import SoundMgr from "../../Mgr/SoundMgr";
import GameStage from "../GameStage";
import { AntLeg, AntLegGroup, LegTurnGroup } from "./AntLeg";
import AntPart from "./AntPart";
import AntHead from "./AntHead";

const MConfigs: any = window;
export default class Ant extends GameObject {
    public onDie = () => { };
    public isPlayingTouchEffect = false;
    private speedValue: number = MConfigs.ant.speedValue;
    public contactToLeg: number = null;
    public contactWall: number = null;
    protected legGroup0: AntLegGroup;
    protected legGroup1: AntLegGroup;
    public doLookAt: (target?: egret.Point) => void;

    private legTurnGroups: LegTurnGroup[];
    private body: AntPart;

    onCreateShadow() {
        this.shadow = new class extends Shadow {
            update() {
                let world = this.target.parent.localToGlobal(this.target.x, this.target.y);
                this.x = this.parent.globalToLocal(world.x, world.y).x + this.target.width / 2 - this.width / 2;
            }
        }(RES.getRes("ant_shadow_png"));
    }


    constructor(gameStage: GameStage) {
        super(Tag.Ant, new egret.Rectangle, gameStage);
        this.width = 80;
        this.height = 62;

        DebugMgr.instance.onClick = () => {
            this.legTurnGroups.forEach(e => console.error(e.centerX));
        }

        /* setTimeout(() => {
            this.doLookAt(new egret.Point(1624, 0));
            setTimeout(() => {
                this.doLookAt();
            }, 2000);
        }, 2000); */
    }

    /**@override */
    protected onDraw() {
        super.onDraw();
        // this.addChild(new egret.Bitmap(RES.getRes("ant_png")));
        let leg0 = new AntLeg(RES.getRes("ant_leg_png"), -12);
        let leg1 = new AntLeg(RES.getRes("ant_leg_png"), -4.5);
        let leg2 = new AntLeg(RES.getRes("ant_leg_png"), 2);
        let leg3 = new AntLeg(RES.getRes("ant_leg_png"), 10);
        let leg4 = new AntLeg(RES.getRes("ant_leg_png"), 18);
        let leg5 = new AntLeg(RES.getRes("ant_leg_png"), 28);
        this.addChild(leg0);
        this.addChild(leg1);
        this.addChild(leg2);
        this.addChild(leg3);
        this.addChild(leg4);
        this.addChild(leg5);

        this.legGroup0 = new AntLegGroup(leg0, leg2, leg4);
        this.legGroup1 = new AntLegGroup(leg1, leg3, leg5);

        this.legTurnGroups = [new LegTurnGroup(leg0, leg1), new LegTurnGroup(leg2, leg3), new LegTurnGroup(leg4, leg5)];

        this.addChild(new AntPart(this, -18, 24, RES.getRes("ant_tail_png")));
        this.body = new AntPart(this, 9, 23, RES.getRes("ant_body_png"));
        this.addChild(this.body);
        this.shadow.target = this.body;
        this.addChild(new AntHead(this, 29, 0, 34, 46));
    }

    private playLegAnim() {
        this.legGroup0.startTween(0);
        this.legGroup1.startTween(1);
    }

    private stopLegAnim() {
        this.legGroup0.stopTween();
        this.legGroup1.stopTween();
    }

    public move(direction: ControllerDirection) {
        let dirVec: egret.Point = new egret.Point(0, 0); //默认为0
        switch (direction) {
            case ControllerDirection.Left:
                if (this.contactToLeg != 0 && this.contactWall != 0) {
                    dirVec = new egret.Point(-1, 0);
                }
                break;
            case ControllerDirection.Right:
                if (this.contactToLeg != 1 && this.contactWall != 1) {
                    dirVec = new egret.Point(1, 0);
                }
                break;

            case ControllerDirection.None:
                // dirVec = new egret.Point(0, 0);
                break;
            default:
                break;
        }
        dirVec.normalize(this.speedValue);

        if (dirVec.length > 0 && this.velocity.length <= 0) {
            SoundMgr.instance.playEffect("ant_move_mp3", true);
            this.playLegAnim();
        } else if (dirVec.length <= 0 && this.velocity.length > 0) {
            SoundMgr.instance.stopLoopEffect("ant_move_mp3");
            this.stopLegAnim();
        }

        this.velocity = dirVec;
    }

    public die() {
        SoundMgr.instance.stopLoopEffect("ant_move_mp3");
        SoundMgr.instance.playEffect("ant_die_mp3");
        this.destroy();
        this.playDieAnim();
        this.onDie();
    }

    public playDieAnim() {
        let clip = GameMgr.instance.particleMgr.get("ant_die");
        let bodyPos = this.body.parent.localToGlobal(this.body.x, this.body.y);
        let local = this.gameStage.shadowLayer.globalToLocal(bodyPos.x, bodyPos.y);
        clip.x = local.x + this.body.width / 2 - 95;
        clip.y = local.y + this.body.height / 2 - 25;
        clip.gotoAndPlay(0);
    }

    onUpdate() {
        super.onUpdate();
        this.legTurnGroups.forEach(e => e.update(this));
        this.updateLookAt();

        DebugMgr.instance.setLog(this.gameStage.getLegList().length.toString());
    }

    /**@virtual */
    protected updateLookAt() { }
}