var reGameStates = require('GameStates');
//建筑物信息
var BuildInfo = cc.Class({
    name: "BuildInfo",
    properties: {
        //建筑类型
        buildType: {
            default: reGameStates.BuildType.Housing,
            type: cc.Enum(reGameStates.BuildType),
        },
        //建筑id
        id: -1,
        //建筑所占区域
        occupyArea: new cc.Vec2(),
        //起始位置，代码动态设置
        startTilePos: {
            default: new cc.Vec2(),
            visible: false,
        },
        //是否激活建筑物
        //建筑在地图上，但是没激活，不能给游客们进行相应的操作
        isItActive: {
            default: false,
            tooltip: '是否激活建筑物',
        },
        //是否有人占用
        isItOccupied: {
            default: false,
            visible: false,
        },
        //占用对象用的信息
        //AI_worker_Player
        occupantPlayerInfo: {
            default: null,
            visible: false,
        },
        //有东西吗?//是否存在物品
        //
        isThereAnItem: {
            default: false,
            visible: false,
        },
        //是否可以销售产品
        isItSaleable: {
            default: false,
            visible: false,
            tooltip: '动态设定，是否允许销售',
        },

        //是否停运
        isItStopOperation: {
            default: false,
            visible: false,
            tooltip: '动态设定，是否停止运营',
        },
        //建筑名称
         /**
         * 重要：建筑时候 要用名字匹配来做减法 ManageGame.MuinusBuilding
         */
        buildingName: {
            default: '',
        },
        BuildingPrefabName: {
            default: '',
        },
        EnglishName: {
            default: '',
        },
        //建筑简介
        BuildingSynopsis: {
            default: '',
        },
        //建筑图片
        BuildingSprite: {
            default: null,
            type: cc.Sprite
        },
        //此建筑维护费用
        RunningCost: {
            default: 200,
            type: cc.Integer,
            visible: true,
            tooltip: '消耗：维护费用/每月',
        },
        //此建筑每分钟消耗的体力值
        consumeStrength: {
            default: 0,
            type: cc.Integer,
            visible: true,
            // displayName: '体力',
            tooltip: '消耗：体力/每分钟',
        },
        //此建筑总共消耗的体力值
        //工厂的生产力，
        totalConsumption: {
            default: 50,
            type: cc.Integer,
            visible: true,
            // displayName: '体力',
            tooltip: '这个建筑总共消耗的体力值,工厂代表生成力,商店代表销售力',
        },
        //商品的库存总量
        totalInventory: {
            default: 0,
            type: cc.Integer,
            tooltip: '商品的库存总量:销售商店需要,工厂',
        },
        //工厂：代表生产的商品
        //商店：代表销售的商品

        //定义的商品类型，也可动态添加
        //todo...
        goodsArray: {
            default: [],
            type: reGameStates.goods,
        },
        //当前销售的商品
        _goods: {
            default: null,
            visible: false,
        },
        //商品的库存
        _inventory: {
            default: 0,
            type: cc.Integer,
            visible: false,
        },
        //目标建筑信息
        //工厂只对应商店
        _targetBuildingsInfo: {
            default: null,
            visible: false,
        },
        //可以寻找的目标点
        //记录图块的二位数组
        _gotoPosistion: {
            default: [],
            visible: false,
        },

        //特殊建筑用到的影响值，在预制设置
        specialSetValue: {
            default: 3,
            type: cc.Integer,
            visible: true,
            tooltip: '特殊建筑需要填写的值',
        },


        consumeStrengthAddValue: {
            default: 0,
            type: cc.Integer,
            visible: false,
            tooltip: '消耗加成值：体力/每分钟',
        },

        goodsPriceAddValue: {
            default: 0,
            type: cc.Integer,
            visible: false,
            tooltip: '商品销售加成值',
        },
        AssetValue: {
            default: 0,
            type: cc.Integer,
            visible: false,
            tooltip: '资产值',
        },
    },
    //设置当前存量
    onSetCurrentInventory(value) {
        // cc.log("onSetCurrentInventory", value);
        //购买的个数
        let callBackNum = Math.abs(value);
        this._inventory += Number(value);
        if (this._inventory <= 0) {
            // cc.warn("onSetCurrentInventory", this._inventory);
            //如果商品库存不够，返回只购买剩余的个数
            callBackNum += this._inventory;
            this._inventory = 0;
        }
        if (this._inventory > this.totalInventory)
            this._inventory = this.totalInventory;

        // this.node.getComponent("buildingTips").setShowStockData(null,"x"+this._inventory);
        if (this.updatainventory != null) {
            this.updatainventory();
        }

        // console.log("现在调用这个了",value);

        return callBackNum;
    },
    updatainventory: function () {

    },
    setCallBackinventory: function (updatainventory) {
        this.updatainventory = updatainventory;
    }

});
cc.Class({
    extends: cc.Component,
    properties: {
        title: cc.Label,
        //建筑物信息
        buildInfo: {
            type: BuildInfo,
            default: null
        },
        //绘制提示区域
        buildZone: cc.Node,
        _canBuild: false,

        //是否是从数据初始化
        InitPosFromStore: {
            default: false,
            visible: false,
        },
        //初始化位置
        InitPos: false,
        //建筑初始化的开始位置
        InitStartPos: new cc.Vec2(),
        //初始化商店的商品信息
        InitBuildingInfo: {
            default: false,
            tooltip: '初始化商店的商品信息',
        },
        ShowTip: {
            default: null,
            type: cc.Label,
            tooltip: '显示状态提示UI节点',
        },
        ShowTipString: '',
        //初始工作地点的状态
        InitWorkBuildingInfo: {
            default: false,
            tooltip: '初始工作地点的状态',
        },
        InitWorkBuildingIndex: {
            default: 0,
            type: cc.Integer,
            tooltip: '下标：初始工作地点的状态',
        },
        // //是否添加到原料地
        // isAddMatrialArray: {
        //     default: false,
        //     tooltip: '是否添加到原料地',
        // },

    },
    onLoad() {
        this._buildZone = this.buildZone.getComponent('buildZone');

        //设置空字符
        this.title.string = '';

        // this.buildInfo.setCallBackinventory(function () {
        //     this.node.getComponent("buildingTips").setShowStockData(null,"x"+this.buildInfo._inventory);
        // }.bind(this));


        this.AssetValues = [];
        this.AssetValues.push(5000);
        this.AssetValues.push(10000);
        this.AssetValues.push(15000);
        this.AssetValues.push(400);
        this.AssetValues.push(500);
        this.AssetValues.push(600);
        this.AssetValues.push(700);
        this.AssetValues.push(800);
        this.AssetValues.push(900);
        // console.log("我经过吗",this.buildInfo.id);
        //设置财产值

        //银行
        // 406001
        //加工厂
        // 610002
        //农田
        // 610003
        //绿化带
        // 401001
        //花坛
        // 402001
        //游乐场
        // 403001
        //医院
        // 404001
        //住宅
        // 101001
        //单元楼
        // 102001
        //别墅
        // 103001
        //矿坑
        // 610005
        //伐木场
        // 610004

        // 神龙相关id
        //农田 
        // 610006

        //警察局
        // 405001
        //甜品店
        // 610010
        //甜品店
        // 610011
        //甜品店
        // 610012
        //甜品店
        // 610013
        //甜品店
        // 610014
        //甜品店
        // 610015
        //甜品店
        // 610016


        switch (this.buildInfo.id) {
            case 101001:
                this.buildInfo.AssetValue = this.AssetValues[0];
                break;
            case 102001:
                this.buildInfo.AssetValue = this.AssetValues[1];
                break;
            case 103001:
                this.buildInfo.AssetValue = this.AssetValues[1];
                break;
            case 610003:
                this.buildInfo.AssetValue = this.AssetValues[0];
                break;
            case 610004:
                this.buildInfo.AssetValue = this.AssetValues[1];
                break;
            case 610005:
                this.buildInfo.AssetValue = this.AssetValues[1];
                break;
            case 610002:
                this.buildInfo.AssetValue = this.AssetValues[0];
                break;
            case 610010:
            case 610013:
            case 610016:
                this.buildInfo.AssetValue = this.AssetValues[0];
                break;
            case 610011:
            case 610014:
                this.buildInfo.AssetValue = this.AssetValues[1];
                break;
            case 610012:
            case 610015:
                this.buildInfo.AssetValue = this.AssetValues[2];
                break;
            case 401001:
                this.buildInfo.AssetValue = this.AssetValues[0];
                break;
            case 402001:
                this.buildInfo.AssetValue = this.AssetValues[1];
                break;
            case 403001:
                this.buildInfo.AssetValue = this.AssetValues[2];
                break;
        }





        // if (this.buildInfo.buildType == reGameStates.BuildType.Housing) {
        //
        //     this.buildInfo.AssetValue = this.AssetValues[0];
        // } else if (this.buildInfo.buildType == reGameStates.BuildType.Farmland) {
        //     this.buildInfo.AssetValue = this.AssetValues[1];
        // } else if (this.buildInfo.buildType == reGameStates.BuildType.TimberYard) {
        //     this.buildInfo.AssetValue = this.AssetValues[2];
        // } else if (this.buildInfo.buildType == reGameStates.BuildType.MiningPit) {
        //     this.buildInfo.AssetValue = this.AssetValues[3];
        // } else if (this.buildInfo.buildType == reGameStates.BuildType.Factory) {
        //     this.buildInfo.AssetValue = this.AssetValues[4];
        // } else if (this.buildInfo.buildType == reGameStates.BuildType.Shop) {
        //     this.buildInfo.AssetValue = this.AssetValues[5];
        // } else if (this.buildInfo.buildType == reGameStates.BuildType.Special) {
        //     this.buildInfo.AssetValue = this.AssetValues[6];
        // }


    },
    start() {
        this._tileMap = GlobalD.TiledMap;

        //开始初始化位置，添加到数组里面
        if (this.InitPos) {

            //场景的预制初始化可销售状态
            if (this.buildInfo.buildType == reGameStates.BuildType.Farmland) {
                //设置销售状态,
                this.buildInfo.isItSaleable = true;
            }

            //把物体定位到对应的坐标上去
            this._currentTiledValue = this.InitStartPos;
            if (this._currentTiledValue) {
                //往前移一格
                var endTiledPos = cc.v2(this._currentTiledValue.x, this._currentTiledValue.y);
                var endPos = this._tileMap._getTheMiddleLocationFromtilePos(endTiledPos);
                this.node.setPosition(endPos);
                let isHas = GlobalD.game.doesItExistArray(this.buildInfo.id);
                if (!isHas) {
                    //起始坐标，占位范围，是否占位
                    GlobalD.game.addBuildTiled(this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);

                    this.buildInfo.startTilePos = this._currentTiledValue;
                    GlobalD.game.addBuilding(this);

                    console.log(this.buildInfo.buildingName);
                } else {//如果已经存在，更新新位置
                    //可以移动
                    let isMove = true;
                    GlobalD.game.updateBuildOccupy(isMove, this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);

                    this.buildInfo.startTilePos = this._currentTiledValue;
                    GlobalD.game.updateBuilding(this);
                }
            }
        }


        //初始化设置房屋信息
        if (this.InitBuildingInfo) {
            //设置销售物品
            this.onSettingBuildingState();
        }
        if (this.InitWorkBuildingInfo) {
            this.onInitBuildingState();
        }
        if (this.buildInfo.consumeStrength == 0 && this.buildInfo.buildType !== reGameStates.BuildType.Shop && this.buildInfo.buildType !== reGameStates.BuildType.Special) {
            cc.warn('这个建筑可能需要消耗或者增加体力值：', this.buildInfo.startTilePos);
        }
    },
    //代码设置位置
    setInitStartPos: function (x, y) {
        this.InitStartPos = cc.v2(x, y);
        //把物体定位到对应的坐标上去
        this._currentTiledValue = this.InitStartPos;
        if (this._currentTiledValue) {
            //往前移一格
            var endTiledPos = cc.v2(this._currentTiledValue.x, this._currentTiledValue.y);
            var endPos = this._tileMap._getTheMiddleLocationFromtilePos(endTiledPos);
            this.node.setPosition(endPos);
            let isHas = GlobalD.game.doesItExistArray(this.buildInfo.id);
            if (!isHas) {
                //起始坐标，占位范围，是否占位
                GlobalD.game.addBuildTiled(this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);

                this.buildInfo.startTilePos = this._currentTiledValue;
                GlobalD.game.addBuilding(this);
            } else {//如果已经存在，更新新位置
                //可以移动
                let isMove = true;
                GlobalD.game.updateBuildOccupy(isMove, this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);

                this.buildInfo.startTilePos = this._currentTiledValue;
                GlobalD.game.updateBuilding(this);
            }
        }
    },


    //初始化时候，生成房屋的数据状态
    onInitBuildingState() {
        // if (this.buildInfo.buildType == reGameStates.BuildType.Farmland)
        {
            //设置库存
            this.buildInfo._inventory = this.buildInfo.totalInventory;
            //设置满库存状态
            this.node.getComponent('WorkingBuilding').onSetWorkStateFormReadingData(this.InitWorkBuildingIndex);

        }
    },
    //设置房屋当前状态
    //比如，商店：没有商品，
    onSettingBuildingState() {
        //如果是商店的话
        if (this.buildInfo.buildType == reGameStates.BuildType.Shop) {
            let num = this.buildInfo.onSetCurrentInventory(-1000);
            // cc.log(num);
            //初始化销售的商品
            for (let i = 0; i < this.buildInfo.goodsArray.length; i++) {
                if (this.buildInfo.goodsArray[i].isItSale) {
                    this.buildInfo._goods = this.buildInfo.goodsArray[i];
                    break;
                }
            }
        }
    },
    //在预制初始化时候调用
    onInitFromPrefabs() {

    },
    onShowTip(tipString) {
        if (!this.ShowTip) return;
        this.ShowTip.string = tipString;
        this.ShowTip.node.parent.active = true;

        setTimeout(() => {
            this.ShowTip.node.parent.active = false;
        }, 1000)
    },
    //商品售罄
    onSetTipSellOut() {

        // cc.log('商店售罄');
        // this.onShowTip(this.ShowTipString);
        //通知工厂生产销售商品
        //{buildingInfo，goodsItem}
        GlobalD.game.onNotificationFactory(this);
    },
    //工人送完商品设置
    //商店
    onResetFromWorkersFinished() {
        //暂时设置最高的库存量
        this.buildInfo.onSetCurrentInventory(5);
        //清空对应的工厂信息
        this.buildInfo._targetBuildingsInfo = null;
    },
    //工厂到商店流程中断时候设置
    onResetFromWorkersSuspend() {
        //暂时设置最高的库存量
        // this.buildInfo.onSetCurrentInventory(0);
        //清空对应的工厂信息
        this.buildInfo._targetBuildingsInfo = null;
    },
    //商店重置对应工厂信息
    onResetProductionRequest() {
        //通知工厂对应的工人
        if (this.buildInfo.occupantPlayerInfo != null) {
            //工厂设置值
            this.buildInfo.occupantPlayerInfo.transTarget = null;
        }

        this.buildInfo._goods = null;
        this.buildInfo._targetBuildingsInfo = null;
        this.buildInfo.isItOccupied = false;

    },


    //清除建筑后，调用此函数 
    onClearSelfResetFromType() {
        // cc.log('清除类型：', this.buildInfo.buildType)
        if (this.buildInfo.buildType == reGameStates.BuildType.Shop) {
            //如果清除的建筑是商店
            //通知工厂工作清除
            if (this.buildInfo._targetBuildingsInfo) {
                this.buildInfo._targetBuildingsInfo.onResetProductionRequest();
            }

        } else if (this.buildInfo.buildType == reGameStates.BuildType.Factory) {
            //如果是工厂拆除了

            //通知工厂对应的工人
            if (this.buildInfo.occupantPlayerInfo != null) {
                // this.occupantPlayerInfo.AIAttribute.isWorking = false;
                // this.occupantPlayerInfo._isColletion = false;
                //工厂设置值
                this.buildInfo.occupantPlayerInfo.transTarget = null;
            }

            //通知商店，通知清除对应的工厂目标
            if (this.buildInfo._targetBuildingsInfo)
                this.buildInfo._targetBuildingsInfo.onResetFromWorkersSuspend();


        } else if (this.buildInfo.buildType == reGameStates.BuildType.Housing) {
            //如果是房子拆除
            if (this.buildInfo.occupantPlayerInfo) {
                this.buildInfo.occupantPlayerInfo.onDismantleBuilding();
            }
        }
        // else {
        //     //其他工作地点，比如农田，矿厂，伐木场
        //     //特殊建筑可以忽略
        //     // cc.log('清除类型：', this.buildInfo.buildType)
        // }
    },

    //如果商店停运调用这个函数
    onStopOperation() {
        if (this.buildInfo.buildType == reGameStates.BuildType.Shop) {
            if (this.buildInfo._targetBuildingsInfo)
                this.buildInfo._targetBuildingsInfo.onResetProductionRequest();

            //如果商店停运
            this.onResetFromWorkersSuspend();

            // cc.log('停运后，',this.buildInfo._targetBuildingsInfo);
        } else if (this.buildInfo.buildType == reGameStates.BuildType.Factory) {

            if (this.buildInfo._targetBuildingsInfo) {
                //如果工厂停运，就把工厂对应的商店目标，通知商店，清空工厂自己
                this.buildInfo._targetBuildingsInfo.buildInfo._targetBuildingsInfo = null;
            }
            this.onResetProductionRequest();
        }
    },

    /**
     * 植物生长
     */
    onInitHolyFarmlandSeedFromGrow(growStage,spriteFrame){
        this.node.getComponent('WorkingBuilding').onSetGrow(growStage,spriteFrame);
        // this.node.getComponent('WorkingBuilding').onHolyFarmlandSeedFromGrow();
    }


});
