/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see .
 */
#include "MapManager.h"
#include "BattlefieldMgr.h"
#include "Battleground.h"
#include "Containers.h"
#include "DatabaseEnv.h"
#include "DB2Stores.h"
#include "GarrisonMap.h"
#include "Group.h"
#include "InstanceLockMgr.h"
#include "Log.h"
#include "Map.h"
#include "OutdoorPvPMgr.h"
#include "Player.h"
#include "ScenarioMgr.h"
#include "ScriptMgr.h"
#include "World.h"
#include "WorldStateMgr.h"
#include 
#include 
MapManager::MapManager()
    : _freeInstanceIds(std::make_unique()), _nextInstanceId(0), _scheduledScripts(0)
{
    i_gridCleanUpDelay = sWorld->getIntConfig(CONFIG_INTERVAL_GRIDCLEAN);
    i_timer.SetInterval(sWorld->getIntConfig(CONFIG_INTERVAL_MAPUPDATE));
}
MapManager::~MapManager() = default;
void MapManager::Initialize()
{
    Map::InitStateMachine();
    int num_threads(sWorld->getIntConfig(CONFIG_NUMTHREADS));
    // Start mtmaps if needed.
    if (num_threads > 0)
        m_updater.activate(num_threads);
}
void MapManager::InitializeVisibilityDistanceInfo()
{
    for (auto iter = i_maps.begin(); iter != i_maps.end(); ++iter)
        iter->second->InitVisibilityDistance();
}
MapManager* MapManager::instance()
{
    static MapManager instance;
    return &instance;
}
Map* MapManager::FindMap_i(uint32 mapId, uint32 instanceId) const
{
    auto itr = i_maps.find({ mapId, instanceId });
    return itr != i_maps.end() ? itr->second.get() : nullptr;
}
Map* MapManager::CreateWorldMap(uint32 mapId, uint32 instanceId)
{
    Map* map = new Map(mapId, i_gridCleanUpDelay, instanceId, DIFFICULTY_NONE);
    map->LoadRespawnTimes();
    map->LoadCorpseData();
    map->InitSpawnGroupState();
    if (sWorld->getBoolConfig(CONFIG_BASEMAP_LOAD_GRIDS))
        map->LoadAllCells();
    return map;
}
InstanceMap* MapManager::CreateInstance(uint32 mapId, uint32 instanceId, InstanceLock* instanceLock, Difficulty difficulty, TeamId team, Group* group)
{
    // make sure we have a valid map id
    MapEntry const* entry = sMapStore.LookupEntry(mapId);
    if (!entry)
    {
        TC_LOG_ERROR("maps", "CreateInstance: no entry for map {}", mapId);
        ABORT();
    }
    // some instances only have one difficulty
    sDB2Manager.GetDownscaledMapDifficultyData(mapId, difficulty);
    TC_LOG_DEBUG("maps", "MapInstanced::CreateInstance: {}map instance {} for {} created with difficulty {}",
        instanceLock && instanceLock->IsNew() ? "" : "new ", instanceId, mapId, sDifficultyStore.AssertEntry(difficulty)->Name[sWorld->GetDefaultDbcLocale()]);
    InstanceMap* map = new InstanceMap(mapId, i_gridCleanUpDelay, instanceId, difficulty, team, instanceLock);
    ASSERT(map->IsDungeon());
    map->LoadRespawnTimes();
    map->LoadCorpseData();
    if (group)
        map->TrySetOwningGroup(group);
    map->CreateInstanceData();
    map->SetInstanceScenario(sScenarioMgr->CreateInstanceScenario(map, team));
    map->InitSpawnGroupState();
    if (sWorld->getBoolConfig(CONFIG_INSTANCEMAP_LOAD_GRIDS))
        map->LoadAllCells();
    return map;
}
BattlegroundMap* MapManager::CreateBattleground(uint32 mapId, uint32 instanceId, Battleground* bg)
{
    TC_LOG_DEBUG("maps", "MapInstanced::CreateBattleground: map bg {} for {} created.", instanceId, mapId);
    BattlegroundMap* map = new BattlegroundMap(mapId, i_gridCleanUpDelay, instanceId, DIFFICULTY_NONE);
    ASSERT(map->IsBattlegroundOrArena());
    map->SetBG(bg);
    bg->SetBgMap(map);
    map->InitSpawnGroupState();
    if (sWorld->getBoolConfig(CONFIG_BATTLEGROUNDMAP_LOAD_GRIDS))
        map->LoadAllCells();
    return map;
}
GarrisonMap* MapManager::CreateGarrison(uint32 mapId, uint32 instanceId, Player* owner)
{
    GarrisonMap* map = new GarrisonMap(mapId, i_gridCleanUpDelay, instanceId, owner->GetGUID());
    ASSERT(map->IsGarrison());
    map->InitSpawnGroupState();
    return map;
}
/*
- return the right instance for the object, based on its InstanceId
- create the instance if it's not created already
- the player is not actually added to the instance (only in InstanceMap::Add)
*/
Map* MapManager::CreateMap(uint32 mapId, Player* player)
{
    if (!player)
        return nullptr;
    MapEntry const* entry = sMapStore.LookupEntry(mapId);
    if (!entry)
        return nullptr;
    std::unique_lock lock(_mapsLock);
    Map* map = nullptr;
    uint32 newInstanceId = 0;                       // instanceId of the resulting map
    if (entry->IsBattlegroundOrArena())
    {
        // instantiate or find existing bg map for player
        // the instance id is set in battlegroundid
        newInstanceId = player->GetBattlegroundId();
        if (!newInstanceId)
            return nullptr;
        map = FindMap_i(mapId, newInstanceId);
        if (!map)
        {
            if (Battleground* bg = player->GetBattleground())
                map = CreateBattleground(mapId, newInstanceId, bg);
            else
            {
                player->TeleportToBGEntryPoint();
                return nullptr;
            }
        }
    }
    else if (entry->IsDungeon())
    {
        Group* group = player->GetGroup();
        Difficulty difficulty = group ? group->GetDifficultyID(entry) : player->GetDifficultyID(entry);
        MapDb2Entries entries{ entry, sDB2Manager.GetDownscaledMapDifficultyData(mapId, difficulty) };
        ObjectGuid instanceOwnerGuid = group ? group->GetRecentInstanceOwner(mapId) : player->GetGUID();
        InstanceLock* instanceLock = sInstanceLockMgr.FindActiveInstanceLock(instanceOwnerGuid, entries);
        if (instanceLock)
        {
            newInstanceId = instanceLock->GetInstanceId();
            // Reset difficulty to the one used in instance lock
            if (!entries.Map->IsFlexLocking())
                difficulty = instanceLock->GetDifficultyId();
        }
        else
        {
            // Try finding instance id for normal dungeon
            if (!entries.MapDifficulty->HasResetSchedule())
                newInstanceId = group ? group->GetRecentInstanceId(mapId) : player->GetRecentInstanceId(mapId);
            // If not found or instance is not a normal dungeon, generate new one
            if (!newInstanceId)
                newInstanceId = GenerateInstanceId();
            instanceLock = sInstanceLockMgr.CreateInstanceLockForNewInstance(instanceOwnerGuid, entries, newInstanceId);
        }
        // it is possible that the save exists but the map doesn't
        map = FindMap_i(mapId, newInstanceId);
        // is is also possible that instance id is already in use by another group for boss-based locks
        if (!entries.IsInstanceIdBound() && instanceLock && map && map->ToInstanceMap()->GetInstanceLock() != instanceLock)
        {
            newInstanceId = GenerateInstanceId();
            instanceLock->SetInstanceId(newInstanceId);
            map = nullptr;
        }
        if (!map)
        {
            map = CreateInstance(mapId, newInstanceId, instanceLock, difficulty, player->GetTeamId(), group);
            if (group)
                group->SetRecentInstance(mapId, instanceOwnerGuid, newInstanceId);
            else
                player->SetRecentInstance(mapId, newInstanceId);
        }
    }
    else if (entry->IsGarrison())
    {
        newInstanceId = player->GetGUID().GetCounter();
        map = FindMap_i(mapId, newInstanceId);
        if (!map)
            map = CreateGarrison(mapId, newInstanceId, player);
    }
    else
    {
        newInstanceId = 0;
        if (entry->IsSplitByFaction())
            newInstanceId = player->GetTeamId();
        map = FindMap_i(mapId, newInstanceId);
        if (!map)
            map = CreateWorldMap(mapId, newInstanceId);
    }
    if (map)
    {
        Trinity::unique_trackable_ptr