const JobPageType = require('../utils/GameEnum').JobPageType;
const JobApi = require('../net/JobApi');
const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
const jobLevelList = require('../data/jobLevel');
const jobList = require('../data/job');
const cardList = require('../data/item');
const GameModule = require('../utils/GameModule');
cc.Class({
    extends: cc.Component,

    properties: {
        titleText: cc.RichText,
        confirmButton: cc.Button,
        confirmText: cc.RichText,
        pageView: cc.PageView,
        levelUpNode: cc.Node,
        bgNode: cc.Node,
        top: cc.Node,

        consume: cc.Node,
        user: cc.Node,

        myActorList: [],

        changeJobSuccessPrefab: cc.Prefab,
        levelUpSuccessPrefab: cc.Prefab,
        failPrefab: cc.Prefab,

        tipsNode: cc.Node,
        tipsLabel: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.node.height = cc.view.getVisibleSize().height;
        if (cc.view.getVisibleSize().height >= 1624) {
            this.top.getComponent(cc.Widget).top = 50;
            this.bgNode.getComponent(cc.Widget).top = -50;
            // this.informationNode.getComponent(cc.Widget).top = 185;
            // this.infoTop = 185;
        }

        this.consume.getComponent('Consume').setConfirmButton(this.confirmButton);
    },

    start() {

    },

    showFromTalent(type, actors) {
        this.myActorList = actors;
        this.show(type, this.myActorList.pop(), 0);
    },

    show(type, actorInfo, zIndex) {
        this.type = type;
        this.actorInfo = actorInfo;
        this.node.parent = cc.find('Canvas');
        this.node.active = true;
        this.node.zIndex = zIndex + 1;

        switch (type) {

            case JobPageType.ChangeJob:
                this.changeJob();
                break;
            case JobPageType.ChooseJob:
                this.chooseJob(actorInfo);
                break;
            case JobPageType.LevelUp:
                this.levelUp();
                break;
        }
    },

    changeJob() {
        this.consume.active = true;
        this.user.active = false;
        this.pageView.node.active = true;
        this.levelUpNode.active = false;
        this.pageView.getComponent('PVCtrl').bind(this.actorInfo.jobId);

        this.titleText.string = '<b>职业转换</b>';
        this.confirmText.string = '<outline color=#690802 width=1><b>转职</b></outline>';

        this.pageViewScript = this.pageView.getComponent('PVCtrl');

        JobApi.changeJobList(this.actorInfo.uid,
            (response) => {
                this.userPack = response.userPack;
                this.bindChangeJobComsume(this.pageViewScript.getSelectedJob());
            },
            (code, msg) => {
                this.showTips('网络错误');
            }
        );
    },

    bindChangeJobComsume(selectedJob) {
        let needCard = '';
        let itemKey = Object.keys(selectedJob.itemInfo)[0];
        cardList.forEach((cardInfo) => {
            if (cardInfo.id == itemKey) {
                needCard = cardInfo;
                needCard.number = selectedJob.itemInfo[itemKey];
            }
        });
        this.pageViewScript.bindSelectedJobText();
        this.consume.getComponent('Consume').bindChangeJob(selectedJob.gold, needCard, this.userPack);
    },

    chooseJob() {
        this.consume.active = false;
        this.user.active = true;
        this.pageView.node.active = true;
        this.levelUpNode.active = false;
        this.pageView.getComponent('PVCtrl').bind();

        this.titleText.string = '<b>选择职业</b>';
        this.confirmText.string = '<outline color=#690802 width=1><b>确定</b></outline>';

        this.pageViewScript = this.pageView.getComponent('PVCtrl');
        this.pageViewScript.bindSelectedJobText();
        this.bindChooseJob(this.actorInfo);
    },

    levelUp() {
        this.consume.active = true;
        this.user.active = false;
        this.pageView.node.active = false;
        this.levelUpNode.active = true;

        this.titleText.string = '<b>进阶</b>';
        this.confirmText.string = '<outline color=#690802 width=1><b>进阶</b></outline>';


        this.currentJobLevel = null;
        this.nextJobLevel = null;

        for (var i = 0; i < jobLevelList.length; i++) {
            let jobLevelInfo = jobLevelList[i];
            if (this.actorInfo.jobId === jobLevelInfo.jobId && this.actorInfo.jobLevel === jobLevelInfo.level) {
                this.currentJobLevel = jobLevelInfo;
                if (i >= jobLevelList.length - 1) {
                    return;
                }
                this.nextJobLevel = jobLevelList[i + 1];
                this.spend = this.nextJobLevel.salary;
            }
        }

        let needCard = '';
        let itemKey = Object.keys(this.nextJobLevel.itemInfo)[0];
        cardList.forEach((cardInfo) => {
            if (cardInfo.id == itemKey) {
                needCard = cardInfo;
                needCard.number = this.nextJobLevel.itemInfo[itemKey];
            }
        });

        this.upgradeName = this.nextJobLevel.name;
        this.upgradeLevel = this.nextJobLevel.level;

        let self = this;
        JobApi.levelUpInfo(this.actorInfo.uid,
            (response) => {
                this.consume.getComponent('Consume').bindDataLevelUp(self.nextJobLevel.salary, needCard, response.userPack);
                this.levelUpNode.getComponent('LevelUp').bind(self.actorInfo, self.nextJobLevel, response.wish);
            },
            (code, msg) => {
                this.showTips('网络错误');
            }
        );

    },


    confirmClick() {
        switch (this.type) {

            case JobPageType.ChangeJob:
                var job = this.pageView.getComponent('PVCtrl').getSelectedJob();
                this.spend = job.gold;
                JobApi.changeJob(this.actorInfo.uid, job.id,
                    (response, msg) => {
                        this.onChangeJobSuccess(msg);
                    },
                    (code, msg) => {
                        this.onFail(msg);
                    });
                break;
            case JobPageType.ChooseJob:
                var job = this.pageView.getComponent('PVCtrl').getSelectedJob();
                JobApi.chooseJob(this.actorInfo.uid, job.id,
                    (response) => {
                        this.onFail('选择成功', () => {
                            this.nextChoose();
                        });
                    },
                    (code, msg) => {
                        this.onFail(msg);
                    });

                break;
            case JobPageType.LevelUp:
                JobApi.levelUp(this.actorInfo.uid,
                    (response) => {
                        this.onLevelUpSuccess();
                    },
                    (code, msg) => {
                        this.onFail(msg);
                    });
                break;
        }
    },


    close() {
        this.node.destroy();
        this.node.zIndex = 0;
    },

    nextChoose() {
        if (this.myActorList && this.myActorList.length > 0) {
            this.actorInfo = this.myActorList.pop();
            this.bindChooseJob();
        } else {
            this.close();
            GameEvent.fire(GameNotificationKey.RefreshUserInformation);
        }
    },

    bindChooseJob() {
        this.user.getComponent('Actor').bind(this.actorInfo);
    },


    onChangeJobSuccess(msg) {
        GameModule.userInfo.grossIncome -= this.spend;
        let self = this;
        let targetJob = this.pageViewScript.getSelectedJob();
        let lastJob = null;
        jobList.forEach(jobInfo => {
            if (jobInfo.id === this.actorInfo.jobId) {
                lastJob = jobInfo;
            }
        });
        this.changeJobSuccess = cc.instantiate(this.changeJobSuccessPrefab);
        this.changeJobSuccess = this.changeJobSuccess.getComponent('ChangeJobSuccess');
        this.changeJobSuccess.show(this.node, lastJob, targetJob, () => {
            self.close();
        });
        GameEvent.fire(GameNotificationKey.RefreshUserInformation);
    },


    onLevelUpSuccess() {
        GameModule.userInfo.grossIncome -= this.spend;
        let self = this;
        this.levelUpSuccess = cc.instantiate(this.levelUpSuccessPrefab);
        this.levelUpSuccess = this.levelUpSuccess.getComponent('JobLevelUpSuccess');
        this.levelUpSuccess.show(this.node, '进阶成功', '恭喜你的艺人进阶成为：', this.upgradeLevel, this.upgradeName, () => {
            self.close();
        });
        GameEvent.fire(GameNotificationKey.RefreshUserInformation);
    },

    onFail(msg, callback) {
        this.fail = cc.instantiate(this.failPrefab);
        this.fail = this.fail.getComponent('JobPageFail');
        this.fail.show(this.node, msg, callback);
    },

    setNeedChooseJobUsers(users) {
        this.myActorList = users;
    },

    pageviewChangeEvent() {
        if (this.type === JobPageType.ChangeJob) {
            this.bindChangeJobComsume(this.pageViewScript.getSelectedJob());
        } else if (this.type === JobPageType.ChooseJob) {
            this.pageViewScript.bindSelectedJobText();
        }
    },

    showTips(text) {
        this.tipsNode.active = true;
        this.tipsLabel.string = text;
        this.scheduleOnce(() => {
            this.tipsNode.active = false;
        }, 2);
    }

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