/*Copyright (C) 2010-2012 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 "TextMenu.hpp"

TextMenu::TextMenu() : Menu(), m_buttonSpace(10), m_orientation(0), m_browsed(false)
{
    
}

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

void TextMenu::draw()
{
    //Update matrix
    Drawable::pushMatrix();
    Drawable::updateMatrix();
    
    for (unsigned int i = 0; i < m_buttonsList.size(); i++)
        m_buttonsList[i].draw();
    
    Drawable::popMatrix();
}

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

void TextMenu::update(const Vector &mousePosition, bool mouseClicked)
{
    //Convert mouse coordinates to local
    Vector localMouse;
    localMouse.x = mousePosition.x - getXPosition();
    localMouse.y = mousePosition.y - getYPosition();
    
    //Browse the list
    for (unsigned 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 = 0;
    
    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 = 0;
    
    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.setYPosition(i * (itemButton.getHeight() + m_buttonSpace));
        else if (m_orientation == 1)
            itemButton.setXPosition(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;
}
