import { debug } from 'util';

var BuildingModel = require('./BuildingModel');
// var BuildingInfoJson = require('../utils/buildingInfo'); 
const bInfo = require('../data/buildingInfo');
const bLevel = require('../data/buildingLevel');
export default class BuildingManager {
    
    static _instance;

    _allBuildData = {};

    constructor() {
        // this._allBuildData = BuildingInfoJson.BuildingInfo;
        this._allBuildData = this.formatBuildingInfo();
        console.log(this._allBuildData);
    };

    static get instance() {
        if (!BuildingManager._instance) {
            BuildingManager._instance = new BuildingManager(); 
            
        }
        return BuildingManager._instance;
    };

    formatBuildingInfo() {
        let bInfoMap = {}
        bInfo.forEach(n => {
            bInfoMap[n.id] = n
        })

        // let bLevelMap = {}
        // bLevel.forEach(n => {
        //     if(bLevelMap[n.buildingId] && bLevelMap[n.buildingId].length > 0) {
        //         bLevelMap[n.buildingId].push(n)
        //     } else {
        //         bLevelMap[n.buildingId] = []
        //         bLevelMap[n.buildingId].push(n)
        //     }
        // })

        for (let i in bLevel) {
            let curLevel = bLevel[i]
            curLevel.forEach((item, index) => {
                
                for(var j in item) {
                    item[j] = parseInt(item[j])
                }
                
                let nextObj = {
                    nextRate:0,
                    nextRateUnit:0,
                    nextUpScore:0,
                    hasNext: 0
                };
                let staticObj = {
                    cityId: bInfoMap[item.buildingId].cityId,
                    type: bInfoMap[item.buildingId].type,
                    name: bInfoMap[item.buildingId].name,
                    jobId: bInfoMap[item.buildingId].jobId,
                };
                if(curLevel[index + 1] && curLevel[index + 1].buildingId == item.buildingId) {
                    nextObj = {
                        nextRate: parseInt(curLevel[index + 1].rate),
                        nextRateUnit: parseInt(curLevel[index + 1].rateUnit),
                        nextUpScore: parseInt(curLevel[index + 1].upScore),
                        hasNext: 1, 
                    }
                    staticObj['unlockScore'] = 0
                }
                item = Object.assign(item, staticObj, nextObj)
            })
        }
        
        for(let i in bLevel) {
            let tarObj = Object.assign({}, bLevel[i][0]);
            tarObj['unlockScore'] = bInfoMap[tarObj.buildingId].unlockScore;
            tarObj['level'] = 0;
            bLevel[i].unshift(tarObj);
        }

        return bLevel;
    }

    /**
     * 获取建筑的下一级信息
     * @param {ing} cityId 
     * @param {int} buildingId 
     * @param {int} level 
     */
    getBuildingInfo(cityId, buildingId, level) {
        let building = this._allBuildData[buildingId];
        let buildObj = building.find(n => {
            return n.level == level;
        })
        let buildModel = new BuildingModel(buildObj);
        return buildModel;

    };

    getLevelCount(buildingId) {
        let building = this._allBuildData[buildingId];
        return building.length-1;
    };

}