// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import JiaZai from "./JiaZai";
import MapConroler from "./Map";
import SceneManager from "./SceneManager";
import { MiniGameSdk } from "./Sdk/MiniGameSdk";


const { ccclass, property } = cc._decorator;


@ccclass
export default class setUi extends cc.Component {
    static _instance: any;
    time: number = 0;

    @property(cc.Node)
    music: cc.Node = null;

    @property(cc.Node)
    effect: cc.Node = null;

    @property(cc.Node)
    vibrate: cc.Node = null;

    @property(cc.Node)
    exit: cc.Node = null;

    @property(cc.Node)
    win: cc.Node = null;

    musicState: boolean = true;
    effectState: boolean = true;
    vibrateState: boolean = true;
    onLoad() {
        this.musicState = cc.fx.GameConfig.GM_INFO.musicOpen;
        this.effectState = cc.fx.GameConfig.GM_INFO.effectOpen;
        this.vibrateState = cc.fx.GameConfig.GM_INFO.vibrateOpen;
        if (this.musicState == true) {
            this.music.children[0].active = true;
        } else {
            this.music.children[1].active = true;
        }
        if (this.effectState == true) {
            this.effect.children[0].active = true;
        } else {
            this.effect.children[1].active = true;
        }
        if (this.vibrateState == true) {
            this.vibrate.children[0].active = true;
        } else {
            this.vibrate.children[1].active = true;
        }
    }

    start() {
    }


    clickMusic() {
        if (this.musicState) {
            this.musicState = false;
            cc.fx.GameConfig.GM_INFO.musicOpen = this.musicState;

            this.setMusicConfig();
            this.music.children[0].active = false;
            this.music.children[1].active = true;
            //如果音乐关闭了，则停止音乐
            cc.fx.AudioManager._instance.stopMusic();
        }
        else {
            this.musicState = true;
            cc.fx.GameConfig.GM_INFO.musicOpen = this.musicState;
            this.setMusicConfig();
            this.music.children[0].active = true;
            this.music.children[1].active = false;
            cc.fx.AudioManager._instance.playMusicGame();
        }
    }

    setMusicConfig() {
        let audioInfo = {
            "musicOpen": cc.fx.GameConfig.GM_INFO.musicOpen,             //音乐
            "effectOpen": cc.fx.GameConfig.GM_INFO.effectOpen,            //音效
            "vibrateOpen": cc.fx.GameConfig.GM_INFO.vibrateOpen,           //震动
        }
        cc.fx.StorageMessage.setStorage("music", audioInfo);
    }

    clickEffect() {
        if (this.effectState) {
            this.effectState = false;
            cc.fx.GameConfig.GM_INFO.effectOpen = this.effectState;
            this.setMusicConfig();
            this.effect.children[0].active = false;
            this.effect.children[1].active = true;
        }
        else {
            this.effectState = true;
            cc.fx.GameConfig.GM_INFO.effectOpen = this.effectState;
            this.setMusicConfig();
            this.effect.children[0].active = true;
            this.effect.children[1].active = false;

        }
    }

    clickVibrate() {
        if (this.vibrateState) {
            this.vibrateState = false;
            cc.fx.GameConfig.GM_INFO.vibrateOpen = this.vibrateState;
            this.setMusicConfig();
            this.vibrate.children[0].active = false;
            this.vibrate.children[1].active = true;

        }
        else {
            this.vibrateState = true;
            cc.fx.GameConfig.GM_INFO.vibrateOpen = this.vibrateState;
            this.setMusicConfig();
            this.vibrate.children[0].active = true;
            this.vibrate.children[1].active = false;


        }
    }
    syncToggleState() {

    }

    //退出游戏
    clickExit() {
        cc.fx.AudioManager._instance.playEffect("anniu_little", null);
        if (MapConroler._instance.gameStart == false) {
            MapConroler._instance.returnHome();
        }
        else {
            MapConroler._instance.trackFinishi("give_up");
            MapConroler._instance.powerState = cc.fx.GameTool.getUserPowerTime();
            if (MapConroler._instance.powerState == false) {
                let pauseNode = null;
                if (cc.fx.GameConfig.GM_INFO.winStreak < 10 || cc.fx.GameConfig.GM_INFO.otherLevel > 0) {
                    this.exit.active = true;
                    pauseNode = this.exit;
                }
                else {
                    this.win.active = true;
                    pauseNode = this.win;
                }

                pauseNode.scale = 0.3;
                cc.tween(pauseNode)
                    .to(0.2, { scale: 1.05 }, { easing: 'backOut' })
                    .to(0.15, { scale: 1.0 }, { easing: 'sineOut' })
                    .start();
                pauseNode.getChildByName("Health").getChildByName("queding").active = false;
            }
            else {
                if (cc.fx.GameConfig.GM_INFO.winStreak >= 10 && cc.fx.GameConfig.GM_INFO.otherLevel == 0) {
                    let pauseNode = null;
                    this.win.active = true;
                    pauseNode = this.win;
                    pauseNode.scale = 0.3;
                    cc.tween(pauseNode)
                        .to(0.2, { scale: 1.05 }, { easing: 'backOut' })
                        .to(0.15, { scale: 1.0 }, { easing: 'sineOut' })
                        .start();
                    pauseNode.getChildByName("Health").getChildByName("queding").active = true;
                }
                else {
                    MapConroler._instance.returnHome();
                }
            }
        }
    }

