const Api = require('../net/Api');

cc.Class({
    extends: cc.Component,

    properties: {
        questNode: cc.Node,
        questFrames: [cc.SpriteFrame],
        questBtns: [cc.Node],
        starNode: {
            tooltip: '星星节点',
            default: [],
            type: [cc.Node]
        },
        giftFrames: {
            tooltip: '奖励图片素材',
            default: [],
            type: [cc.SpriteFrame]
        },
        starFrames: {
            tooltip: '星星图片素材',
            default: [],
            type: [cc.SpriteFrame]
        },
        progressLabel: {
            tooltip: '奖励数量',
            default: null,
            type: cc.Label
        },
        progressBar: cc.ProgressBar,
        questTitle: {
            tooltip: '任务标题',
            default: null,
            type: cc.Label
        },
    },

    onLoad () {
        this.giftMap = ['diamond', 'coin', 'ticket']
    },

    start () {

    },

    init (tasks, sn) {
        this.tasks = tasks
        this.sn = parseInt(sn)
        let task = this.task = tasks[sn]
        let nameLevelMap = ['I', 'II', 'III', 'IV', 'V']
        let giftType
        let giftCount
        
        this.giftMap.forEach(n => {
            if(task[n] > 0) {
                giftType = n
                giftCount = task[n]
            }
        })
        
        // 设置当前任务标题
        this.questTitle.string = `${task.name}(${nameLevelMap[sn]}) ${task.msg + task.totalAimValue}`

        // 设置当前任务星级
        this._setStarLevel(sn)

        // 设置任务阶段进度条
        this._setProgress(task.aimValue, task.totalAimValue)

        // 设置任务领取按钮
        this._setGiftBtn(task.status, giftType, giftCount)
    },

    /**
     * 领取按钮点击
     */
    handleGiftBtn () {
        Api.httpPost({
            url: "/task/gain.do",
            data: {
                taskId: this.task.taskId,
                sn: this.task.sn
            },
            success: (res) => {
                // 领取成功
                this.sn += 1
                // this.init(this.tasks, this.sn)

            },
            fail: () => {
                // 领取失败

            }
        })
    },

    /**
     * 设置任务领取状态按钮
     * @param {number} status 任务状态[0 : 未完成, 1 : 完成可领取, 2 : 完成已领取]
     * @param {string} giftType 奖励类型[ticket: 艺人券, diamond: 钻石, coin: 金币]
     * @param {number} giftCount 奖励数量
     */
    _setGiftBtn (status, giftType, giftCount) {
        let btn
        if(status == 1) {
            btn = this.questBtns[0]
        } else {
            btn = this.questBtns[1]
        }
        btn.active = true
        let countNode = cc.find('/count_label', btn)
        let spriteNode = cc.find('/sprite', btn)
        let giftSprite
        this.giftMap.forEach((value, index) => {
            if(value == giftType) {
                giftSprite = this.giftFrames[index]
            }
        })
        

        countNode.getComponent('cc.Label').string = `x ${giftCount}`
        spriteNode.getComponent('cc.Sprite').spriteFrame = giftSprite
    },

    /**
     * 设置任务阶段进度条
     * @param {number} current 当前值
     * @param {number} total 阶段总值
     */
    _setProgress (current, total) {
        let _progress = (current / total).toFixed(1)
        console.log(_progress);
        this.progressBar.progress = _progress
        this.progressLabel.string = `${current} / ${total}`
    },

    /**
     * 设置当前任务星级
     * @param {number} level 星级
     */
    _setStarLevel (level) {
        this.starNode.forEach((node, index) => {
            if(index < level) {
                node.getComponent('cc.Sprite').spriteFrame = this.starFrames[1]
            } else {
                node.getComponent('cc.Sprite').spriteFrame = this.starFrames[0]
            }
        })
    }
});
