const GameModule = require("../utils/GameModule");

cc.Class({
    extends: cc.Component,

    properties: {
        questDaily: {
            tooltip: '每日任务容器',
            default: null,
            type: cc.Node
        },
        questMain: {
            tooltip: '主线任务容器',
            default: null,
            type: cc.Node
        },
        tab: [cc.Node],
        tabFade: cc.Node,
        tabIndex: {
            tooltip: '默认显示的Tab',
            default: 0,
            type: cc.Integer
        },
        mask: {
            tooltip: '遮罩层',
            default: null,
            type: cc.Node
        },
        wrap: {
            tooltip: '弹窗载体',
            default: null,
            type: cc.Node
        },
        actGift: {
            tooltip: '活跃度弹出层',
            default: null,
            type: cc.Node
        },
        actGiftFrames: {
            tooltip: '奖品大图',
            default: [],
            type: [cc.SpriteFrame]
        }

    },

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

        this.tabList = [{
            wrap: this.questMain,
            init: () => {
                this._initMain()
            },
        }, {
            wrap: this.questDaily,
            init: () => {
                this._initDaily()
            }
        }]

        this.mask.zIndex = 10
        this.wrap.zIndex = 11
        this.actGift.zIndex = 15
        this.actGift.active = false

        this._initTab()

        // setTimeout(() => {
        //     this.showActGift('coin')
        // }, 2000);
    },

    start () {
        // let space = 50;
        // let upAction = cc.moveTo(0.25, cc.v2(0, space));
        // let downAction = cc.moveBy(0.1, cc.v2(0, -space));
        // let bouncesActionArray = [upAction, downAction];
        //
        // this.node.runAction(cc.sequence(bouncesActionArray));
    },

    /**
     * 初始化Tab
     */
    _initTab () {
        for(let i = 0; i < this.tab.length; i++) {
            this.tab[i] = this.tab[i].getComponent('QuestTab')
            this.tab[i].init(this)
        }
        this.tabFade.zIndex = 20
        this.handleTab(null, this.tabIndex)
    },

    /**
     * 初始化每日任务
     */
    _initDaily () {
        if(!this.initDailyDone) {
            this.initDailyDone = true
            this.questDaily = this.questDaily.getComponent('QuestDaily')
            this.questDaily.init(this)
        }
    },

    /**
     * 初始化主线任务
     */
    _initMain () {
        if(!this.initMainDone) {
            this.initMainDone = true
            this.questMain = this.questMain.getComponent('QuestMain')
            this.questMain.init(this)
        }
    },

    /**
     * Tab切换
     * @param {number} tabIndex [1: 每日任务, 0: 完成目标]
     */
    handleTab(event, tabIndex) {
        this.tab.forEach((item, index) => {
            if(tabIndex == index) {
                item.show()
                item.node.zIndex = 30

                this.tabList[index].wrap.active = true
                this.tabList[index].init.apply(this)
            } else {
                item.hide()
                item.node.zIndex = 10

                this.tabList[index].wrap.active = false
            }
        })
    },

    /**
     * 更新全局用户数据
     * @param {number} coin 金币
     * @param {number} diamond 钻石
     * @param {number} ticket 艺人券
     */
    updateUserInfo (coin, diamond, ticket) {
        GameModule.userInfo.updateUserRes({
            gold: coin,
            diamond: diamond,
            ticket: ticket
        })
    },

    /**
     * 显示领取活跃度奖励动画
     * @param {object} giftObj {ticket: 艺人券, diamond: 钻石, coin: 金币}
     */
    showActGift (giftObj) {
        let giftMap = {
            diamond: {
                showText: '钻石',
                count: giftObj.diamond
            },
            coin: {
                showText: '金币',
                count: giftObj.coin
            },
            ticket: {
                showText: '艺人券',
                count: giftObj.ticket
            },
        }

        let showText, showType
        for(let i in giftMap) {
            if(giftMap[i].count > 0) {
                showText = `${giftMap[i].showText} x ${giftMap[i].count}`
                showType = i
            }
        }

        this.mask.zIndex = 12
        this.actGift.active = true
        this.giftMap.forEach((value, index) => {
            if(showType == value) {
                cc.find('/gift_sprite', this.actGift).getComponent('cc.Sprite').spriteFrame = this.actGiftFrames[index]
                cc.find('/gift_label', this.actGift).getComponent('cc.Label').string = showText
            }
        })
    },

    /**
     * 显示领取奖励动画
     * @param {cc.SpriteFrame} showFrame 图片素材
     * @param {string} showText 显示文案
     */
    showMainGift (showFrame, showText) {
        // GameModule.audioMng.playGift()
        this.mask.zIndex = 12
        this.actGift.active = true
        cc.find('/gift_sprite', this.actGift).getComponent('cc.Sprite').spriteFrame = showFrame;
        cc.find('/gift_label', this.actGift).getComponent('cc.Label').string = showText;
    },

    hideActGift () {
        this.mask.zIndex = 10
        this.actGift.active = false
    },

    close() {
        this.node.destroy();
    }
});
