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

const itemInfo = require('../data/item');

cc.Class({
    extends: cc.Component,

    properties: {
        isHasCard: {
            get: function() {
                if (!this._isHasCard) {
                    this._isHasCard = false;
                }
                return this._isHasCard;
            },
            set: function(value) {
                this._isHasCard = value;
                if (this._isHasCard) {
                    this.cardInfoNode.active = true;
                    this.noCardNode.active = false;
                    this.cardLight.runAction(cc.rotateBy(5,360).repeatForever());
                } else {
                    this.cardInfoNode.active = false;
                    this.noCardNode.active = true;
                    this.cardLight.stopAllActions();
                }
            }
        },

        noCardNode: cc.Node,

        cardInfoNode: cc.Node,
        timeNode: cc.Node,
        timeLabel: cc.Label,
        cardLight: cc.Node,
        cardSprite: cc.Sprite,
        cardTitleLabel: cc.Label,
        cardDescRichText: cc.RichText,

        unloadButton: cc.Button,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    update (dt) {
        if (this.cardInfo && this.isHasCard == true && this.cardInfo.validTime > 0) {
            this.cardInfo.validTime -= (dt * 1000);
            if (this.cardInfo.validTime > 0) {
                this.timeLabel.string = DWTool.calculateTime(this.cardInfo.validTime / 1000);
            } else {
                this.isHasCard = false;
                GameEvent.fire(GameNotificationKey.RefreshInsertCardsInfo,this.cardInfo,false);
            }
        }
    },

    onDisable() {
        this.cardLight.stopAllActions();
    },

    cardOpenPack() {

        if (this.isHasCard === true) {
            return
        }

        GameEvent.fire(GameNotificationKey.OpenPack,false);
    },

    configCardInfo(cardInfo,uid) {
        this.isHasCard = true;
        this.cardInfo = cardInfo;

        if (uid != undefined) {
            this.uid = uid;
        }

        var validTime = '永久';
        if (cardInfo.validTime > 0) {
            this.timeNode.active = true;
            this.timeLabel.string = DWTool.calculateTime(cardInfo.validTime / 1000);
            this.unloadButton.active = false;
        } else {
            this.timeNode.active = false;
            this.unloadButton.active = true;
        }

        this.cardTitleLabel.string = cardInfo.name;
        cc.loader.loadRes('item/'+this.cardInfo.picId, cc.SpriteFrame, (err, spriteFrame) => {
            this.cardSprite.spriteFrame = spriteFrame;
        });

        if (cardInfo.defend) {
            this.cardDescRichText.string = '<color=#238310>+'+ cardInfo.defend + '%</color>防守成功率';
        }
    },

    unloadCard() {
        this.unloadButton.enabled = false;
        this.postUnloadCard();
    },

    postUnloadCard() {
        if (this.cardInfo != undefined && this.cardInfo.id > 0) {
            PackApi.postUnloadCard(this.uid, this.cardInfo.id, (responseData) => {
                // console.log("unload card: " + JSON.stringify(responseData));
                this.unloadCardSuccess();
            }, (error) => {
                console.log('unload card error' + error);
                this.unloadButton.enabled = true;
            });
        } else {
            this.unloadButton.enabled = true;
        }
    },

    unloadCardSuccess() {
        this.unloadButton.enabled = true;
        this.isHasCard = false;
        GameEvent.fire(GameNotificationKey.RefreshInsertCardsInfo,this.cardInfo,false);
        // GameEvent.fire(GameNotificationKey.RefreshPackInfo,this.cardInfo.id,true);
    },
});
