
var reGameStates = require('GameStates');
import gameToast from "../Network/gameToast";
// const { values } = require('../Network/runtime');
cc.Class({
    extends: cc.Component,

    properties: {
        seedType: {
            default: reGameStates.SeedType.Normal,
            type: cc.Enum(reGameStates.SeedType),
        },
        seedName: {
            default: ''
        },
        _EndPosition: {
            default: new cc.Vec2(),
            serializable: false,
            visible: false,
        },
        labelTip: {
            default: null,
            type: cc.Label,
        },
        labelTipString: {
            default: '移动可拆除对应建筑',
        },
        //记录节点
        targetBuildingsInfo: { default: null, visible: false },

        //移除的提示预制
        removeTipPrefab: { default: null, type: cc.Prefab },

        //记录要删除的节点
        selectedTarget: { default: [], type: cc.Node, visible: false },
        //记录选中的index
        selectedTargetIndex: { default: [], type: cc.Integer, visible: false },

        isMoveState: { default: true, visible: false },

        MoveStartNode: cc.Node,
        MoveEndNode: cc.Node,

        //content_seed 传递过来
        goodsSeedInfo: {
            default: null,
            tooltip: '设定一个背包种子信息',
        },
        //content_seed 设置
        seedSprite: {
            default: null,
            type: cc.Sprite,
            tooltip: "当前生出工具的提示"
        },
        fruitSpriteFrame: {
            default: null,
            type: cc.SpriteFrame,
            tooltip: "成熟的图片展示"
        },
        upTarget: {
            default: null,
            type: cc.Node,
            tooltip: "把当前操作的ui按钮传进来"
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

        //获取对应的地图层级
        this.buildLayer = GlobalD.TiledMap._tiledMap.getLayer('BuildsHighway');

        GlobalD.game._ManageUIScript.onBottomMenuView(false);

        var self = this;
        self._MainCamera = GlobalD.game.MainCamera;
        self._tiledMap = GlobalD.TiledMap._tiledMap;
        //获取最后建造公路的层级
        self.HighwayLayer = self._tiledMap.getLayer('Highway');
        self._game = GlobalD.game;

        //初始化时候设置一下位置
        //后面只要点击生成铺路就需要重置一下
        let startTiledPos = GlobalD.TiledMap._tilePosFromLocation(self.node.getPosition());
        let startEndPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(startTiledPos);
        self.node.setPosition(startEndPos);
        self._EndPosition = startTiledPos;

        if (reGameStates.SeedType.Normal === self.seedType) {
            //锤子类型
            let startIndex = GlobalD.TiledMap.getIndex(startTiledPos);
            let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(startIndex);
            if (_targetBuildingsInfo) {
                self.labelTip.string = '播种到' + _targetBuildingsInfo.buildInfo.buildingName + '上';
                self.targetBuildingsInfo = _targetBuildingsInfo;
            }
            else {
                self.labelTip.string = self.labelTipString;
                self.targetBuildingsInfo = null;
            }
        }



        self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
            GlobalD.GameControl._isBuildingMove = true;
            var delta = event.touch.getDelta();
            self._touchPosition.x += delta.x / self._MainCamera.zoomRatio;
            self._touchPosition.y += delta.y / self._MainCamera.zoomRatio;
            let tiledPos = GlobalD.TiledMap._tilePosFromLocation(self._touchPosition);
            // //不相等的时候才绘制
            if (tiledPos.sub(self._EndPosition).mag() != 0) {
                self._EndPosition = tiledPos;
                self._SelectHighwayTiled(tiledPos);

            }
        }, self.node);

        self.node.on(cc.Node.EventType.TOUCH_START, function (event) {
            self.isMoving = true;
            self._touchPosition = self.node.getPosition();
        }, self.node);

        self.node.on(cc.Node.EventType.TOUCH_END, function (event) {
            //起始触摸位置
            GlobalD.GameControl._isBuildingMove = false;
            let buildIndex = GlobalD.TiledMap.getIndex(self._EndPosition);
            let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(buildIndex);
            if (_targetBuildingsInfo) {
                if (_targetBuildingsInfo.buildInfo.buildType == reGameStates.BuildType.HolyFarmland) {
                    self.labelTip.string = '播种到' + _targetBuildingsInfo.buildInfo.buildingName + '上';
                    self.targetBuildingsInfo = _targetBuildingsInfo;
                } else {
                    self.labelTip.string = '请移动到农田上！';
                    self.targetBuildingsInfo = null;
                }

            }
            else {
                self.labelTip.string = self.labelTipString;
                self.targetBuildingsInfo = null;
            }

        }, self.node);
        self.node.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
            //起始触摸位置
            GlobalD.GameControl._isBuildingMove = false;
        }, self.node);


    },
    onCancleSelf() {
        this.node.active = false;

        GlobalD.game._ManageUIScript.onBottomMenuView(true);
    },
    /**
     * 
     * 处理播种逻辑，记录数据库
     * @returns 
     */
    onSowOnTheGround() {
        //调用土地相关
        if (!this.targetBuildingsInfo) return;
        // cc.log('this.targetBuildingsInfo', this.targetBuildingsInfo)
        // this.targetBuildingsInfo.onClearSelfResetFromType();

        // 种植土地的信息
        let _leaseFarmlandInfo = this.targetBuildingsInfo.getComponent("LeaseFarmlandInfo");
        let leaseLandInfo = _leaseFarmlandInfo.leaseLandInfo;

        if (leaseLandInfo == null) {
            GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请先租赁土地！", 2, () => {
                console.log("finish toast!");
            });
            //隐藏种子和显示UI
            this.node.active = false;
            GlobalD.game._ManageUIScript.onBottomMenuView(true);
            return;
        }

        // console.log(leaseLandInfo);

        let data = { landId: leaseLandInfo.configLandId, seedId: this.goodsSeedInfo.id };

        GlobalD.GameData.onPlant(data, (value) => {
            //面板种子消耗减少1  
            if (0 === value.code) {
                console.log('种子减少的类型名字', this.seedName);
                console.log(value);
                // GlobalD.game.MuinusBuilding(this.seedName);
                //刷新本地列表对象，数量减去1
                // console.log(this.upTarget);
                // let NumLabel = this.upTarget.getComponent("Content_Button").NumLabel.getComponent(cc.Label);
                // if (parseInt(NumLabel.string) <= leaseLandInfo.leaseMultiple) {
                //     //这里只隐藏
                //     this.upTarget.active = false;
                // } else {
                //     NumLabel.string = parseInt(NumLabel.string) - leaseLandInfo.leaseMultiple;
                // }
                //
                var BuildingView = cc.find("Canvas/UICamera/BuildingContainer/BuildingView").getComponent("BuildingView");
                BuildingView.onUpdateList();

                //把种子的信息存储到当前的土地 plantInfo 上;
                _leaseFarmlandInfo.setLeaseLandInfo(value.data,true);

                //开始种植，给一个初始的状态
                // this.targetBuildingsInfo.onInitHolyFarmlandSeedFromGrow(1, this.fruitSpriteFrame);

                GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "已成功种植！", 1);
            } else if (706 === value.code) {
                //土地到期，没有更新刷新的处理刷新
                GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
                //重置锁定状态显示
                _leaseFarmlandInfo.onLockLand();
            } else {
                //土地已经种植了
                GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
            }

        });
        //隐藏种子和显示UI
        this.node.active = false;
        GlobalD.game._ManageUIScript.onBottomMenuView(true);
    },

    _SelectHighwayTiled(tiledPos) {

        let endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(tiledPos);
        this.node.setPosition(endPos);

    }


});
