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

    properties: {

        inputInviteValue: {
            default: 1,
            visible: false
        },

        //1 对应3个月，2对应1年，3对应5年
        toggleInputValue: {
            default: '2',
            visible: false
        },
        totalPriceLabel: {
            default: null,
            type: cc.Label,
            serializable: true
        },
        totalPriceValue: {
            default: 0,
            visible: false
        },

        ToastParent: {
            default: null,
            type: cc.Node,
            serializable: true
        },
        //输入节点
        inputContainer: {
            default: null,
            type: cc.Node,
            serializable: true
        },

        toggle1: {
            default: null,
            type: cc.Toggle,
            serializable: true
        },

        toggle2: {
            default: null,
            type: cc.Toggle,
            serializable: true
        },

        toggle3: {
            default: null,
            type: cc.Toggle,
            serializable: true
        },

        //这个显示的时候，拦截输入
        blockInput: {
            default: null,
            type: cc.Node,
            serializable: true
        },
        leaseFarmlandInfoNode: {
            default: null,
            type: cc.Node,
            serializable: true,
            visible: false,
            toolTip: "操作的土地对象,初始化时候传入"
        },
        //轮询土地状态
        AutoGetLandData: null,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    // start() {

    // },

    onOpenInfo() {
        this.node.active = true;

    },

    //初始化时候需要初始化一次默认值
    onToggleInput(value, evnentData) {
        // console.log(value.isChecked, evnentData);
        this.toggleInputValue = evnentData;
        this._updatePrice();
    },

    inputLeaseValue(value, e) {

        var numberTemp = new RegExp("^[A-Za-z0-9]+$");
        if (numberTemp.test(value)) {
            if (Number(value) >= 1 && Number(value) <= 100) {
                this.inputInviteValue = Number(value);
            } else if (Number(value) > 100) {
                //限制只能输入100
                this.inputInviteValue = 100;
                this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
            } else {
                this.inputInviteValue = 1;
                this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
            }
        } else {
            this.inputInviteValue = 1;
            this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
            console.log("请输入整数的倍数", this.inputInviteValue);
        }
        this._updatePrice();
    },

    _updatePrice() {
        //this.inputInviteValue 默认是1倍
        // console.log( this.inputInviteValue);
        switch (this.toggleInputValue) {
            case "1":
                this.totalPriceValue = 700 * this.inputInviteValue;
                this.totalPriceLabel.string = this.totalPriceValue + "CNT";
                break;
            case "2":
                this.totalPriceValue = 2500 * this.inputInviteValue;
                this.totalPriceLabel.string = this.totalPriceValue + "CNT";
                break;
            case "3":
                this.totalPriceValue = 5000 * this.inputInviteValue;
                this.totalPriceLabel.string = this.totalPriceValue + "CNT";
                break;
            default:
                console.error("this.toggleInputValue 不是1 2 3！", this.toggleInputValue);
                break;
        }

    },

    /**
     * 显示面板时候，设置一下初始化数据
     */
    onInitLeaseInfo(multiple) {
        //这里处理倍数
        this.inputInviteValue = multiple;
        this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
        this._updatePrice();
    },

    //租赁支付
    onLeaseInfo() {
        console.log('支付总额：' + this.totalPriceValue);
        if (!GlobalD.dapp) {
            console.error("onBindInviteInfo warn,dapp未初始化！");
            return;
        }
        let _self = this;
        //cntAmount,payType,itemType,callback
        let _configLandInfo = this.leaseFarmlandInfoNode.getComponent("LeaseFarmlandInfo").configLandInfo

        //租赁的itemType 要处理一下
        let _itemType = this.toggleInputValue + "or" + _configLandInfo.id;

        GlobalD.GameData.payCNT(this.totalPriceValue, 1, _itemType, (data) => {
            console.log("土地支付:", this.totalPriceValue, "== playType=", 1, ",item_type==", _itemType);
            console.log("支付data:", data);
            const [err, tx] = data;

            if (err === null) {
                // TODO 成功, 支付为链上操作，需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
                console.log(tx) // 交易hash，唯一标识符

                //todo 如果是土地租赁，轮询土地状态 
                if (this.AutoGetLandData) {
                    this.unschedule(this.AutoGetLandData);
                    this.AutoGetLandData = null;
                }
                this.AutoGetLandData = () => {
                    GlobalD.GameData.getLandState({
                        landId: _configLandInfo.id,
                        callback: (res, value) => {
                            console.log("轮询土地数据:" + JSON.stringify(value.msg));
                            if (0 === value.code) {
                                this.unschedule(this.AutoGetLandData);
                                console.log("轮询到土地购买成功！");
                                GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "土地购买成功！", 5);
                                //解锁土地操作
                            
                                let _landData = value.data;
                                this.leaseFarmlandInfoNode.getComponent("LeaseFarmlandInfo").setLeaseLandInfo(_landData);
                               

                                this.onClose();
                            }
                        }
                    });
                };
                this.schedule(this.AutoGetLandData, 5);
            } else {
                console.log(err)
            }
        });
        // GlobalD.dapp.bindParent(_self.inputInviteValue).then((data) => {
        //     if (data.err === null) {
        //         // 返回成功，见下面消息体
        //         console.log(data.res)
        //         //绑定成功时候，隐藏输入框，设置父节点
        //         GlobalD.Dapp.UserInfo.parent = data.res;
        //         _self.parentInviteValue.string = GlobalD.Dapp.UserInfo.parent.invite_code;
        //         //如果存在父节点，隐藏输入框
        //         _self.inputContainer.active = false;


        //     } else {
        //         //TODO 服务错误
        //         console.log(data.err)
        //     }
        // })
    },

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


});
