const GameModule = require('../utils/GameModule');
const buildingLevel = require('../data/buildingLevel');
const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;

cc.Class({
    extends: cc.Component,

    properties: {
        //技能3的提升倍数
        multiple: {
            default: 1,
            visible: false,
            type: cc.Float
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        GameModule.skill = this;

        this.skills = Global.skills;

        this.getAllSkillInfo();
        //先获取技能3的加成倍数
        this.getSkill3Ability();

        this.updateAutoGetGold();
        this.updateClickGold();

        this.setupEventListener();
    },

    //添加相关接收通知
    setupEventListener() {
        GameEvent.on(GameNotificationKey.UpBuildingLevel, this, this.updateClickGold);

        GameEvent.on(GameNotificationKey.UpdateFixationSkill, this, (skillInfo) => {
            this.updateSkill(skillInfo);
        });

        //使用实时技能通知
        GameEvent.on(GameNotificationKey.UseTimeSkill, this, (skillInfo) => {
            this.useTimeSkill(skillInfo);
        });

        //重置技能CD时间通知
        GameEvent.on(GameNotificationKey.ResetSkill, this, () => {
            this.resetSkill();
        });
    },

    //技能列表解锁或升级技能
    updateSkill(skillInfo) {
        switch (skillInfo.skillId) {
            case 4:
                this.skill4Info.skillLevel = skillInfo.level;
                this.updateAutoGetGold();
                break;
            case 5:
                this.skill5Info.skillLevel = skillInfo.level;
                this.updateClickGold();
                break;
            default:
                break;
        }
    },

    //技能实时技能
    useTimeSkill(skillInfo) {
        switch (skillInfo.skillId) {
            case 1:
                this.skill1Info.skillLevel = skillInfo.level;
                this.skill1Info.skillStatus = 1;
                this.skill1Info.cdStarTime = skillInfo.td * 1000;
                this.updateAutoGetGold();
                break;
            case 2:
                this.skill2Info = skillInfo;
                GameModule.userInfo.gold += (GameModule.userInfo.gold * this.skill2Info.mt);
                break;
            case 3:
                this.skill3Info.skillLevel = skillInfo.level;
                this.skill3Info.skillStatus = 1;
                this.skill3Info.cdStarTime = skillInfo.td * 1000;
                this.getSkill3Ability();
                break;
            default:
                break;
        }
    },

    //重置技能CD时间
    resetSkill() {
        this.skill1Info.cdStarTime = 0;
        this.skill1Info.skillStatus = 0;
        this.updateAutoGetGold();

        this.skill3Info.cdStarTime = 0;
        this.skill3Info.skillStatus = 0;
        this.multiple = 1;
        this.updateClickGold();
        GameModule.userInfo.refreshSecondText();
        GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
    },

    //获取所有技能的信息
    getAllSkillInfo() {
        this.skill1Info = this.skills.find(n => {
            return n.skillId == 1;
        });

        this.skill2Info = this.skills.find(n => {
            return n.skillId == 2;
        });

        this.skill3Info = this.skills.find(n => {
            return n.skillId == 3;
        });

        this.skill4Info = this.skills.find(n => {
            return n.skillId == 4;
        });

        this.skill5Info = this.skills.find(n => {
            return n.skillId == 5;
        });
    },

    // start () {
    //
    // },

    update (dt) {
        if (this.skill1Info && this.skill1Info.skillStatus == 1) {
            if (this.skill1Info.cdStarTime > 0) {
                this.skill1Info.cdStarTime -= (dt * 1000);
            }

            if (this.skill1Info.cdStarTime < 0) {
                this.skill1Info.cdStarTime = 0;
                this.skill1Info.skillStatus = 2;
                this.updateAutoGetGold();
            }
        }

        if (this.skill3Info && this.skill3Info.skillStatus == 1) {
            if (this.skill3Info.cdStarTime > 0) {
                this.skill3Info.cdStarTime -= (dt * 1000);
            }

            if (this.skill3Info.cdStarTime < 0) {
                this.skill3Info.cdStarTime = 0;
                this.skill3Info.skillStatus = 2;
                this.multiple = 1;
                this.updateClickGold();
                GameModule.userInfo.refreshSecondText();
                GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
            }
        }
    },

    //更新每秒自动点击生成金币的速率
    updateAutoGetGold() {

        var second = 1;
        var click = 0;

        var skill1 = this.getSkill1Ability();
        if (skill1 != null) {
            click += skill1.cc / skill1.iv;
        }

        var skill4 = this.getSkill4Ability();
        if (skill4 != null) {
            click += skill4.cc / skill4.iv;
        }

        if (click > 0) {
            GameModule.userInfo.secondClick = second / click;
        } else {
            GameModule.userInfo.secondClick = 0;
        }
    },

    //更新每次点击生成金币的数量
    updateClickGold() {
        var clickGold = this.getBuildingGold();
        clickGold = clickGold * this.getSkill5Ability();
        GameModule.userInfo.coinTap = clickGold * this.multiple * GameModule.userInfo.perpetualClickMt * GameModule.userInfo.starMt;
    },

    //总部大楼等级技能
    getBuildingGold() {
        this.userBuildingLevel = Global.userData.buildingLevel;
        // let buildingObj = buildingLevel.find(n => {
        //     return n.level == this.userBuildingLevel;
        // });
        // let clickGold = buildingObj.gold;
        let clickGold = 25 + this.userBuildingLevel * 5;
        return clickGold;
    },

    //主动释放技能1，多少分钟内每秒自动点击多少次
    getSkill1Ability() {
        if (this.skill1Info == undefined || this.skill1Info.skillLevel <= 0 ) {
            return null;
        }
        
        if (this.skill1Info.skillStatus != 1 || this.skill1Info.cdStarTime == 0) {
            return null;
        }

        let skillInfo = Global.BuildingManager.getSkillLevelInfo(this.skill1Info.skillId, this.skill1Info.skillLevel);
        return skillInfo;
    },

    //主动释放技能3，增加加成倍数
    getSkill3Ability() {
        if (this.skill3Info == undefined || this.skill3Info.skillLevel <= 0 ) {
            return null;
        }

        if (this.skill3Info.skillStatus != 1 || this.skill3Info.cdStarTime == 0) {
            return null;
        }

        let skillInfo = Global.BuildingManager.getSkillLevelInfo(this.skill3Info.skillId, this.skill3Info.skillLevel);
        this.multiple = skillInfo.mt;
        GameModule.userInfo.multiple = this.multiple;
    },

    //永久技能1，指定时间内自动点击次数
    getSkill4Ability() {
        if (this.skill4Info == undefined || this.skill4Info.skillLevel <= 0 ) {
            return null;
        }

        let skillInfo = Global.BuildingManager.getSkillLevelInfo(this.skill4Info.skillId, this.skill4Info.skillLevel);
        return skillInfo;
    },

    //永久技能2，每次点击产出的金币提升
    getSkill5Ability() {
        if (this.skill5Info == undefined || this.skill5Info.skillLevel <= 0 ) {
            return 1;
        }

        let skillInfo = Global.BuildingManager.getSkillLevelInfo(this.skill5Info.skillId, this.skill5Info.skillLevel);
        let mt = skillInfo.mt;
        return mt;
    },

    //永久技能3，减少CD时间
    getSkill6Ability() {
        let skillInfo = this.skills.find(n => {
            return n.skillId == 6;
        });
        skillInfo = Global.BuildingManager.getSkillLevelInfo(skillInfo.skillId, skillInfo.skillLevel);
        let rcd = (1 - skillInfo.rcd);
        return rcd;
    },
});
