// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.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
//const StatesManage = require("StatesManage");
cc.Class({
    extends: require("BaseUiConstroller"),

    properties: {
        interfacaArr: { //界面列表
            default: [],
            type: cc.Node,
        },
        passArr: { //
            type: cc.Prefab,
            default: [],
        },
        audioArr: { //音频
            type: cc.AudioClip,
            default: [],
        },
        Column_Right_Grid: cc.Node,

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.bg1 = cc.find("Canvas/Panel_Botton/Btn_Activity/Bg_1");
        this.bg2 = cc.find("Canvas/Panel_Botton/Btn_Game/Bg_2");
        this.bg3 = cc.find("Canvas/Panel_Botton/Btn_Role/Bg_3");
        this.Btn_Activity = cc.find("Canvas/Panel_Botton/Btn_Activity");
        this.Btn_Game = cc.find("Canvas/Panel_Botton/Btn_Game");
        this.Btn_Role = cc.find("Canvas/Panel_Botton/Btn_Role");
        this.game = cc.find("Canvas/Game");
        this.Interface_Node = cc.find("Canvas/Interface_Node");
        this.Panel_Botton = cc.find("Canvas/Panel_Botton");
        this.back_btn = cc.find("Canvas/UiController/Btn_Back");

        this.Interface_PassArr = cc.find("Canvas/Interface_PassArr");

        this.role_headArr = cc.find("Canvas/Interface_Against/Panel_Middle/Bg/PassNode").children;

        var self = this;
        cc.loader.loadRes("prefab/Items/Grid", (err, prefab) => {

            for (let i = 0; i < 30; i++) {
                self.Column_Right_Grid.addChild(cc.instantiate(prefab));
            }

        });

    },

    start() {
        this.init();
        this.Role_Head_Event();
        this.interface_info = cc.find("Canvas/Interface_Info").getComponent('Interface_Info');



    },
    Role_Head_Event() {
        this.role_headArr.forEach(element => {
            element.on(cc.Node.EventType.TOUCH_START, () => {
                cc.audioEngine.playEffect(this.audioArr[0]);
                console.log("触摸头像");
                //打开全部关卡界面
                this.Interface_PassArr.active = true;
            });
        });

    },
    update(dt) {
        this.role_headArr.forEach(element => {
            element.active = false;
        });

        this.role_headArr[this.GameState._currPass].active = true;

    },

    Bottonbtn_Touch(event) {
        cc.audioEngine.playEffect(this.audioArr[0]);
        this.Interface_Node.removeAllChildren();
        if (event.target.name == "Btn_Activity") {
            this.bg1.active = true;
            this.bg2.active = false;
            this.bg3.active = false;
            this.Btn_Activity.setScale(1);
            this.Btn_Game.setScale(0.8);
            this.Btn_Role.setScale(0.8);

            this.interfacaArr[0].active = false;
            this.interfacaArr[1].active = false;
            cc.resources.load("prefab/Interface/Interface_Activitys", (err, prefab) => {
                var interfaceactivity = cc.instantiate(prefab);
                this.Interface_Node.addChild(interfaceactivity);

            });
        }
        if (event.target.name == "Btn_Game") {
            this.bg1.active = false;
            this.bg2.active = true;
            this.bg3.active = false;
            this.Btn_Activity.setScale(0.8);
            this.Btn_Game.setScale(1);
            this.Btn_Role.setScale(0.8);

            this.change_interface(this.interfacaArr, 0);
            this.game.removeAllChildren();
            this.Interface_Node.removeAllChildren();
        }
        if (event.target.name == "Btn_Role") {
            this.bg1.active = false;
            this.bg2.active = false;
            this.bg3.active = true;
            this.Btn_Activity.setScale(0.8);
            this.Btn_Game.setScale(0.8);
            this.Btn_Role.setScale(1);

            this.change_interface(this.interfacaArr, 1);
            this.Interface_Node.removeAllChildren();
        }
        if (event.target.name == "Btn_Back") {
            this.game.removeAllChildren();
            this.Panel_Botton.active = true;
            this.change_interface(this.interfacaArr, 0);
            this.back_btn.active = false;
            this.GameMode._gamestart = false;
        }
        if (event.target.name == "Btn_Back_Pass") {

            this.change_interface(this.interfacaArr, 0);
            this.Interface_PassArr.active = false;

        }
    },
    //触发按钮 关卡
    btn_Touch(event) {

        cc.audioEngine.playEffect(this.audioArr[0]);
        this.game.removeAllChildren();
        this.Interface_Node.removeAllChildren();
        this.interfacaArr[0].active = false;
        this.interfacaArr[1].active = false;
        if (event.target.name == "Btn_Against") {
            this.Panel_Botton.active = false;
            //this.GameState._currPass = 0;
            this.game.addChild(cc.instantiate(this.passArr[this.GameState._currPass]));
            // var self = this;
            // cc.resources.loadDir("prefab/Pass", (err, prefabs) => {
            //     var interfacegame = cc.instantiate(prefabs[]);
            //     self.game.addChild(interfacegame);
            // });
        }


        // this.game.addChild(StatesManage.Game_Pass[this.GameState._currPass]);
        //var self = this;
        //
        // cc.loader.loadRes("prefab/Pass", (err, prefabs) => {
        //     console.log("关卡：", self.GameState._currPass);
        //     var interfacegame = cc.instantiate(prefabs[self.GameState._currPass]);
        //     self.game.addChild(interfacegame);
        // });
    },
    init() {
        //角色脚本
        this.GameState = cc.find("GameStates").getComponent("GameStates");
        this.GameMode = cc.find("GameMode").getComponent("GameMode");
        // this.controScp = this.playerController.getComponent('PlayerController');
        // this.charactortroScp = this.Charactor.getComponent('Charactor');
        this.font_Arr = cc.find("Canvas/UiController/Ui_caution").children;
        this.change_interface(this.interfacaArr, 0);
        this.bg2.active = true;
        this.Btn_Activity.setScale(0.8);
        this.Btn_Game.setScale(1);
        this.Btn_Role.setScale(0.8);
    },
    actions() {

        let action1 = cc.scaleTo(0.8, 0.8, 0.8);
        let action2 = cc.scaleTo(1, 2, 2);
        this.ui_countdown(action1, action2);

    },
    ui_countdown(action, action2) {
        for (let i = 0; i < 3; i++) {
            this.scheduleOnce(function() {
                cc.audioEngine.playEffect(this.audioArr[6]);
                this.font_Arr[i].active = true;
                this.font_Arr[i].runAction(action);
                this.scheduleOnce(function() {
                    this.font_Arr[i].active = false;
                    this.font_Arr[i].setScale(2);
                }, 0.6);
            }, i + 1);
        }
        this.scheduleOnce(function() {
            this.font_Arr[3].active = true;
            this.font_Arr[3].runAction(action2);
            this.scheduleOnce(function() {
                this.font_Arr[3].active = false;
                this.back_btn.active = true;
            }, 0.8);
            cc.audioEngine.playEffect(this.audioArr[5]);
            this.GameMode._gamestart = true;
        }, 4);
    },
    Ui_Shake(node_index) {
        if (node_index == 1) { //左
            this.font_Arr[4].active = true;
            this.Shake(this.font_Arr[4], 2);
            cc.audioEngine.playEffect(this.audioArr[6]);
            this.scheduleOnce(function() {
                this.font_Arr[4].active = false;
            }, 0.5);
        }
        if (node_index == 2) { //右
            this.font_Arr[5].active = true;
            this.Shake(this.font_Arr[5], 2);
            cc.audioEngine.playEffect(this.audioArr[6]);
            this.scheduleOnce(function() {
                this.font_Arr[5].active = false;
            }, 0.5);
        }
        if (node_index == 3) { //红屏
            this.font_Arr[8].active = true;
            this.scheduleOnce(function() {
                this.font_Arr[8].active = false;
            }, 0.5);
        }
        if (node_index == 4) { //血量不足
            this.font_Arr[7].active = true;
            this.scheduleOnce(function() {
                this.font_Arr[7].active = false;
            }, 0.5);
        }
        if (node_index == 5) { //能量不足
            this.font_Arr[6].active = true;
            this.scheduleOnce(function() {
                this.font_Arr[6].active = false;
            }, 0.5);
        }
    },
    Shake(node_shake, index) {
        let x = node_shake.x;
        let y = node_shake.y;
        let offset = 10;
        if (index == 0) {
            let action = cc.repeatForever(
                cc.sequence(
                    // cc.moveTo(0.018, cc.v2(x + (5 + offset), y + (offset + 7))),
                    // cc.moveTo(0.018, cc.v2(x - (6 + offset), y + (offset + 7))),
                    // cc.moveTo(0.018, cc.v2(x - (13 + offset), y + (offset + 3))),
                    // cc.moveTo(0.018, cc.v2(x + (3 + offset), y - (6 + offset))),
                    // cc.moveTo(0.018, cc.v2(x - (5 + offset), y + (offset + 5))),
                    cc.moveTo(0.018, cc.v2(x + (2 + offset), y - (8 + offset))),
                    cc.moveTo(0.018, cc.v2(x - (8 + offset), y - (10 + offset))),
                    cc.moveTo(0.018, cc.v2(x + (3 + offset), y + (offset + 10))),
                    cc.moveTo(0.018, cc.v2(x + (0 + offset), y + (offset + 0)))
                )
            )
            node_shake.runAction(action);
            setTimeout(() => {
                node_shake.stopAction(action);
                node_shake.x = x;
                node_shake.y = y;
            }, 1000);
        } else if (index == 1) {
            let action = cc.repeatForever(
                cc.sequence(
                    cc.moveTo(0.018, cc.v2(x + (5 + offset), y + (offset + 7))),
                    cc.moveTo(0.018, cc.v2(x - (6 + offset), y + (offset + 7))),
                    cc.moveTo(0.018, cc.v2(x - (13 + offset), y + (offset + 3))),
                    cc.moveTo(0.018, cc.v2(x + (3 + offset), y - (6 + offset))),
                )
            )
            node_shake.runAction(action);
            setTimeout(() => {
                node_shake.stopAction(action);
                node_shake.x = x;
                node_shake.y = y;
            }, 1000);
        } else if (index == 2) {
            let action = cc.repeatForever(
                cc.sequence(
                    cc.moveTo(0.018, cc.v2(x + offset, y + offset)),
                    cc.moveTo(0.018, cc.v2(x - offset, y + offset)),
                    cc.moveTo(0.018, cc.v2(x - offset, y + offset)),
                    cc.moveTo(0.018, cc.v2(x + offset, y - offset)),
                    cc.moveTo(0.018, cc.v2(x - offset, y + offset)),
                )
            )
            node_shake.runAction(action);
            setTimeout(() => {
                node_shake.stopAction(action);
                node_shake.x = x;
                node_shake.y = y;
            }, 1000);
        }
    },

});