var BuildingModel = require('./BuildingModel');
const bInfo = require('../data/roomInfo');
const bLevel = require('../data/roomLevel');
const skillInfo = require('../data/skillInfo');
const skillLevel = require('../data/skillLevel');
const starInfo = require('../data/starInfo');
const GameModule = require("./GameModule");

export default class BuildingManager {
    
    static _instance;

    _allBuildData = {};

    _allSkillInfo = {};

    _allSkillLevel = {};

    networkRooms = [];

    constructor() {
        // this._allBuildData = BuildingInfoJson.BuildingInfo;
        this._allBuildData = this.formatBuildingInfo();
        this.initSkill();
        let starInfBp = {}
        starInfo.forEach(n => {
            starInfBp[n.starId] = n;
        })
        this._allStarInfo = starInfBp;

        // console.log(this._allBuildData);
    };

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

    initSkill() {
        let skiInfo = {};
        /// 得到数组 根据技能id获取对应技能的信息
        skillInfo.forEach(n => {
            skiInfo[n.skillId] = n;
        });
        this._allSkillInfo = skiInfo;
        this._allSkillLevel = skillLevel;
    }

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

        this._binfoMap = bInfoMap;

        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 = {
                    nextGold:0,
                    nextGoldUnit:0,
                    nextUpGold:0,
                    hasNext: 0
                };
                let staticObj = {
                    type: bInfoMap[item.roomId].type,
                    name: bInfoMap[item.roomId].name,
                    jobId: bInfoMap[item.roomId].jobId,
                };
                if(curLevel[index + 1] && curLevel[index + 1].roomId == item.roomId) {
                    nextObj = {
                        nextGold: parseInt(curLevel[index + 1].gold),
                        nextGoldUnit: parseInt(curLevel[index + 1].goldUnit),
                        nextUpGold: parseInt(curLevel[index + 1].upGold),
                        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.roomId].upGold;
            tarObj['buildingLevel'] = bInfoMap[tarObj.roomId].buildingLevel;
            tarObj['level'] = 0;
            bLevel[i].unshift(tarObj);
        }

        return bLevel;
    }

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

    /**
     * 获取对应建筑的最高等级
     * @param {*} roomId
     */
    getLevelCount(roomId) {
        let building = this._allBuildData[roomId];
        return building.length-1;
    };

    /// 根据roomId获取房间名字
    getRoomName(roomId) {
        let roomInfo = this._binfoMap[roomId];
        return roomInfo.name;
    }

    /// 获取某个技能的信息
    getSkillInfo(skillId) {
        let skill = this._allSkillInfo[skillId];
        return skill;
    }

    ///获取某个技能 某个level的信息
    getSkillLevelInfo(skillId, skillLevel) {
        if (skillLevel == 0) {
            return undefined;
        }
        let info = this._allSkillLevel[skillId].filter(n => n.level == skillLevel)[0];
        return info;
    }

    /// 获取某个明星的信息
    getStarInfo(starId) {
        return this._allStarInfo[starId];
    }

    /// 根据roomId获取是否已经解锁
    getRoomIsUnlocked(roomId) {
        for (let i = 0; i < GameModule.roomInfo.length; i ++) {
            let room = GameModule.roomInfo[i];
            if (roomId == room.roomId) {
                return room.isUnlocked;
            }
        }
        /// 返回没有解锁
        return 0;
    }

}