import AntPart from "./AntPart";
import Ant from "./Ant";
import { MTweenObject } from "../MTween";

export default class AntHead extends AntPart {
    private leftEye: Eye = null;
    private rightEye: Eye = null;
    private mouthSmile: egret.Bitmap = null;
    private mouthFear: egret.Bitmap = null;


    constructor(context: Ant, x: number, y: number, width: number, height: number) {
        super(context, x, y, width, height);
        this.onDraw();
        this.width = width;
        this.height = height;
        this.localRect = new egret.Rectangle(0, 15, width, height - 15);
        context.doLookAt = (target: egret.Point) => {
            if (target) {
                this.leftEye.lookAt(this.globalToLocal(target.x, target.y));
                this.rightEye.lookAt(this.globalToLocal(target.x, target.y));
                this.isSmile = false;
            } else {
                this.leftEye.lookAt();
                this.rightEye.lookAt();
                this.isSmile = true;
            }
        }
    }

    private _isSmile: boolean = true;
    private set isSmile(v: boolean) {
        if (v == this._isSmile) return;
        this.mouthFear.visible = !v;
        this.mouthSmile.visible = v;
        this._isSmile = v;
    }
    private get isSmile() { return this._isSmile }

    onDraw() {
        this.width = 34;
        this.height = 46;

        let leftTentacle = new egret.Bitmap(RES.getRes("ant_tentacle_png"));
        leftTentacle.x = 7.15;
        leftTentacle.y = 0.47;
        this.addChild(leftTentacle);

        let rightTentacle = new egret.Bitmap(RES.getRes("ant_tentacle_png"));
        rightTentacle.x = 18.65;
        rightTentacle.y = -0.23;
        this.addChild(rightTentacle);

        let face = new egret.Bitmap(RES.getRes("ant_face_png"));
        face.x = -0.02;
        face.y = 15.64;
        this.addChild(face);

        this.leftEye = new Eye(
            new EyeWhite(this, RES.getRes("ant_eyewhite_png"), 3.78, 22.64),
            new EyeBall(this, RES.getRes("ant_eyeball_png"), 5.95, 24.32)
        );

        this.rightEye = new Eye(
            new EyeWhite(this, RES.getRes("ant_eyewhite_png"), 18.62, 22.28),
            new EyeBall(this, RES.getRes("ant_eyeball_png"), 21.14, 23.96)
        );

        this.mouthSmile = new egret.Bitmap(RES.getRes("ant_mouth_smile_png"));
        this.mouthSmile.x = 9.65;
        this.mouthSmile.y = 37.49;
        this.addChild(this.mouthSmile);

        this.mouthFear = new egret.Bitmap(RES.getRes("ant_mouth_fear_png"));
        this.mouthFear.x = 9.13;
        this.mouthFear.y = 37;
        this.mouthFear.visible = false;
        this.addChild(this.mouthFear);
    }
}

class Eye {
    private white: EyeWhite = null;
    private ball: EyeBall = null;
    constructor(white: EyeWhite, ball: EyeBall) {
        this.white = white;
        this.ball = ball;
    }

    private _eyeState: EyeState = EyeState.Center;
    private get eyeState() { return this._eyeState; }
    private set eyeState(v: EyeState) {
        if (v == EyeState.Center && this._eyeState == EyeState.Looking) {
            this._eyeState = EyeState.Transition;
            this.doTween(this.white.center, () => this._eyeState = EyeState.Center);
        }
        this._eyeState = v;
    }

    public lookAt(target?: egret.Point) {
        if (target) {
            let targetVec = target.subtract(this.white.center);
            let limitRadius = this.white.radius - this.ball.radius;
            targetVec.normalize(limitRadius);
            let limitedTarget = targetVec.add(this.white.center);
            if (this.eyeState == EyeState.Center) {
                this.eyeState = EyeState.Transition;
                this.doTween(limitedTarget, () => this.eyeState = EyeState.Looking);
            } else if (this.eyeState == EyeState.Looking) {
                this.ball.center = limitedTarget;
            }
        } else {
            this.eyeState = EyeState.Center;
        }
    }

    private doTween(target: egret.Point, callback?: () => void) {
        egret.Tween.get(this.ball).to({ x: target.x - this.ball.width / 2, y: target.y - this.ball.height / 2 }, 100).call(callback);
    }
}

class EyePart extends egret.Bitmap {
    public radius: number = 0;

    private _center: egret.Point = new egret.Point();
    public get center() {
        this._center.x = this.x + this.width / 2;
        this._center.y = this.y + this.height / 2;
        return this._center;
    }
    public set center(v: egret.Point) {
        this.x = v.x - this.width / 2;
        this.y = v.y - this.height / 2;
    }

    constructor(context: AntHead, texture: egret.Texture, x: number, y: number) {
        super(texture);
        this.x = x;
        this.y = y;
        context.addChild(this);
        this.radius = texture.textureWidth / 2;
    }
}

class EyeWhite extends EyePart {
    public ball: EyeBall = null;


}

enum EyeState {
    Center,
    Looking,
    Transition
}

class EyeBall extends EyePart {

}
