import GameConst, { wait } from './../GameConst';
import { playBg } from './../ctrls/soundCtrl';
import MovieClipCtrl from "../ctrls/MovieClipCtrl";

export default class Ani {

    static get instance() {
        return Ani._instance || (Ani._instance = new Ani())
    }

    private static _instance: Ani;

    constructor() {

    }

    /**
     * 设备生成动效
     * @param target 目标物体
     */
    createAni(target) {
        return new Promise((r) => {
            egret.Tween.removeTweens(target);
            let targetY = target.y
            egret.Tween.get(target)
                .set({ scaleX: 0, scaleY: 0 })
                .to({ scaleX: 0.9, scaleY: 1.1, y: targetY - 30 }, 330)
                .to({ scaleX: 1, scaleY: 1, y: targetY }, 170)
                .call(() => {
                    r();
                })
        })
    }

    /**
     * 产生金币物体的动效
     * @param target 目标物体
     */
    updateCoinBase(target) {
        return new Promise(r => {
            egret.Tween.removeTweens(target)
            egret.Tween.get(target)
                .set({ scaleX: 0, scaleY: 0 })
                .to({ scaleX: 0.9, scaleY: 1.1 }, 100)
                .to({ scaleX: 1, scaleY: 1 }, 100)
                .call(() => {
                    r()
                })
        })
    }

    /**
     * 产生金币提示的动效
     * @param target 目标物体
     */
    createCoinTips(target) {
        return new Promise(r => {
            egret.Tween.removeTweens(target)
            egret.Tween.get(target)
                .set({ scaleX: 0, scaleY: 0 })
                .wait(100)
                .to({ scaleX: 1.1, scaleY: 1.1 }, 300)
                .to({ scaleX: 1, scaleY: 1 }, 30)
                .call(() => {
                    r();
                })
        })

    }

    /**
     * 更新金币动效
     * @param target 目标提示物体
     * @param number 更改的金币数
     */
    updateCoinTips(target: eui.Label, number: number | string) {
        return new Promise(r => {
            egret.Tween.removeTweens(target)
            egret.Tween.get(target)
                .set({ scaleX: 1, scaleY: 1 })
                .wait(100)
                .to({ scaleX: 1.3, scaleY: 1.3 }, 170)
                .call(() => { target.text = number + "" })
                .to({ scaleX: 1, scaleY: 1 }, 160)
                .call(() => {
                    r();
                })
        })
    }

    /**
     * 更新服务员金币动效
     * @param target 服务员头上的金币提示
     */
    updateCoinWaitress(target) {
        egret.Tween.removeTweens(target)
        let targetY = target.y;
        egret.Tween.get(target, { loop: true })
            .set({ scaleX: 0, scaleY: 0, y: targetY, alpha: 1 })
            .wait(100)
            .to({ scaleX: 1.1, scaleY: 1.1 }, 300)
            .to({ scaleX: 1, scaleY: 1 }, 30)
            .to({ y: targetY - 39 }, 500)
            .to({ y: targetY - 40, alpha: 0 }, 30)
    }

    /**
     * 收集金币动效
     * @param x 起始位置x
     * @param y 起始位置y
     * @param scene 当前场景
     */
    collectCoinAni(x, y, scene) {
        return new Promise(r => {
            let i = 0;
            for (let i = 0; i < 10; i++) {
                let coin = MovieClipCtrl.instance.getMovieClipByName("coinAni_" + i)
                coin.scaleX = 0.5;
                coin.scaleY = 0.5;
                coin.x = x;
                coin.y = y;
                scene.addChild(coin)
                coin.play(-1);
                egret.Tween.removeTweens(coin)
                wait(60 * i).then(() => {
                    let data = { a: 0 }
                    egret.Tween.get(data, {
                        onChange: () => {
                            coin.x = GameConst.bezier(data.a, x + Math.pow(-1, i) * 20, 120 + Math.pow(-1, i) * 20, 60)
                            coin.y = GameConst.bezier(data.a, y, 150, 100)
                        }
                    })
                        .to({ a: 1 }, 500)
                        .call(() => {
                            i++
                            scene.removeChild(coin)
                            if (i == 9) {
                                r()
                            }
                        })

                })
            }
        })
    }


    /**
     * 奖励金币动效
     * @param scene 当前场景
     */
    awardCoinAni(scene) {
        return new Promise(r => {
            let coinLight = MovieClipCtrl.instance.getMovieClipByName("coinLightAni_0");
            coinLight.x = 375;
            coinLight.y = 600;
            coinLight.anchorOffsetX = 200;
            coinLight.anchorOffsetY = 200;
            scene.addChild(coinLight);
            coinLight.gotoAndPlay(0)
            for (let i = 0; i < 10; i++) {
                let coin = MovieClipCtrl.instance.getMovieClipByName("coinAni_" + i)
                coin.x = 375 - 83;
                coin.y = 610 - 100;
                coin.scaleX = 1;
                coin.scaleY = 1;
                coin.anchorOffsetX = 83 / 2;
                coin.anchorOffsetY = 86 / 2;
                console.log(coin);

                scene.addChild(coin)
                coin.play(-1);
                egret.Tween.removeTweens(coin)

                wait(60 * i).then(() => {
                    let data = { a: 0 }
                    egret.Tween.get(data, {
                        onChange: () => {
                            coin.x = GameConst.bezier(data.a, 300 + Math.pow(-1, i) * 30, 120 + Math.pow(-1, i) * 30, 120)
                            coin.y = GameConst.bezier(data.a, 500, 150, 150)
                            coin.scaleX = 1 - data.a;
                            coin.scaleY = 1 - data.a;
                        }
                    })
                        .to({ a: 1 }, 500)
                        .call(() => {
                            i++
                            scene.removeChild(coin)
                            if (i == 9) {
                                scene.removeChild(coinLight)
                                r()
                            }
                        })

                })
            }
        })
    }
}