
var ClearType = cc.Enum({
    Hammer: -1,
    Shovel: -1,
});

cc.Class({
    extends: cc.Component,

    properties: {
        clearType: {
            default: ClearType.Hammer,
            type: cc.Enum(ClearType),
        },

        _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,
    },

    // 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 (ClearType.Hammer === self.clearType) {
            //锤子类型
            let startIndex = GlobalD.TiledMap.getIndex(startTiledPos);
            let buildingsTiledMapStart = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(startIndex);
            if (buildingsTiledMapStart) {
                self.labelTip.string = '是否移除' + buildingsTiledMapStart.buildInfo.buildingName;
                self.targetBuildingsInfo = buildingsTiledMapStart;
            }
            else {
                self.labelTip.string = self.labelTipString;
                self.targetBuildingsInfo = null;
            }
        } else if (ClearType.Shovel === self.clearType) {
            //铲子类型
        }



        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) {
                // cc.log('tiledPos',tiledPos);
                self._EndPosition = tiledPos;
                self._SelectHighwayTiled(tiledPos);

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

                // if (self.isMoveState) return;

                // if (ClearType.Shovel === self.clearType) {
                //     let buildIndex = GlobalD.TiledMap.getIndex(tiledPos);
                //     //铲子类型
                //     if (GlobalD.game.onGetHighwayFromIndex(buildIndex)) {

                //         let tempNode = self.HighwayLayer.node.children;
                //         let length = tempNode.length;
                //         for (let i = length - 1; i >= 0; i--) {
                //             let tiledTile = tempNode[i].getComponent('TiledTile');
                //             let tiledVector2 = new cc.Vec2(tiledTile.x, tiledTile.y);
                //             let tiledIndex = GlobalD.TiledMap.getIndex(tiledVector2);

                //             if (tiledIndex === buildIndex) {

                //                 let highwayTemp = cc.instantiate(self.removeTipPrefab);
                //                 highwayTemp.parent = self.buildLayer.node;
                //                 let tiledTile = highwayTemp.addComponent('TiledTile');
                //                 tiledTile.x = tiledVector2.x;
                //                 tiledTile.y = tiledVector2.y;

                //                 //记录要删除的节点
                //                 self.selectedTarget.push(tempNode[i]);
                //                 return true;
                //             }

                //         }
                //     }
                // }
            }
        }, 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;

            //不是锤子类型，返回
            if (ClearType.Hammer !== self.clearType) return;

            // cc.log(GlobalD.game.buildingsTiledMapUnit);
            let buildIndex = GlobalD.TiledMap.getIndex(self._EndPosition);
            let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(buildIndex);
            // cc.log(buildIndex, _targetBuildingsInfo);

            if (_targetBuildingsInfo) {
                self.labelTip.string = '是否移除' + _targetBuildingsInfo.buildInfo.buildingName;
                self.targetBuildingsInfo = _targetBuildingsInfo;
            }
            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);


    },

    //拆除建筑时候调用
    onRemoveThisItem() {
        if (!this.targetBuildingsInfo) return;
        // cc.log('this.targetBuildingsInfo', this.targetBuildingsInfo)
        this.targetBuildingsInfo.onClearSelfResetFromType();
        //清除时候，消除建筑物的影响力
        GlobalD.game.onClearBuildingsEffect(this.targetBuildingsInfo);
        //清除底部提示区域
        GlobalD.game.onClearSpawnEditorBuildingTip();

        this.targetBuildingsInfo.node.destroy();
        GlobalD.game.removeBuilding(this.targetBuildingsInfo);
        this.labelTip.string = this.labelTipString;
        this.targetBuildingsInfo = null;


        // cc.log('removeEnd', GlobalD.game.buildingsTiledMapUnit);
    }
    ,
    onCancleSelf() {
        this.node.active = false;

        GlobalD.game._ManageUIScript.onBottomMenuView(true);

        this._CancelClearHighway();
    },
    //取消清除
    _CancelClearHighway() {


        if (this.isMoveState) {
            return;
        }
        //删除对应层的子节点
        let tempNode = this.buildLayer.node.children;
        let length = tempNode.length;
        for (let i = length - 1; i >= 0; i--) {
            if (tempNode[i].getComponent('TiledTile')) {
                tempNode[i].removeComponent('TiledTile');
            }
            tempNode[i].destroy();
        }

        this.selectedTargetIndex = [];
        //清空选中的节点的数组
        this.selectedTarget = [];
    },

    _isShowMoveSelectFrame() {

        this.isMoveState = false;

        this.MoveStartNode.active = false;
        this.MoveEndNode.active = true;

        this._SelectHighwayTiled(this._EndPosition);
    },

    _SelectHighwayTiled(tiledPos) {

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

        if (this.isMoveState) return;

        if (ClearType.Shovel === this.clearType) {
            let buildIndex = GlobalD.TiledMap.getIndex(tiledPos);
            //铲子类型
            if (GlobalD.game.onGetHighwayFromIndex(buildIndex)) {

                let tempNode = this.HighwayLayer.node.children;
                let length = tempNode.length;
                for (let i = length - 1; i >= 0; i--) {
                    let tiledTile = tempNode[i].getComponent('TiledTile');
                    let tiledVector2 = new cc.Vec2(tiledTile.x, tiledTile.y);
                    let tiledIndex = GlobalD.TiledMap.getIndex(tiledVector2);

                    if (tiledIndex === buildIndex) {
                        // cc.log(this.selectedTargetIndex,tiledIndex)
                        if (GlobalD.TiledMap.getIndexArrayAt(tiledVector2, this.selectedTargetIndex)) {

                            // cc.log('存在目标数组了！');
                            return;
                        }
                        let highwayTemp = cc.instantiate(this.removeTipPrefab);
                        highwayTemp.parent = this.buildLayer.node;
                        let tiledTile = highwayTemp.addComponent('TiledTile');
                        tiledTile.x = tiledVector2.x;
                        tiledTile.y = tiledVector2.y;

                        //记录选中的index
                        this.selectedTargetIndex.push(tiledIndex);
                        //记录要删除的节点
                        this.selectedTarget.push(tempNode[i]);
                        return;
                    }

                }
            }
        }
    },
    onConfirmClearHighway() {

        if (this.isMoveState) {

            this._isShowMoveSelectFrame();
            return;
        }

        // cc.log('选中的节点1：',this.selectedTarget);
        let _length = this.selectedTarget.length;

        for (let i = _length - 1; i >= 0; i--) {

            let tiledTile = this.selectedTarget[i].getComponent('TiledTile');
            let tiledVector2 = new cc.Vec2(tiledTile.x, tiledTile.y);
            let tiledIndex = GlobalD.TiledMap.getIndex(tiledVector2);

            //删除节点
            this.selectedTarget[i].destroy();

            //地图设置可行走区域公路设置
            AStar.setMapSolid(tiledTile.x, tiledTile.y, 1);

            //清除公路占位信息
            let occupyArray = GlobalD.game.OccupyArray;
            let occupyLength = occupyArray.length;
            for (let i = occupyLength - 1; i >= 0; i--) {
                if (tiledIndex === occupyArray[i].y) {
                    occupyArray.splice(i, 1);
                    break;

                }
            }

            //ManageGame里面的index
            let allHighway = GlobalD.game.AllHighwayStylesAndIndex;
            let allHighwayLength = GlobalD.game.AllHighwayStylesAndIndex.length;
            for (let i = allHighwayLength - 1; i >= 0; i--) {
                if (tiledIndex === allHighway[i].highwayInfoIndex) {
                    allHighway.splice(i, 1);
                    break;
                }
            }

        }

        // cc.log('选中的节点：',this.selectedTarget);

        this._CancelClearHighway();



    }
    // update (dt) {},
});
