const DWTool = require("../utils/DWTool");
const {GameNotificationKey} = require('../utils/GameEnum');
var TalentApi = require('../net/TalentApi');

//任务状态[0 : 完成可领取, 1 : 未完成, 2 : 完成已领取]
var TalentMissionItemType = cc.Enum({
    Gain: 0,
    NoFinished: 1,
    Finished: 2,
});

cc.Class({
    extends: cc.Component,

    properties: {
        awardNode1: cc.Node,
        awardItemNode1: cc.Node,
        countLabel1: cc.Label,

        awardNode2: cc.Node,
        awardItemNode2: cc.Node,
        countLabel2: cc.Label,

        awardRichText: cc.RichText,

        noButton: cc.Button,
        gainButton: cc.Button,
        finishButton: cc.Button,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    hideAllButton() {
        this.noButton.node.active = false;
        this.gainButton.node.active = false;
        this.finishButton.node.active = false;
    },

    onDisable() {
        this.awardNode1.active = false;
        this.awardNode2.active = false;
    },

    configData(model, invitedCount) {

        this.model = model;

        this.hideAllButton();

        switch (model.state) {
            case TalentMissionItemType.NoFinished:
                this.noButton.node.active = true;
                break;
            case TalentMissionItemType.Finished:
                this.finishButton.node.active = true;
                break;
            case TalentMissionItemType.Gain:
                this.gainButton.node.active = true;
                break;
        }

        let coin = model.coin;
        let diamond = model.diamond;
        let ticket = model.ticket;

        if (coin > 0) {
            this.awardNode1.active = true;
            cc.loader.loadRes("talent/talent_invite_coin", cc.SpriteFrame, (err, spriteFrame) => {
                if (err) {

                } else {
                    this.awardItemNode1.getComponent('cc.Sprite').spriteFrame = spriteFrame;
                }
            });
            this.countLabel1.string = DWTool.coinParse(coin);
        }

        if (diamond > 0) {
            if (this.awardNode1.active) {
                this.awardNode2.active = true;
                cc.loader.loadRes("talent/talent_invite_diamond", cc.SpriteFrame, (err, spriteFrame) => {
                    if (err) {

                    } else {
                        this.awardItemNode2.getComponent('cc.Sprite').spriteFrame = spriteFrame;
                    }
                });
                this.countLabel2.string = DWTool.coinParse(diamond);
            } else {
                this.awardNode1.active = true;
                cc.loader.loadRes("talent/talent_invite_diamond", cc.SpriteFrame, (err, spriteFrame) => {
                    if (err) {

                    } else {
                        this.awardItemNode1.getComponent('cc.Sprite').spriteFrame = spriteFrame;
                    }
                });
                this.countLabel1.string = DWTool.coinParse(diamond);
            }
        }

        if (ticket > 0) {
            if (this.awardNode1.active) {
                this.awardNode2.active = true;
                cc.loader.loadRes("talent/talent_invite_ticket", cc.SpriteFrame, (err, spriteFrame) => {
                    if (err) {

                    } else {
                        this.awardItemNode2.getComponent('cc.Sprite').spriteFrame = spriteFrame;
                    }
                });
                this.countLabel2.string = DWTool.coinParse(ticket);
            } else {
                this.awardNode1.active = true;
                cc.loader.loadRes("talent/talent_invite_ticket", cc.SpriteFrame, (err, spriteFrame) => {
                    if (err) {

                    } else {
                        this.awardItemNode1.getComponent('cc.Sprite').spriteFrame = spriteFrame;
                    }
                });
                this.countLabel1.string = DWTool.coinParse(ticket);
            }
        }

        this.awardRichText.string = `签约奖励\n<color=#AF7434 >（艺人：${invitedCount}/${model.count}名）</c>`;
    },

    gainAward() {
        TalentApi.postGainAward(this.model.id, (responseData) => {
            this.refreshButtonState();
        }, (error) => {
            console.log('星探领取 error' + error);
        });
    },

    refreshButtonState() {
        this.model.state = TalentMissionItemType.Finished;
        this.hideAllButton();
        this.finishButton.node.active = true;
        GameEvent.fire(GameNotificationKey.TalentGainAward, this.model);
    },
    // update (dt) {},
});
