/*Copyright (C) 2010-2020 Duncan Deveaux

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 "PlayerInfo.hpp"

PlayerInfo::PlayerInfo() :
Drawable(), m_player(NULL), m_barsWidth(0.f), m_barsHeight(0.f), m_iconScale(1.f)
{
    m_name.setColor( Color(255,255,255) );
    m_nameBorders.setColor( Color(0,0,0) );
}

void PlayerInfo::draw()
{
    //Update matrix
    Drawable::pushMatrix();
    Drawable::updateMatrix();
    
    m_icon.draw();
    
    m_nameBorders.draw();
    m_name.draw();
    
    m_lifeBarBack.draw();
    m_lifeBar.draw();
    m_lifeText.draw();
    
    Drawable::popMatrix();
}

void PlayerInfo::update()
{
    if (m_player != NULL)
    {
        float playerHealth = m_player->getHealth();
        
        std::ostringstream healthText;
        healthText << playerHealth;
        
        m_lifeText.setText(healthText.str() + " / 100");
        m_lifeBar.setXScale(playerHealth / 100);
    }
}

void PlayerInfo::init()
{
    const Font* nameFont = m_name.getFont();
    const Font* barsFont = m_lifeText.getFont();
    
    if (m_player != NULL && nameFont != NULL && barsFont != NULL)
    {
        float xBase = m_icon.getXPosition() + m_icon.getWidth() * m_icon.getXScale() + 10;
        
        m_barsHeight = (barsFont->getCharSize() / 1.3) + 10;
        m_barsWidth = m_barsHeight * 6;
        
        m_name.setPosition(xBase, 0);
        
        m_lifeBarBack = Polygon::rectangle(0, 0, m_barsWidth, m_barsHeight, Color(175, 175, 175, 175));
        m_lifeBarBack.setBorderSize(0);
        
        m_lifeBar = Polygon::rectangle(0, 0, m_barsWidth, m_barsHeight, Color(0, 192, 0));
        m_lifeBar.setPosition(xBase, (nameFont->getCharSize() / 1.3) + 5);
        m_lifeBar.setBorderSize(0);
        
        m_lifeText.setPosition(m_lifeBar.getXPosition() + 20, m_lifeBar.getYPosition() + 5);
        m_lifeText.setColor(Color(255, 255, 255, 200));
        
        // Height alignment
        float textHeight = m_lifeBar.getYPosition() + m_barsHeight;
        float diffY = (m_icon.getHeight() * m_icon.getYScale() - textHeight) / 2;
        
        m_name.setYPosition(m_name.getYPosition() + diffY);
        m_nameBorders.setPosition(xBase, m_name.getYPosition());
        
        m_lifeBar.setYPosition(m_lifeBar.getYPosition() + diffY);
        m_lifeBarBack.setPosition(m_lifeBar.getXPosition(), m_lifeBar.getYPosition());
        
        m_lifeText.setYPosition(m_lifeText.getYPosition() + diffY);
    }
}

//Get and set methods
void PlayerInfo::setPlayer(Player &player, TextureManager &textureManager)
{
    m_player = &player;
    
    m_icon.setTexture( (*textureManager.getTexture("gfx/characters/reduced.png")) );
    m_icon.setSubRect( Box( ((player.getName() == "Hikou") ? 0.f : 0.5), 0.f, 0.5, 1.f) );
    m_icon.setScale(BASE_ICON_SCALE * m_iconScale);
    
    m_name.setText(player.getName());
    m_nameBorders.setText(player.getName());
    
    init();
}

const Player &PlayerInfo::getPlayer() const
{
    return *m_player;
}

void PlayerInfo::setFonts(Font &nameFont, Font &barsFont)
{
    m_name.setFont(nameFont);
    m_nameBorders.setFont(nameFont);
    
    m_lifeText.setFont(barsFont);
    
    init();
}

const Font *PlayerInfo::getNameFont() const
{
    return m_name.getFont();
}

const Font *PlayerInfo::getBarsFont() const
{
    return m_lifeText.getFont();
}

void PlayerInfo::setIconScale(float scale)
{
    if (scale > 0 && scale < 1 / BASE_ICON_SCALE)
    {
        m_iconScale = scale;
        m_icon.setScale(BASE_ICON_SCALE * m_iconScale);
        
        init();
    }
}

float PlayerInfo::getIconScale() const
{
    return m_iconScale;
}

float PlayerInfo::getWidth() const
{
    if (m_barsWidth > m_name.getWidth())
        return m_lifeBar.getXPosition() + m_barsWidth;
    
    return m_name.getXPosition() + m_name.getWidth();
}

float PlayerInfo::getHeight() const
{
    return m_lifeBar.getYPosition() + m_barsHeight;
}
