import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import ShopView from "./ShopView";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ExchangeAmethystView extends ViewObject {

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

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

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

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

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

    count: number = 0;

    callBack: Function = null;

    init(pandoraDrawCount: number, needAmethyst: number, callBack: Function) {
        this.callBack = callBack;
        let amethystCount = this.main.player.getGoodCount(1009);
        this.count = needAmethyst - amethystCount;
        this.lbdrawcount.string = `抽奖${pandoraDrawCount}次`;
        this.lbneed.string = `${needAmethyst}`;
        this.lbhave.string = `${amethystCount}`;
        this.diamond_1.string = `(可消耗${this.count * 10}钻石购买)`;
        this.diamond_2.string = `${this.count * 10}`;
    }

    buy() {
        let msg = {
            count: this.count
        };
        this.main.gameHttp.sendJson('pandora/v1/buyZsj', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    console.log("==reve=exchangeamethyst===", reve)
                    // this.main.showReward(reve);
                    this.callBack && this.callBack();
                    this.exitDistroy();
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }

    onClick() {
        this.buy();
    }

}