import gameToast from "../Network/gameToast"
cc.Class({
    extends: cc.Component,

    properties: {
        grantName: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        amount: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        //介绍
        describe: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        inputAddressValue: {
            default: "",
            visible: false,
            serializable: false
        },

        inputAmountValue: {
            default: 0,
            visible: false,
            serializable: false
        },
        inputAmountContainer: {
            default: null,
            type: cc.Node,
            tooltip: "输入果实数量的节点"
        },

        //当前操作的果实信息
        fruitInfo: {
            default: null,
            serializable: true,
            visible: false
        },
        upTarget: {
            default: null,
            type: cc.Node,
            tooltip: "把当前操作的ui按钮传进来"
        },
       
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    // start() {

    // },

    /**
     * 赠送
     */
    // onGrant() {

    // },

    setInfo(grantName, amount) {
        this.grantName.string = grantName;
        this.amount.string = amount;
    },

    onInputAddressValue(value, e) {
        this.inputAddressValue = value;
    },

    onInputAmountValue(value, e) {
        var numberTemp = new RegExp("^[A-Za-z0-9]+$");
        if (numberTemp.test(value)) {
            if (Number(value) >= 1) {
                this.inputAmountValue = Number(value);
            } else {
                this.inputAmountValue = 0;
                this.inputAmountContainer.getComponent(cc.EditBox).string = this.inputAmountValue;
            }
        } else {
            this.inputAmountValue = 0;
            this.inputAmountContainer.getComponent(cc.EditBox).string = this.inputAmountValue;
            console.log("请输入整数的倍数", this.inputAmountValue);
        }
    },

    onclose() {
        this.node.destroy();
    },

    //确定增送果实
    onLeaseGrantFruit() {
        //todo 判断一下amount
        console.log(this.inputAddressValue);
        if (this.inputAddressValue.length < 20) {
            GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), "请输入地址！", 1);
            return;
        }
        if (Number(this.inputAmountValue) <= 0) {
            GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), "请输入赠送的果实数量！", 1);
            return;
        }
        if (Number(this.inputAmountValue) > Number(this.amount.string)) {
            GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), "输入赠送的果实数量过多！", 1);
            return;
        }

        if (GlobalD.GameData.isOnGrantFruit) return;
        GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), "赠送果实中...", 5);
        //赠送一部分果实
        GlobalD.GameData.onGrantFruit({ fruitId: this.fruitInfo.id, amount: this.inputAmountValue, address: this.inputAddressValue }, (res, value) => {
            // console.log(value);
            if (value.code === 0) {
                // let NumLabel = this.upTarget.getComponent("Content_Button").NumLabel.getComponent(cc.Label);
                //这里只隐藏
                // this.upTarget.active = false;
                // NumLabel.string = 0;

                var BuildingView = cc.find("Canvas/UICamera/BuildingContainer/BuildingView").getComponent("BuildingView");
                BuildingView.onUpdateList();

                this.amount.string =  Number(this.amount.string) - this.inputAmountValue;

                GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), value.msg, 1);
            } else {
                GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), value.msg, 1);
            }
        });
    }
});
