const JobPageType = require('../utils/GameEnum').JobPageType;
const JobApi = require('../net/JobApi');
const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
cc.Class({
    extends: cc.Component,

    properties: {
        titleText: cc.RichText,
        confirmText: cc.RichText,
        pageView: cc.PageView,
        levelUpNode: cc.Node,

        top: cc.Node,

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

        myActor: [],

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

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

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        let offset = cc.view.getVisibleSize().height < 1624 ? 100 : 0;
        this.top.y = cc.view.getVisibleSize().height / 2 - this.top.height / 2 + offset;
        this.node.height = cc.view.getVisibleSize().height;
    },

    start() {

    },

    showFromTalent(type, actors) {
        this.myActor = actors;
        this.show(type, this.myActor.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.node.y = -174;
        this.pageView.getComponent('PVCtrl').bind(this.actorInfo.jobId);

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

        JobApi.changeJobList(this.actorInfo.uid,
            (response) => {
                // this.pageView.getComponent('PVCtrl').bindJobList(response.jobList);
                this.consume.getComponent('Consume').bindChangeJob(response.itemConsume, response.userPack);
            },
            (code, msg) => {
                this.showTips('网络错误');
            }
        );

    },

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

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


        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 = '<outline color=#690802 width=3><b>进阶</b></outline>';
        this.confirmText.string = '<outline color=#690802 width=1><b>进阶</b></outline>';


        let self = this;
        JobApi.levelUpInfo(this.actorInfo.uid,
            (response) => {
                this.consume.getComponent('Consume').bindDataLevelUp(response.itemConsume, response.userPack);
                this.levelUpNode.getComponent('LevelUp').bind(self.actorInfo, response.upgradeVo);
                this.upgradeName = response.upgradeVo.jobLevelName;
                this.upgradeLevel = self.actorInfo.jobLevel + 1;
            },
            (code, msg) => {
                this.showTips('网络错误');
            }
        );

    },


    confirmClick() {
        switch (this.type) {

            case JobPageType.ChangeJob:
                var job = this.pageView.getComponent('PVCtrl').getCurrentJob();
                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').getCurrentJob();
                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.showTips(response.msg);
                        this.onLevelUpSuccess();
                    },
                    (code, msg) => {
                        this.onFail(msg);
                    });
                break;
        }
    },


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

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

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


    onChangeJobSuccess(msg) {
        var job = this.pageView.getComponent('PVCtrl').getCurrentJob();
        this.changeJobSuccess = cc.instantiate(this.changeJobSuccessPrefab);
        this.changeJobSuccess = this.changeJobSuccess.getComponent('ChangeJobSuccess');
        this.changeJobSuccess.show(this.node, this.actorInfo, job, msg, () => {
            this.close();
        });
        Global.GameEvent.fire(GameNotificationKey.RefreshUserInformation);
    },


    onLevelUpSuccess() {
        this.levelUpSuccess = cc.instantiate(this.levelUpSuccessPrefab);
        this.levelUpSuccess = this.levelUpSuccess.getComponent('JobLevelUpSuccess');
        this.levelUpSuccess.show(this.node, '进阶成功', '恭喜你的艺人进阶成为：', this.upgradeLevel, this.upgradeName, () => {
            this.close();
        });
        Global.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.myActor = users;
    },

    pageviewChangeEvent() {
        console.log('page change: ', this.pageView.getCurrentPageIndex());
    },

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

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