    returnHome() {
        if (cc.fx.GameConfig.GM_INFO.otherLevel == 0) {
            cc.fx.GameTool.setWinStreak("give_up");
            MapConroler._instance.trackFinishi();
        }

        MapConroler._instance.returnHome();
    }

    //重开游戏
    clickRestart(event, customEventData) {
        cc.fx.AudioManager._instance.playEffect("anniu_little", null);
        //当游戏没开始前，可直接重开
        if (MapConroler._instance.gameStart == false) {
            MapConroler._instance.againLevel();
        }
        //游戏开始后
        else {
            //没有无限体力
            MapConroler._instance.powerState = cc.fx.GameTool.getUserPowerTime();
            if (MapConroler._instance.powerState == false) {
                console.log("没有无限体力", MapConroler._instance.powerState);
                //首页重开按钮
                if (customEventData != "hp") {
                    console.log("从首页一级页面点击重开直接重开");
                    let pauseNode = null;
                    if (cc.fx.GameConfig.GM_INFO.winStreak < 10 || cc.fx.GameConfig.GM_INFO.otherLevel > 0) {
                        this.exit.active = true;
                        pauseNode = this.exit;
                    }
                    else {
                        this.win.active = true;
                        pauseNode = this.win;
                    }
                    pauseNode.scale = 0.3;
                    cc.tween(pauseNode)
                        .to(0.2, { scale: 1.05 }, { easing: 'backOut' })
                        .to(0.15, { scale: 1.0 }, { easing: 'sineOut' })
                        .start();
                    pauseNode.getChildByName("Health").getChildByName("queding").active = true;
                }
                else {
                    console.log("从二级页面点击重开");
                    if (cc.fx.GameConfig.GM_INFO.otherLevel == 0) {
                        cc.fx.GameTool.setWinStreak("fail");
                        MapConroler._instance.trackFinishi("give_up");
                    }
                    MapConroler._instance.againLevel();
                }
            }
            //有无限体力
            else {
                console.log("______有无限体力", MapConroler._instance.powerState);
                if (customEventData != "hp") {
                    console.log("从首页一级页面点击重开直接重开");
                    if (cc.fx.GameConfig.GM_INFO.winStreak >= 10 && cc.fx.GameConfig.GM_INFO.otherLevel == 0) {
                        let pauseNode = null;
                        this.win.active = true;
                        pauseNode = this.win;
                        pauseNode.scale = 0.3;
                        cc.tween(pauseNode)
                            .to(0.2, { scale: 1.05 }, { easing: 'backOut' })
                            .to(0.15, { scale: 1.0 }, { easing: 'sineOut' })
                            .start();
                        pauseNode.getChildByName("Health").getChildByName("queding").active = true;
                    }
                    else {
                        console.log("没有连胜，直接重开");
                        MapConroler._instance.againLevel();
                    }
                }
                else {
                    console.log("从二级页面点击重开");
                    cc.fx.GameTool.setWinStreak("fail");
                    MapConroler._instance.trackFinishi("give_up");
                    MapConroler._instance.againLevel();
                }
            }
        }
    }

    //取消
    cancelExit() {
        this.exit.active = false;
        this.win.active = false;
        // 获取场景中的 pause 组件
        // const pauseTs = cc.find("Canvas");
        // if (pauseTs) {
        //     const pause = pauseTs.getComponent(SceneManager);
        //     if (pause) {
        //         pause.closePause();
        //     } else {
        //         console.warn("pause 组件未找到");
        //     }
        // } else {
        //     console.warn("pause 节点未找到");
        // }
    }
    //关闭ui
    closeUi() {
        cc.fx.AudioManager._instance.playEffect("anniu_little", null);
        cc.fx.AudioManager._instance.playEffect("anniu_little", null);
        if (this.node) {
            this.node.active = false;
        }
    }

    // update (dt) {}
}
