/*
 * Copyright (C) 2020 BfaCore
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#ifndef TRINITY_OBJECTACCESSOR_H
#define TRINITY_OBJECTACCESSOR_H

#include "ObjectGuid.h"
#include <unordered_map>

class AreaTrigger;
class Conversation;
class Corpse;
class Creature;
class DynamicObject;
class GameObject;
class Map;
class Object;
class Pet;
class Player;
class SceneObject;
class Transport;
class Unit;
class WorldObject;

namespace boost
{
    class shared_mutex;
}

template <class T>
class TC_GAME_API HashMapHolder
{
    //Non instanceable only static
    HashMapHolder() { }

public:
    typedef std::unordered_map<ObjectGuid, T*> MapType;

    static void Insert(T* o);

    static void Remove(T* o);

    static T* Find(ObjectGuid guid);

    static MapType& GetContainer();

    static boost::shared_mutex* GetLock();
};

namespace ObjectAccessor
{
    // these functions return objects only if in map of specified object
    TC_GAME_API WorldObject* GetWorldObject(WorldObject const&, ObjectGuid const&);
    TC_GAME_API Object* GetObjectByTypeMask(WorldObject const&, ObjectGuid const&, uint32 typemask);
    TC_GAME_API Corpse* GetCorpse(WorldObject const& u, ObjectGuid const& guid);
    TC_GAME_API GameObject* GetGameObject(WorldObject const& u, ObjectGuid const& guid);
    TC_GAME_API Transport* GetTransportOnMap(WorldObject const& u, ObjectGuid const& guid);
    TC_GAME_API Transport* GetTransport(ObjectGuid const& guid);
    TC_GAME_API DynamicObject* GetDynamicObject(WorldObject const& u, ObjectGuid const& guid);
    TC_GAME_API AreaTrigger* GetAreaTrigger(WorldObject const& u, ObjectGuid const& guid);
    TC_GAME_API SceneObject* GetSceneObject(WorldObject const& u, ObjectGuid const& guid);
    TC_GAME_API Conversation* GetConversation(WorldObject const& u, ObjectGuid const& guid);
    TC_GAME_API Unit* GetUnit(WorldObject const&, ObjectGuid const& guid);
    TC_GAME_API Creature* GetCreature(WorldObject const& u, ObjectGuid const& guid);
    TC_GAME_API Pet* GetPet(WorldObject const&, ObjectGuid const& guid);
    TC_GAME_API Player* GetPlayer(Map const*, ObjectGuid const& guid);
    TC_GAME_API Player* GetPlayer(WorldObject const&, ObjectGuid const& guid);
    TC_GAME_API Player* GetObjectInWorld(ObjectGuid guid, Player* /*typeSpecifier*/);
    TC_GAME_API Creature* GetCreatureOrPetOrVehicle(WorldObject const&, ObjectGuid const&);

    // these functions return objects if found in whole world
    // ACCESS LIKE THAT IS NOT THREAD SAFE
    TC_GAME_API Unit* FindUnit(ObjectGuid const& g);
    TC_GAME_API Player* FindPlayer(ObjectGuid const&);
    TC_GAME_API Player* FindPlayerByName(std::string const& name);
    TC_GAME_API Player* FindPlayerByLowGUID(ObjectGuid::LowType lowguid);

    // this returns Player even if he is not in world, for example teleporting
    TC_GAME_API Player* FindConnectedPlayer(ObjectGuid const&);
    TC_GAME_API Player* FindConnectedPlayerByName(std::string const& name);

    // when using this, you must use the hashmapholder's lock
    TC_GAME_API HashMapHolder<Player>::MapType const& GetPlayers();

    template<class T>
    void AddObject(T* object)
    {
        HashMapHolder<T>::Insert(object);
    }

    template<class T>
    void RemoveObject(T* object)
    {
        HashMapHolder<T>::Remove(object);
    }

    template<>
    void AddObject(Player* player);

    template<>
    void RemoveObject(Player* player);

    TC_GAME_API void SaveAllPlayers();
    TC_GAME_API Creature* FindCreature(ObjectGuid const&);
};

#endif
