/*Copyright (C) 2010 Deveaux Duncan

This file is part of Hikou no mizu.

Hikou no mizu 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 3 of the License, or
(at your option) any later version.

Hikou no mizu 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 Hikou no mizu.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "FightInterface.hpp"

FightInterface::FightInterface()
{
    
}

void FightInterface::Render(sf::RenderTarget& target, sf::Renderer& renderer) const
{
    for (int i = 0; i < m_playersList.size(); i++)
    {
        target.Draw(m_nameTextsList[i]);
        target.Draw(m_lifeTextsList[i]);
        target.Draw(m_lifeBarsList[i]);
        target.Draw(m_energyTextsList[i]);
        target.Draw(m_energyBarsList[i]);
    }
}

void FightInterface::addPlayer(Player *addedPlayer)
{
    //Add player to m_playersList
    m_playersList.push_back(addedPlayer);
    
    //Create user interface items
    m_nameTextsList.push_back(sf::Text());
    m_lifeTextsList.push_back(sf::Text());
    m_lifeBarsList.push_back(sf::Shape());
    m_energyTextsList.push_back(sf::Text());
    m_energyBarsList.push_back(sf::Shape());
    
    //Draw them
    draw(m_playersList.size() - 1);
}

void FightInterface::removePlayer(Player *removedPlayer)
{
    //Get player's index
    int playerSearchResult = searchForPlayer(removedPlayer);
    
    if (playerSearchResult != -1) //Player's index is registered
    {
        int ix = playerSearchResult;
        
        //Delete objects from their lists
        m_playersList.erase(m_playersList.begin() + ix);
        m_nameTextsList.erase(m_nameTextsList.begin() + ix);
        m_lifeTextsList.erase(m_lifeTextsList.begin() + ix);
        m_lifeBarsList.erase(m_lifeBarsList.begin() + ix);
        m_energyTextsList.erase(m_energyTextsList.begin() + ix);
        m_energyBarsList.erase(m_energyBarsList.begin() + ix);
    }
}

void FightInterface::updateHealth(Player *updatedPlayer)
{
    //Get player's index
    int playerSearchResult = searchForPlayer(updatedPlayer);
    
    if (playerSearchResult != -1) //Player's index is registered
    {
        //Convert int values to std::string
        std::ostringstream healthString;
        healthString << updatedPlayer->getHealth();
        
        std::ostringstream maxHealthString;
        maxHealthString << updatedPlayer->getMaxHealth();
        
        //Update items
        m_lifeTextsList[playerSearchResult].SetString(healthString.str() + "/" + maxHealthString.str());
        m_lifeBarsList[playerSearchResult].SetScaleX((float)updatedPlayer->getHealth() / updatedPlayer->getMaxHealth());
    }
}

void FightInterface::updateEnergy(Player *updatedPlayer)
{
    //Get player's index
    int playerSearchResult = searchForPlayer(updatedPlayer);
    
    if (playerSearchResult != -1) //Player's index is registered
    {
        //Convert int values to std::string
        std::ostringstream energyString;
        energyString << updatedPlayer->getEnergy();
        
        std::ostringstream maxEnergyString;
        maxEnergyString << updatedPlayer->getMaxEnergy();
        
        //Update items
        m_energyTextsList[playerSearchResult].SetString(energyString.str() + "/" + maxEnergyString.str());
        m_energyBarsList[playerSearchResult].SetScaleX(updatedPlayer->getEnergy() / updatedPlayer->getMaxEnergy());
    }
}

void FightInterface::draw(int ixPlayer)
{
    int lifeBarWidth = 100;
    int lifeBarHeight = 20;
    int energyBarWidth = 100;
    int energyBarHeight = 20;
    int elementXPosition = (lifeBarWidth + 50) * ixPlayer;
    
    //Name
    sf::Text playerName(m_playersList[ixPlayer]->getName(), sf::Font::GetDefaultFont(), 30);
    playerName.SetX(elementXPosition);
    playerName.SetColor(sf::Color(0, 0, 0, 255));
    
    m_nameTextsList[ixPlayer] = playerName;
    
    //Health
    std::ostringstream healthString;
    healthString << m_playersList[ixPlayer]->getHealth();
    
    std::ostringstream maxHealthString;
    maxHealthString << m_playersList[ixPlayer]->getMaxHealth();

    int lifeBarYPosition = playerName.GetPosition().y + playerName.GetRect().Height + 5;
    
    sf::Shape playerLifeBar = sf::Shape::Rectangle(0, 0, lifeBarWidth, lifeBarHeight, sf::Color(0, 255, 0, 75));
    playerLifeBar.SetX(elementXPosition);
    playerLifeBar.SetY(lifeBarYPosition);
    
    sf::Text playerLifeText(healthString.str() + "/" + maxHealthString.str(), sf::Font::GetDefaultFont(), 10);
    int lifeTextWidth = playerLifeText.GetRect().Width;
    int lifeTextHeight = playerLifeText.GetRect().Height;
    playerLifeText.SetX(elementXPosition + (lifeBarWidth - lifeTextWidth) / 2);
    playerLifeText.SetY(lifeBarYPosition + (lifeBarHeight - lifeTextHeight) / 2);
    playerLifeText.SetColor(sf::Color(255, 255, 255, 255));
    
    m_lifeTextsList[ixPlayer] = playerLifeText;
    m_lifeBarsList[ixPlayer] = playerLifeBar;
    
    //Energy
    std::ostringstream energyString;
    energyString << m_playersList[ixPlayer]->getEnergy();
    
    std::ostringstream maxEnergyString;
    maxEnergyString << m_playersList[ixPlayer]->getMaxEnergy();

    int energyBarYPosition = lifeBarYPosition + lifeBarHeight + 5;
    
    sf::Shape playerEnergyBar = sf::Shape::Rectangle(0, 0, energyBarWidth, energyBarHeight, sf::Color(0, 200, 255, 75));
    playerEnergyBar.SetX(elementXPosition);
    playerEnergyBar.SetY(energyBarYPosition);
    
    sf::Text playerEnergyText(energyString.str() + "/" + maxEnergyString.str(), sf::Font::GetDefaultFont(), 10);
    int energyTextWidth = playerEnergyText.GetRect().Width;
    int energyTextHeight = playerEnergyText.GetRect().Height;
    playerEnergyText.SetX(elementXPosition + (energyBarWidth - energyTextWidth) / 2);
    playerEnergyText.SetY(energyBarYPosition + (energyBarHeight - energyTextHeight) / 2);
    playerEnergyText.SetColor(sf::Color(255, 255, 255, 255));
    
    m_energyTextsList[ixPlayer] = playerEnergyText;
    m_energyBarsList[ixPlayer] = playerEnergyBar;
}

int FightInterface::searchForPlayer(Player *searchedPlayer)
{
    std::vector<Player*>::iterator it;
    it = std::find(m_playersList.begin(), m_playersList.end(), searchedPlayer);
    
    if (it != m_playersList.end())
    {
        int ixPlayer = std::distance(m_playersList.begin(), it);
        return ixPlayer;
    }
    
    return -1;
}