import { __BuyPandoraData } from "../../data/sdata/SManage";

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

interface PlayerPandora {
    count: number
    data: Map<string, number>
}

const descList = {
    1001: "<color=#FFFFFF>包含1件</c>普通、<color=#3DFF00>精良</color>、<color=#0fffff>稀有</color>的物品",
    1002: "<color=#FFFFFF>包含1件</c><color=#0fffff>稀有</color>、<color=#E62CFF>史诗</color>、<color=#FFE000>传说</color>的物品",
    1003: "<color=#FFFFFF>包含10件</c><color=#0fffff>稀有</color>、<color=#E62CFF>史诗</color>、<color=#FFE000>传说</color>的物品"
}

@ccclass
export default class ShopPandoraItem extends cc.Component {

    @property(cc.RichText)
    lbdesc: cc.RichText = null;

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

    @property(cc.RichText)
    lbtext: cc.RichText = null;

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

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

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

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

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

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

    shopId: number = 0;
    videoMap: Map<string, number> = new Map;
    drawMap: Map<string, number> = new Map;
    pandoraDrawCount: number = 0;
    needAmethyst: number = 0;

    public callback: (item: ShopPandoraItem) => void

    public _buyPandoraData: __BuyPandoraData
    public init(_buyPandoraData: __BuyPandoraData, useData: PlayerPandora) {
        this._buyPandoraData = _buyPandoraData;
        this.shopId = _buyPandoraData.id;
        this.lbdesc.string = descList[_buyPandoraData.id];
        this.lbname.string = _buyPandoraData.name;
        if (_buyPandoraData.adCount > 0) {
            let use = !useData.data[_buyPandoraData.id] ? 0 : useData.data[_buyPandoraData.id];
            if (_buyPandoraData.adCount - use > 0) {
                this.videoMap[String(_buyPandoraData.id)] = `${_buyPandoraData.adCount - use}`;
                this.videoNum.string = `${_buyPandoraData.adCount - use}`;
                this.videoButtom.active = true;
                this.diamondButtom.active = false;
            } else {
                this.videoButtom.active = false;
                this.diamondButtom.active = true;
                this.diamondNum.string = `${_buyPandoraData.goodId}`;
            }
        } else {
            this.videoButtom.active = false;
            this.diamondButtom.active = true;
            this.diamondNum.string = `${_buyPandoraData.goodId}`;
        }
        this.lbtime.node.active = _buyPandoraData.flushTime > 0;
        this.initIcon(_buyPandoraData.id);
        this.initText(_buyPandoraData.id, useData);
    }

    initText(id: number, useData: PlayerPandora) {
        if (id == 1001) {
            this.lbtext.node.active = false;
            this.pandoraDrawCount = 1;
        } else if (id == 1002) {
            this.pandoraDrawCount = 1;
            let count = !useData.count ? 0 : useData.count;
            this.drawMap[id] = 10 - count;
            this.lbtext.string = `<color=#0fffff>${10 - count}次内</c><color=#ffffff>必得<color=#ffff00>传说<color=#ffffff>物品</color>`;
        } else if (id == 1003) {
            this.pandoraDrawCount = 10;
            this.lbtext.string = `<color=#ffffff>必得<color=#ffff00>传说<color=#ffffff>物品</color>`;
        }
        this.needAmethyst = this._buyPandoraData.goodId;
    }

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

    public refresh(id: number) {
        if (this.videoMap[id]) {
            this.videoMap[id]--;
            if (this.videoMap[id] == 0) {
                this.videoButtom.active = false;
                this.diamondButtom.active = true;
                this.diamondNum.string = `${this._buyPandoraData.goodId}`;
            } else {
                this.videoNum.string = this.videoMap[id];
            }
        }
    }

    public refreshDraw(id: number) {
        if (this.drawMap[id]) {
            this.drawMap[id]--;
            if (this.drawMap[id] == 0) {
                this.drawMap[id] = 10;
            }
            this.lbtext.string = `<color=#0fffff>${this.drawMap[id]}次内</c><color=#ffffff>必得<color=#ffff00>传说<color=#ffffff>物品</color>`;
        }
    }

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

}
