/**
 * 购买小狗
 */
cc.Class({
    extends: cc.Component,

    properties: {

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

        //1 小包，2 大包
        toggleInputValue: {
            default: '1',
            visible: false
        },
        totalPriceLabel: {
            default: null,
            type: cc.Label,
            serializable: true
        },
        totalPriceValue: {
            default: 0,
            visible: false
        },

        currentFoodUnitLabel: {
            default: null,
            type: cc.Label,
            serializable: true
        },

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

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

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

        foodList: {
            default: [],
            visible: false
        },
        foodSmall: {
            default: null,
            visible: false
        },
        foodBig: {
            default: null,
            visible: false
        },
        currentFood: {
            default: null,
            visible: false
        },



        parentNode: {
            default: null,
            visible: false
        }
    },

    onLoadFoodList() {
        GlobalD.GameData.onGetMallFoodList((value) => {
            if (0 === value.code) {
                this.foodList = value.data;
                for (let i = 0; i < this.foodList.length; i++) {
                    if (1 === this.foodList[i].id) {
                        this.foodSmall = this.foodList[i];
                        this.currentFood = this.foodSmall;
                        this.currentFoodUnitLabel.string = "X" + this.currentFood.priceSnb;

                    } else if (2 === this.foodList[i].id) {
                        this.foodBig = this.foodList[i];
                    }
                }
                this.toggleInputValue = '1';
                this._updatePrice();
            } else {
                GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
            }
        });

        this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
    },

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

    inputValue(value, e) {

        var numberTemp = new RegExp("^[A-Za-z0-9]+$");
        let limitValue = 100;
        if (numberTemp.test(value)) {
            if (Number(value) >= 1 && Number(value) <= limitValue) {
                this.inputInviteValue = Number(value);
            } else if (Number(value) > limitValue) {
                //限制只能输入100
                this.inputInviteValue = limitValue;
                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() {

        switch (this.toggleInputValue) {
            case "1":
                this.totalPriceValue = this.foodSmall.priceSnb * this.inputInviteValue;
                this.totalPriceLabel.string = "X" + this.totalPriceValue;
                this.currentFood = this.foodSmall;
                break;
            case "2":
                this.totalPriceValue = this.foodBig.priceSnb * this.inputInviteValue;
                this.totalPriceLabel.string = "X" + this.totalPriceValue;
                this.currentFood = this.foodBig;
                break;
            default:
                console.error("this.toggleInputValue 不是1 2", this.toggleInputValue);
                break;
        }

        this.currentFoodUnitLabel.string = "X" + this.currentFood.priceSnb;

    },
    //租赁支付
    onPlaySnbInfo() {
        if (this.currentFood == null) {
            GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "购买狗粮NULL", 1);
            return;
        }
        GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "添加狗粮..", 5);
        let data = { foodId: this.currentFood.id, buyAmount: this.inputInviteValue };
        GlobalD.GameData.onGetAddDogFood(data, (value) => {
            console.log(value);
            if (0 === value.code) {
                GlobalD.GameData.SetSNB(value.data.snb + value.data.snbPart);
                GlobalD.Dog = value.data.dog;
                GlobalD.GameData.hideToast();

                //更新日志数据
                cc.find("GameNode/ManageDapp").getComponent("ManageDapp").onUpdateSnbList();
                //需要赋值
                this.parentNode.getComponent('DogContainer')._updateDogState(GlobalD.Dog);
            } else {
                GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
            }
        });
    },

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

});
