/*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 "TextMenu.hpp"

TextMenu::TextMenu() :
m_sampleStyle( TextButton("", 100, 50, sf::Color::White) ), m_browsed(false), m_buttonSpace(10), m_orientation(0)
{
    
}

TextMenu::TextMenu(const TextButton &sampleButton, float space, int orientation) :
m_sampleStyle(sampleButton), m_browsed(false), m_buttonSpace(space), m_orientation(orientation)
{
    
}

void TextMenu::Render(sf::RenderTarget& target, sf::Renderer& renderer) const
{
    for (int i = 0; i < m_buttonsList.size(); i++)
    {
        target.Draw(m_buttonsList[i]);
    }
}

void TextMenu::launch()
{
    //Clearing possible selections
    m_selections.clear();
    m_browsed = false;
    
    //Drawing root elements
    resetCurrentElement();
    draw(m_currentElement);
}

void TextMenu::update(const sf::Vector2f &mousePosition, bool mouseClicked)
{
    //Convert mouse coordinates to local
    sf::Vector2f localMouse = sf::Drawable::TransformToLocal(mousePosition);
    
    //Browse the list
    for (int i = 0; i < m_buttonsList.size(); i++)
    {
        if (m_buttonsList[i].mouseHitTest(localMouse) && mouseClicked) //Button clicked
        {
            std::string buttonString = m_buttonsList[i].getText();
            
            //Search the matching TinyXML element
            while (m_currentElement)
            {
                std::string itemName = m_currentElement->Attribute("content");
                std::string itemId = m_currentElement->Attribute("id");
                
                if (itemName == buttonString) //Found
                {
                    //Register selection in m_selections
                    m_selections.push_back(itemId);
                    
                    if (m_currentElement->NoChildren()) //Selection ended
                        m_browsed = true;
                    else //Continue browsing
                    {
                        //Draw next items
                        m_currentElement = m_currentElement->FirstChildElement();
                        draw(m_currentElement);
                    }
                    
                    return; //End the fonction now
                }
                m_currentElement = m_currentElement->NextSiblingElement();
            }
        }
    }
}

bool TextMenu::browsed() const
{
    return m_browsed;
}

const std::vector<std::string> &TextMenu::result() const
{
    return m_selections;
}

float TextMenu::getWidth() const
{
    float width;
    
    if (m_orientation == 0) //Menu is vertical
        width = m_sampleStyle.getWidth();
    else if (m_orientation == 1) //Menu is Horizontal
        width = (m_sampleStyle.getWidth() + m_buttonSpace) * m_buttonsList.size();
    
    return width;
}

float TextMenu::getHeight() const
{
    float height;
    
    if (m_orientation == 0) //Menu is vertical
        height = (m_sampleStyle.getHeight() + m_buttonSpace) * m_buttonsList.size();
    else if (m_orientation == 1) //Menu is horizontal
        height = m_sampleStyle.getHeight();
    
    return height;
}

void TextMenu::applyStyleNow()
{
    draw(m_currentElement);
}

void TextMenu::draw(TiXmlElement *itemsList)
{
    m_buttonsList.clear();
    
    int i = 0;
    while (itemsList)
    {
        std::string itemName = itemsList->Attribute("content");
        
        TextButton itemButton = m_sampleStyle;
        itemButton.setText(itemName);
        
        if (m_orientation == 0)
            itemButton.SetY(i * (itemButton.getHeight() + m_buttonSpace));
        else if (m_orientation == 1)
            itemButton.SetX(i * (itemButton.getWidth() + m_buttonSpace));
        
        m_buttonsList.push_back(itemButton);
        
        itemsList = itemsList->NextSiblingElement();
        i++;
    }
}

//Get and set methods
void TextMenu::setOrientation(int orientation)
{
    if (orientation == 0 || orientation == 1)
        m_orientation = orientation;
}

int TextMenu::getOrientation() const
{
    return m_orientation;
}

void TextMenu::setStyleSample(const TextButton &sampleStyle)
{
    m_sampleStyle = sampleStyle;
}

const TextButton &TextMenu::getStyleSample() const
{
    return m_sampleStyle;
}

void TextMenu::setButtonsSpace(float space)
{
    m_buttonSpace = space;
}

float TextMenu::getButtonsSpace() const
{
    return m_buttonSpace;
}