var {GameNotificationKey} = require('../utils/GameEnum');

cc.Class({
    extends: cc.Component,

    properties: {

        backgroundNode: cc.Node,
        backgroundSelectedNode: cc.Node,

        itemNode: cc.Node,
        itemSprite: cc.Sprite,
        countLabel: cc.Label,

        isSelected: {
            get: function() {
                if (!this._isSelected) {
                    this._isSelected = false;
                }
                return this._isSelected;
            },
            set: function(value) {
                this._isSelected = value;
                if (this._isSelected) {
                    this.backgroundSelectedNode.active = true;
                    this.backgroundNode.active = false;
                } else {
                    this.backgroundSelectedNode.active = false;
                    this.backgroundNode.active = true;
                }
            }
        },

        hasItem: {
            get: function() {
                if (!this._hasItem) {
                    this._hasItem = false;
                }
                return this._hasItem;
            },
            set: function(value) {
                this._hasItem = value;
                if (this._hasItem) {
                    this.itemNode.active = true;
                } else {
                    this.itemNode.active = false;
                }
            }
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.winSize = cc.view.getVisibleSize();

        this.setEventListener();
    },

    setEventListener() {
        GameEvent.on(GameNotificationKey.RefreshPackItem, this, () => {
            this.isSelected = false;
        });
    },

    onDisable() {
        this.hasItem = false;
        this.isSelected = false;
        GameEvent.off(GameNotificationKey.RefreshPackItem, this);
    },

    init(index,page,itemInfo) {
        this.itemIndex = index;
        this.page = page;

        if (itemInfo != undefined) {
            this.hasItem = true;
            this.itemInfo = itemInfo;
            this.configData();
        } else {
            this.hasItem = false;
        }
    },

    configData() {
        let itemId = this.itemInfo.itemId;
        let count = this.itemInfo.count;

        cc.loader.loadRes('item/'+this.itemInfo.picId, cc.SpriteFrame, (err, spriteFrame) => {
            this.itemSprite.spriteFrame = spriteFrame;
        });

        this.countLabel.string = count;
    },

    start () {

    },

    // update (dt) {},

    selectItem() {

        if (this.hasItem == false) {
            return;
        }

        if (this.isSelected == true) {
            this.isSelected = false;
            GameEvent.fire(GameNotificationKey.HidePropDesc);
        } else {
            GameEvent.fire(GameNotificationKey.RefreshPackItem);
            this.isSelected = true;

            let position = this.getWorldPosition(this.node);
            GameEvent.fire(GameNotificationKey.ShowPropDesc,position, this.itemInfo);
        }
    },

    getWorldPosition(node) {
        return node.convertToNodeSpaceAR(cc.p((this.winSize.width/2), (this.winSize.height/2)));
    },
});
