import { __BuyPowerData, __PayData } from "../../data/sdata/SManage";

/**
 * 充值节点
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class ShopPowerItem extends cc.Component {

    @property(cc.Label)
    addNum: cc.Label = null;

    @property(cc.Label)
    diamondNum: cc.Label = null;

    @property(cc.Node)
    diamond: cc.Node = null;

    @property(cc.Node)
    video: cc.Node = null;

    @property(cc.Label)
    maxCount: cc.Label = null;

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(cc.Node)
    lbtime: cc.Node = null;

    shopId: number = 0;
    videoId: number = 0;
    videoMap: Map<string, number> = new Map();

    public callback: (item: ShopPowerItem) => void

    public _buyPowerData: __BuyPowerData
    public init(_buyPowerData: __BuyPowerData, useData: Map<string, number>) {
        this._buyPowerData = _buyPowerData
        this.shopId = _buyPowerData.id;
        this.addNum.string = `+${_buyPowerData.power}`;
        this.video.active = _buyPowerData.gold == 0;
        this.diamond.active = _buyPowerData.gold > 0;
        this.diamondNum.string = _buyPowerData.gold == 0 ? "免费" : _buyPowerData.gold + "";
        if (_buyPowerData.maxCount < 0) {
            this.maxCount.node.active = false;
        } else {
            let useNum = !useData[this.shopId] ? 0 : useData[this.shopId];
            this.maxCount.string = `剩余次数${_buyPowerData.maxCount - useNum}`;
        }
        if (_buyPowerData.gold == 0) {
            let useNum = !useData[this.shopId] ? 0 : useData[this.shopId];
            this.videoMap[this.shopId] = _buyPowerData.maxCount - useNum;
            this.videoId = this.shopId;
        }
        this.initIcon(_buyPowerData.id);
    }

    public initIcon(id: number) {
        cc.resources.load('icon/shop/vigor/' + id, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
            if (err) {
                cc.error(err);
            } else {
                this.icon.spriteFrame = spriteFrame;
            }
        });
    }

    public refresh(id: number) {
        this.videoMap[id]--;
        this.maxCount.string = `剩余次数${this.videoMap[id]}`;
    }

    public setCallback(callback: (item: ShopPowerItem) => void) {
        this.callback = callback
    }
    public onclick() {
        this.callback(this)
    }


}
