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

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

@ccclass
export default class ShopCoinItem 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: ShopCoinItem) => void

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

    public initIcon(id: number) {
        cc.resources.load('icon/shop/money/' + 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: ShopCoinItem) => void) {
        this.callback = callback
    }
    public onclick() {
        this.callback(this)
    }


}
