const SkillApi = require('../net/SkillApi');
const DWTool = require("../utils/DWTool");

cc.Class({
    extends: cc.Component,

    properties: {
        topSkillItems:{
            tooltip: '技能顶部的四个实时技能',
            default: [],
            type: [cc.Node]
        },
        
        skillScrollView: cc.Node,

    },

    // LIFE-CYCLE CALLBACKS:
    onLoad () {    
        this.node.height = cc.view.getVisibleSize().height;
        this.setUpNotification();
        this.skillScrollView.active = false;        
    },

    setUpNotification() {
        GameEvent.on("skillAlert_done", this, () => {
            this.unschedule(this.timeAction, this);
            this.node.destroy();
        });
    },

    start () {
        this.getAllSkills().then((respondData) => {
            console.log(respondData);
            /// 初始化这个scrollView
            Global.rcdSkillLevel = respondData.skills2[2].skillLevel;
            this.skillScrollView.getComponent('SkillScrollView').init(respondData);
            this.skillScrollView.active = true;
            this.initTopSkillItem(respondData.skills1);
            this.schedule(this.timeAction, 1);
        }).catch(({code, msg}) => {
            console.log(code, msg);
        });
    },

    onDestroy() {
        GameEvent.off("skillAlert_done", this);
    },

    /// 初始化顶部的四个实时技能
    initTopSkillItem(timeSkill) {
        var isAllLocked = true;
        for(let i = 0; i < 3; ++i) {
            let skillItemScript = this.topSkillItems[i].getComponent('SkillTopItem');
            let timeSkillData = timeSkill[i];
            if (timeSkillData.isHave == 1) {
                isAllLocked = false;
            }
            skillItemScript.init(timeSkillData);
        }

        let upDateItemScript = this.topSkillItems[3].getComponent('SkillTopItem');
        upDateItemScript.initUpdateSkill(isAllLocked);
    },

    timeAction() {
        for(let i = 0; i < 4; ++i) {
            let skillItemScript = this.topSkillItems[i].getComponent('SkillTopItem');
            skillItemScript.updateTime();
        }
    },
    /// 网络请求
    getAllSkills() {
        return new Promise((resolve, reject) => {
            // 获取目标用户的建筑
            SkillApi.getAllSkills((respondData) => {
                resolve(respondData);
            }, ({code, msg}) => {
                reject({code, msg});
            });
        })
    },


    // update (dt) {},
});
