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

cc.Class({
    extends: cc.Component,

    properties: {
        questItem: {
            default: null,
            type: cc.Node,
        },
        questWrap: {
            tooltip: '每日任务容器',
            default: null,
            type: cc.Node
        },
        actGifts: {
            tooltip: '活跃度奖励节点',
            default: [],
            type: [cc.Node]
        },
        actGiftSprite: {
            tooltip: '活跃度奖励素材',
            default: [],
            type: [cc.SpriteFrame]
        },
        actProgress: {
            tooltip: '每日活跃度进度',
            default: null,
            type: cc.ProgressBar
        },
        userDegreeLabel: {
            tooltip: '用户当前活跃度',
            default: null,
            type: cc.Label
        },
        _userDegree: 0,
        userDegree: {
            get () {
                return this._userDegree
            },
            set (data) {
                this._userDegree = data

                // 设置用户当前活跃度
                this.actProgress.progress = data / 100
                this.userDegreeLabel.string = data
            }
        }
    },


    onLoad () {
        this.userDegree = 0
        this.dailyDegreeList = []
    },

    init (quest) {
        this.quest = quest
        this.questItem.active = false

        Api.httpGet({
            url: "/daily/getUserDailyInfo",
            data: {},
            success: ({dailyDegreeList, dailyTaskList, userDegree}) => {
            this.dailyDegreeList = dailyDegreeList.sort((a, b) => {
            return a.degree - b.degree
        })
        this.dailyTaskList = dailyTaskList
        this.userDegree = parseInt(userDegree)

        this._setDegreeList()
        this._setTaskList()
    }
    })
    },

    /**
     * 渲染每日任务列表
     */
    _setTaskList () {
        this.dailyTaskList.forEach((item, index) => {
            let questItem = cc.instantiate(this.questItem)
            this.questWrap.addChild(questItem)
        questItem.active = true
        questItem.getComponent('QuestDailyItem').init(this, item, index)
    })
    },

    /**
     * 设置活跃度奖励进度条
     */
    _setDegreeList () {
        this.dailyDegreeList.forEach((item, index) => {
            let giftFrame = this.actGiftSprite[item.state]
            this.actGifts[index].getComponent('cc.Sprite').spriteFrame = giftFrame
    })
    },

    updateStatus (task) {
        let _degree = this.userDegree + parseInt(task.degree)
        this.userDegree = _degree > 100 ? 100 : _degree

        this.dailyDegreeList.forEach((item, index) => {
            if(this.userDegree >= item.degree && item.state != 2) {
            item.state = 0
        }

        let giftFrame = this.actGiftSprite[item.state]
        this.actGifts[index].getComponent('cc.Sprite').spriteFrame = giftFrame
    })
    },

    /**
     * 领取活跃度奖励
     * @param {object} event
     * @param {number} index 活跃度奖励节点索引
     */
    handleGiftBtn (event, index) {
        let act = this.dailyDegreeList[index]

        // state = 0, 可领取状态
        if(act.state == 0) {
            Api.httpPost({
                url: "/daily/getDailyDegreeReward",
                data: {
                    degree: act.degree
                },
                success: (res) => {
                // 更新全局userInfo
                this.quest.updateUserInfo(act.coin, act.diamond, act.ticket)

            // state = 2, 已领取状态
            act.state = 2

            // 获取当前活跃度对应的奖品内容并展示
            event.target.getComponent('cc.Sprite').spriteFrame = this.actGiftSprite[2]

            // 显示动画
            setTimeout(() => {
                this.quest.showActGift({
                ticket: act.ticket,
                diamond: act.diamond,
                coin: act.coin
            })
        }, 200);
        },
            fail: () => {
                // 领取失败

            }
        })
        }
    },

    start () {

    }
});
