const { ccclass, property } = cc._decorator;

/**
 * 道具类型
 */
export enum PROP_TYPE {
    SHOOT3,
    TIMES3,
    WALL
}

@ccclass
export default class Global extends cc.Component {
    //physics tags
    public static TAG_Obstacle = 998;
    public static TAG_WALL = 999;
    public static TAG_WALL_BOTTOM = 1000;

    public static BALL_SPEED = 400;
    public static MAX_BALLS = 800;

    public static WALL_TIME = 5;
    public static PROP_WALL_RATE = 0.025;

    //获得奖励总需积分
    public static REWARD_NEED_SCORE = 4057;//总分数
    public static THUNDER_COST = 200;//闪电花费钻石

    //积分奖励 钻石
    public static SCORE_REWARD_GEM = 30;
    public static WATCH_VIDEO_GEM = 30;//看视频获得钻石

    //复活消耗
    public static REVIVE_COST_GEM = 200;

    public static PROP_SHOOT_COST = 50;
    public static PROP_TIMES_COST = 100;
    public static PROP_WALL_COST = 100;

    public static SKIN_COST = 500;

    public static THREE_STARS_TOTAL_TIME = 135;
    public static SHOW_PROP_TIP_TIME = 30;

    public static DEBUG = false;
    public static OPENALL_LEVEL = false;

    public static firstOpenMenu = true;
    public static firstOpenGame = true;

    public static MAX_MORE_LEVELS = 50;
    public static MAX_STAGE = 100;//100一个小章节

    private static lastPropType = 0;

    //获取今日日期字符串,代表日期的当地写法（不含小时、分和秒）。
    static getTodayString(): string {
        let now = new Date();

        let year = now.getFullYear();
        let month = now.getMonth() + 1;
        let day = now.getDate();
        return ''.concat(year.toString(), month.toString(), day.toString());
    }

    static getNextPropType(selectLevel: number): PROP_TYPE {
        let rand = Math.random();
        let propType = PROP_TYPE.SHOOT3;
        //let timesRand = Global.PROP_WALL_RATE + (0.55 - Math.min(0.25, selectLevel * 0.00125));
        if (rand < Global.PROP_WALL_RATE) {
            propType = PROP_TYPE.WALL;
        } else {
            if (this.lastPropType != PROP_TYPE.TIMES3) {
                propType = PROP_TYPE.TIMES3;
            } else {
                propType = PROP_TYPE.SHOOT3;
            }
        }

        this.lastPropType = propType;
        return propType;
    }

    static getPropDropCount(createTimes: number, selectLevel: number): number {
        if (selectLevel < 10) {
            return Math.min(30, createTimes * 2 + 2);
        } else if (selectLevel < 20) {
            return Math.min(30, Math.floor(createTimes * 2.5) + 2);
        } else if (selectLevel < 30) {
            return Math.min(40, Math.floor(createTimes * 3) + 2);
        } else if (selectLevel < 45) {
            return Math.min(40, Math.floor(createTimes * 3.5) + 3);
        } else if (selectLevel < 60) {
            return Math.min(40, Math.floor(createTimes * 4) + 3);
        } else if (selectLevel < 80) {
            return Math.min(50, Math.floor(createTimes * 4.5) + 3);
        } else if (selectLevel < 100) {
            return Math.min(50, Math.floor(createTimes * 5) + 4);
        } else if (selectLevel < 125) {
            return Math.min(50, Math.floor(createTimes * 5.5) + 4);
        } else {
            return Math.min(60, Math.floor(createTimes * 6) + 4);
        }
    }

    //index 更多关卡每１００关索引加１
    public static moreLevelNeedStars(index: number): number {
        if (index > 49) {
            index = 49;
        }
        return Math.floor((100 * index) * (index * 0.0165 + 2));
    }

    //加载界面
    public static loadingScene(sceneName: string) {
        cc.loader.loadRes("prefabs/ui/Loading", (err, prefab) => {
            let loading = cc.find('Canvas/Loading');
            if (loading) {
                loading.active = false;
            }

            let _node = cc.instantiate(prefab);
            _node.name = 'Loading';
            cc.game.addPersistRootNode(_node);

            _node.runAction(
                cc.callFunc(() => {
                    cc.director.preloadScene(sceneName, () => {
                        cc.director.loadScene(sceneName);
                    });
                })
            );

        });
    }

    //显示积分奖励界面
    static showScoreRewardUI(parentNode: cc.Node, node: cc.Node) {
        let loading = cc.find('Canvas/Loading');
        if (loading) {
            loading.zIndex = 999;
            loading.active = true;
        }
        cc.loader.loadRes("prefabs/ui/RewardLayer", function (err, prefab) {
            let _node = cc.instantiate(prefab);
            _node.parent = parentNode;

            let rewardLayer = _node.getComponent("RewardLayer");
            rewardLayer.setEventNode(node);

            loading.active = false;
        });
    }

    //显示提示
    static showTip(str: string, delayTime = 0.6) {
        cc.loader.loadRes("prefabs/ui/TipPanel", function (err, prefab) {
            let tipNode = cc.instantiate(prefab);
            tipNode.parent = cc.find('Canvas');

            let tipPanel = tipNode.getComponent("TipPanel");
            tipPanel.showLabel(str, delayTime);
        });
    }

    private static debounceStates: { [key: string]: boolean } = {};
    private static debounceTimer: { [key: string]: number } = {};
    //防快速点击函数
    static debounceClicks = (callback: () => void, identifier = 'default', delay = 1000) => {
        return () => {
            if (!Global.debounceStates[identifier]) {
                Global.debounceStates[identifier] = true;
                Global.debounceTimer[identifier] = setTimeout(() => {
                    Global.debounceStates[identifier] = false;
                    clearTimeout(Global.debounceTimer[identifier]);
                }, delay);
                callback();
            }
        };
    };

}
