/*
 * Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "map.h"

#include <framework/core/resourcemanager.h>

House::House(uint32 hId, const std::string &name, const Position &pos)
{
    setId(hId);
    setName(name);
    if(pos.isValid())
        setEntry(pos);
}

void House::setTile(const TilePtr& tile)
{
    tile->setFlags(TILESTATE_HOUSE);
    m_tiles.insert(std::make_pair(tile->getPosition(), tile));
}

void House::load(const TiXmlElement *elem)
{
    std::string name = elem->Attribute("name");
    if(name.empty())
        name = stdext::format("Unnamed house #%lu", getId());

    setRent(elem->readType<uint32>("rent"));
    setSize(elem->readType<uint32>("size"));
    setTownId(elem->readType<uint32>("townid"));
    m_isGuildHall = elem->readType<bool>("rent");
    setEntry(elem->readPos("entry"));
}

void Houses::addHouse(const HousePtr& house)
{
    if(findHouse(house->getId()) == m_houses.end())
        m_houses.push_back(house);
}

void Houses::removeHouse(uint32 houseId)
{
    auto it = findHouse(houseId);
    if(it != m_houses.end())
        m_houses.erase(it);
}

HousePtr Houses::getHouse(uint32 houseId)
{
    auto it = findHouse(houseId);
    return it != m_houses.end() ? *it : nullptr;
}

void Houses::load(const std::string& fileName)
{
    TiXmlDocument doc;
    doc.Parse(g_resources.loadFile(fileName).c_str());
    if(doc.Error())
        stdext::throw_exception(stdext::format("failed to load '%s': %s (House XML)", fileName, doc.ErrorDesc()));

    TiXmlElement *root = doc.FirstChildElement();
    if(!root || root->ValueTStr() != "houses")
        stdext::throw_exception("invalid root tag name");

    for(TiXmlElement *elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) {
        if(elem->ValueTStr() != "house")
            stdext::throw_exception("invalid house tag.");

        uint32 houseId = elem->readType<uint32>("houseid");
        HousePtr house = getHouse(houseId);
        if(!house)
            house = HousePtr(new House(houseId)), addHouse(house);

        house->load(elem);
    }
}

HouseList::iterator Houses::findHouse(uint32 houseId)
{
    return std::find_if(m_houses.begin(), m_houses.end(),
                           [=] (const HousePtr& house) -> bool { return house->getId() == houseId; });
}

