var TalentApi = require('../net/TalentApi');
const wechat = require('../net/WeChat');
const inviteMission = require('../data/inviteReward');
const {GameNotificationKey} = require('../utils/GameEnum');
const GameModule = require("../utils/GameModule");
const ShareAction = require('../utils/ShareAction');

var InvitedItem = require("TalentInvitedItem");

cc.Class({
    extends: cc.Component,

    properties: {

        contentNode: cc.Node,

        invitedPrefab: cc.Prefab,
        invitedScrollViewNode: cc.ScrollView,

        listScrollViewNode: cc.ScrollView,
        missionPrefab: cc.Prefab,

        invitedItemArray: [InvitedItem],

        //
        actGift: {
            tooltip: '活跃度弹出层',
            default: null,
            type: cc.Node
        },
        actGiftFrames: {
            tooltip: '奖品大图',
            default: [],
            type: [cc.SpriteFrame]
        },

        actGiftSprite: cc.Sprite,
        actGiftLabel: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

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

        for (var i = 0; i < 10; i++) {
            let item = cc.instantiate(this.invitedPrefab);
            item = item.getComponent('TalentInvitedItem');
            item.node.parent = this.invitedScrollViewNode.content;
            this.invitedItemArray.push(item);
        }

        GameEvent.on(GameNotificationKey.TalentGainAward, this, (model) => {
            this.gainActGift(model);
        });
    },

    start () {

    },

    onDisable() {
        for (let child of this.listScrollViewNode.content.children) {
            child.destroy();
        }
    },

    // update (dt) {},
    init() {
        this.node.parent = cc.find('Canvas');
        this.node.active = true;
        this.node.setContentSize(cc.view.getVisibleSize());

        this.contentNode.y = -cc.view.getVisibleSize().height;
        this.contentNode.runAction(cc.moveTo(0.2, 0, 0).easing(cc.easeCubicActionOut()));

        // this.layout();
        this.getNetworkData();
    },

    closeNode() {
        let finish = cc.callFunc(() => {
            this.node.active = false;
        }, this);
        this.contentNode.runAction(cc.sequence(cc.moveTo(0.2, 0, -cc.view.getVisibleSize().height).easing(cc.easeCubicActionIn()), finish));
    },

    getNetworkData() {
        TalentApi.getTalentMissionList((responseData) => {
            // console.log("responseData: " + JSON.stringify(responseData));
            this.loadData(responseData);
        }, (error) => {
            console.log('talentmission error' + error);
        });
    },

    loadData(responseData) {
        this.userHeadInfoList = responseData.userHeadInfoList;
        this.inviteRewardRecordVoList = responseData.inviteRewardRecordVoList;
        this.invitedCount = responseData.invited;

        this.layoutInvitedUser();
        this.layoutMission();
    },

    layoutInvitedUser() {
        for (var i = 0; i < this.userHeadInfoList.length; i++) {
            let item = this.invitedItemArray[i];
            let itemModel = this.userHeadInfoList[i];
            item.configData(itemModel);
        }
    },

    layoutMission() {
        for (var i = 0; i < this.inviteRewardRecordVoList.length; i++) {
            let itemModel = this.inviteRewardRecordVoList[i];

            for (let mission of inviteMission) {
                if (mission.id == itemModel.rid) {
                    let item = cc.instantiate(this.missionPrefab);
                    item = item.getComponent('TalentInviteMissionItem');
                    item.node.parent = this.listScrollViewNode.content;
                    item.node.width = this.listScrollViewNode.node._contentSize.width;
                    mission.state = itemModel.state;
                    item.configData(mission,this.invitedCount);
                    break;
                }
            }
        }
    },

    inviteArtist() {
        wechat.inviteFriend(ShareAction.BECOME_ARTIST);
    },

    /**
     * 显示领取活跃度奖励动画
     * @param {string} type {coin: 金币, diamond: 钻石, ticket: 艺人券,}
     */
    showActGift (type, showText) {
        this.actGift.active = true;
        this.giftMap.forEach((value, index) => {
            if(type == value) {
                this.actGiftSprite.spriteFrame = this.actGiftFrames[index];
                if (showText) {
                    this.actGiftLabel.node.active = true;
                    this.actGiftLabel.string = showText;
                } else {
                    this.actGiftLabel.node.active = false;
                }
            }
        })
    },

    hideActGift () {
        this.showArray.shift();
        if (this.showArray.length > 0) {
            let itemModel = this.showArray[0];
            this.showActGift(itemModel.type, itemModel.count);
        } else {
            this.actGift.active = false;
        }
    },

    gainActGift(model) {
        this.showArray = [];

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

        if (coin > 0) {
            let type = {'type' : 'coin', 'count' : `金币 ${coin}`};
            this.showArray.push(type);
        }

        if (diamond > 0) {
            let type = {'type' : 'diamond', 'count' : `钻石 ${diamond}`};
            this.showArray.push(type);
        }

        if (ticket > 0) {
            let type = {'type' : 'ticket', 'count' : `艺人券 ${ticket}`};
            this.showArray.push(type);
        }

        if (this.showArray.length > 0) {
            let itemModel = this.showArray[0];
            this.showActGift(itemModel.type, itemModel.count);
        }

        GameModule.userInfo.grossIncome += parseInt(coin);
        GameModule.userInfo.diamond += parseInt(diamond);
    },
});
