import date from "../Unit/date.js"
import gameToast from "../Network/gameToast"
/**
 * 操作租赁土地的相关业务
 */
cc.Class({
    extends: cc.Component,

    properties: {
        leasePanelPrefabs: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },

        /**
         * 显示天数部分
         */
        upSliderNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        multipleLabel: {
            default: null,
            type: cc.Label,
            serializable: true,
            toolTip: "显示的倍数"
        },
        leaseDate: {
            default: null,
            type: cc.Label,
            serializable: true,
            toolTip: "剩余天数/总天数"
        },


        /**
         * 成熟期部分
         */
        midSliderNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        midDate: {
            default: null,
            type: cc.Label,
            serializable: true,
            toolTip: "剩余天数/总天数"
        },

        midSliderProgressNode: {
            default: null,
            type: cc.Node,
            serializable: true,
            toolTip: "进度条"
        },


        notLeased: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

        // configLandId: 1
        // createTime: "2022-01-10 21:25:30"
        // id: 1
        // isLease: 1
        // isPlant: 0
        // landDescribe: "土地1xxx"
        // leaseMultiple: 1
        // leaseTime: "2022-01-10 21:25:17"
        // name: "土地1"
        // plantMature: 0
        // plantStart: "2022-01-10 21:25:22"
        // updateTime: "2022-01-10 21:25:33"
        // userId: "4"
        // 已经租赁的土地信息
        leaseLandInfo: {
            default: null,
            visible: false
        },

        // createTime: "2022-01-10 21:25:30"
        // id: 4
        // initMultiple: 1
        // isInit: 1
        // isLease: 0
        // isPlant: 0
        // landDescribe: "一倍土地"
        // leaseMultiple: 1
        // leaseTime: "2022-01-10 21:25:17"
        // name: "土地4"
        // plantMature: 0
        // plantStart: "2022-01-10 21:25:22"
        // posX: 19
        // posY: 10
        // sizeX: 2
        // sizeY: 2
        // updateTime: "2022-01-10 21:25:33"
        // 未初始化时候的土地信息
        configLandInfo: {
            default: null,
            visible: false
        },

        showLandInfo: {
            default: null,
            type: cc.Node,
            serializable: true,
            toolTip: "显示土地信息"
        },
        landInfoPrefabs: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },

        // 已经种植的作物信息
        plantInfo: {
            default: null,
            visible: false
        },
        showPlantInfo: {
            default: null,
            type: cc.Node,
            serializable: true,
            toolTip: "显示种植信息"
        },
        plantInfoPrefabs: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },

        //收获阶段

        harvestNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

        isHarvest: false
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    onSpawnLeasePanel() {

        let leasePanel = cc.instantiate(this.leasePanelPrefabs);
        let parent = cc.find('Canvas/UICamera/DAPPContainer');
        leasePanel.parent = parent;
        leasePanel.setPosition(0, 0);
        // leasePanel.zIndex = 999;

        //把操作对象传入
        let leasePanelScript = leasePanel.getComponent("LeaseInfo");
        leasePanelScript.leaseFarmlandInfoNode = this;
        /**
         * 初始化两个操作。一个是续约，续约的话，不给操作，只有续约按钮可点，相当于原来配置再购买一次
         * 
         * this.leaseLandInfo 如果购买了土地，这个参数不为空
         */

        if (this.leaseLandInfo) {
            //续约时候拦截输入，固定倍数 
            leasePanelScript.blockInput.active = true;
            //传入当前计算倍数
            leasePanelScript.onInitLeaseInfo(this.leaseLandInfo.leaseMultiple);
        } else if (0 === this.configLandInfo.initMultiple) {
            // 固定土地开启，限制输入，设置默认值是1
            leasePanelScript.blockInput.active = true;
            leasePanelScript.onInitLeaseInfo(1);
        } else {
            //多倍土地，可以自己输入
            leasePanelScript.onInitLeaseInfo(this.configLandInfo.initMultiple);
        }


    },

    /**
     * 解锁土地操作
     */
    onUnlockLand() {
        //直接删除此节点
        if (this.notLeased) {
            this.notLeased.destroy();
            this.notLeased = null;
        }

    },
    //设置config的土地信息
    setConfigLandInfo(value) {
        this.configLandInfo = value;
    },
    //设置已租赁参数
    setLeaseLandInfo(value) {
        this.leaseLandInfo = value;
        //更新一下土地信息
        this.updateLandState();
    },

    updateLandState() {
        if (!this.leaseLandInfo) return;
        // console.log("this.leaseLandInfo.1", this.leaseLandInfo)
        if (1 === this.leaseLandInfo.isLease) {
            //已解锁
            //显示剩余天数
            this.upSliderNode.parent.active = true;
            this.multipleLabel.string = this.leaseLandInfo.leaseMultiple;
            //计算天数
            let allDay = date.datedifference(this.leaseLandInfo.createTime, this.leaseLandInfo.leaseTime);
            //租赁土地的剩余天数
            let [_remainingLandDay, _remainingLandHour] = date.remainDayAndHour(this.leaseLandInfo.leaseTime, date.formatTime(new Date()));
            //todo 处理了

            this.leaseDate.string = "剩余" + _remainingLandDay + "天数,共" + allDay + "天";
            let sliderProgressScript = this.upSliderNode.getComponent("slider_progress");
            sliderProgressScript.onSetProcgress(_remainingLandDay / allDay);
            //todo 到期处理土地回收

            //删除解锁图标
            this.onUnlockLand();

            /**
             * 绑定生成显示信息面板，现在土地信息
             */

            this.showLandInfo.on(cc.Node.EventType.TOUCH_START, () => {
                let leasePanel = cc.instantiate(this.landInfoPrefabs);
                let parent = cc.find('Canvas/UICamera/DAPPContainer');
                leasePanel.parent = parent;
                leasePanel.setPosition(0, 0);
                // leasePanel.zIndex = 999;
                let leasePanelScript = leasePanel.getComponent("LandInfo");
                //price,date,start,end,multiple,describe
                let _date = date.datedifference(this.leaseLandInfo.createTime, this.leaseLandInfo.leaseTime);
                let { rentalExpenses, createTime, leaseTime, leaseMultiple, landDescribe } = this.leaseLandInfo;
                leasePanelScript.setInfo(rentalExpenses, _date + "天", createTime, leaseTime, leaseMultiple + "倍", landDescribe);
            })

            // console.log("this.leaseLandInfo.id", this.leaseLandInfo, " ", this.leaseLandInfo.seedInfo)
            if (this.leaseLandInfo.seedInfo) {

                this.midSliderNode.active = true;
                let _currentDay = date.dateAddDate(this.leaseLandInfo.plantStart, this.leaseLandInfo.seedInfo.maturity)
                let [_remainingDay, _remainingHour] = date.remainDayAndHour(_currentDay, date.formatTime(new Date()));

                let _hour = _remainingHour - parseInt(_remainingHour / 24) * 24;

                this.midDate.string = "剩余" + _remainingDay + "天" + _hour + "小时";
                let _maturityHour = this.leaseLandInfo.seedInfo.maturity * 24
                //计算剩余时间显示
                this.midSliderProgressNode.getComponent(cc.ProgressBar).progress = _remainingHour / _maturityHour;

                /**
                  * 绑定生成显示信息面板，种植信息
                  */
                this.showPlantInfo.on(cc.Node.EventType.TOUCH_START, () => {
                    let leasePanel = cc.instantiate(this.plantInfoPrefabs);
                    let parent = cc.find('Canvas/UICamera/DAPPContainer');
                    leasePanel.parent = parent;
                    leasePanel.setPosition(0, 0);
                    // leasePanel.zIndex = 999;
                    let leasePanelScript = leasePanel.getComponent("PlantInfo");
                    let _harvest = 0;
                    if (this.leaseLandInfo.leaseDate === 1) {
                        _harvest = this.leaseLandInfo.seedInfo.harvest1;
                    } else if (this.leaseLandInfo.leaseDate === 2) {
                        _harvest = this.leaseLandInfo.seedInfo.harvest2;
                    } else if (this.leaseLandInfo.leaseDate === 3) {
                        _harvest = this.leaseLandInfo.seedInfo.harvest3;
                    }
                    //price,maturity,plantStart,harvestQuantity,describe
                    let { priceCnt, maturity, seedDescribe } = this.leaseLandInfo.seedInfo;
                    leasePanelScript.setInfo(priceCnt, maturity + "天", this.leaseLandInfo.plantStart, _harvest, seedDescribe);
                })
            }


        } else {

        }
    },




    /**
     * 点击之后，重置土地信息， 并且收入进仓库果实列表中去
     */
    onShowHarvest() {
        this.harvestNode.active = true;

        this.harvestNode.on(cc.Node.EventType.TOUCH_START, () => {
            console.log("点击收获");
            //限制点击多次
            if (this.isHarvest) return;
            this.isHarvest = true;

            let data = { landId: this.leaseLandInfo.configLandId };
            //果实收入仓库，重置土地种植信息
            GlobalD.GameData.onAddFruit(data, (res, value) => {
                // console.log(value);
                if (value.code === 0) {
                    //收成后处理相关状态
                    this.harvestNode.active = false;
                    this.midSliderNode.active = false
                    let _workingBuilding = this.node.getComponent("WorkingBuilding");
                    _workingBuilding.onSetGrow(0, null);
                    //更新仓库列表
                    let BuildingView = cc.find("Canvas/UICamera/BuildingContainer/BuildingView").getComponent("BuildingView");
                    BuildingView.onUpdateList();
                    // "收获成功！"
                    GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "收获成功！", 2, () => {
                        console.log("finish toast!");
                    });
                } else {
                    GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 2, () => {
                        console.log("finish toast!");
                    });
                }
                //处理信息后开放
                this.isHarvest = false;
            });
        })


    },

    onPayCnt() {

    }
});
