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

Chooser::Chooser() : Widget(), m_selection(0), m_contentWidth(100)
{
    
}

void Chooser::draw()
{
    //Update matrix
    Drawable::pushMatrix();
    Drawable::updateMatrix();
    
    m_textField.draw();
    
    m_arrow.draw();
    
    glTranslatef(m_arrow.getWidth() * 2 + m_contentWidth, 0, 0);
    glScalef(-1, 1, 1);
    
    m_arrow.draw();
    
    Drawable::popMatrix();
}

bool Chooser::mouseClick(const Vector &mouseCoords)
{
    Vector localMouse = Widget::localCoords(mouseCoords);
    
    Box leftBox(0, 0, m_arrow.getWidth(), m_arrow.getHeight());
    Box rightBox(m_arrow.getWidth() + m_contentWidth, 0, m_arrow.getWidth(), m_arrow.getHeight());
    
    if (leftBox.contains(localMouse))
    {
        if (m_selection == 0) m_selection = m_dataList.size() - 1;
        else m_selection--;
        
        updateText();
        return true;
    }
    else if (rightBox.contains(localMouse))
    {
        if (m_selection == m_dataList.size() - 1) m_selection = 0;
        else m_selection++;
        
        updateText();
        return true;
    }
    
    return false;
}

bool Chooser::mouseMove(const Vector &mouseCoords)
{
    return Widget::contains( Widget::localCoords(mouseCoords) );
}

void Chooser::setData(const std::vector<std::string> &data, unsigned int selection)
{
    if (data.size() > selection)
    {
        m_dataList = data;
        
        m_selection = selection;
        updateText();
    }
}

const std::vector<std::string> &Chooser::getData()
{
    return m_dataList;
}

unsigned int Chooser::getSelection() const
{
    return m_selection;
}

//Get and set methods
void Chooser::setArrow(const Sprite &arrow)
{
    m_arrow = arrow;
    updateText();
}

const Sprite &Chooser::getArrow() const
{
    return m_arrow;
}

void Chooser::setContentWidth(float width)
{
    if (width > 0)
    {
        m_contentWidth = width;
        updateText();
    }
}

float Chooser::getContentWidth() const
{
    return m_contentWidth;
}

void Chooser::setTextFont(Font &textFont)
{
    m_textField.setFont(textFont);
    updateText();
}

const Font &Chooser::getTextFont() const
{
    return m_textField.getFont();
}

void Chooser::setTextColor(const Color &textColor)
{
    m_textField.setColor(textColor);
}

const Color &Chooser::getTextColor() const
{
    return m_textField.getColor();
}

float Chooser::getWidth() const
{
    return m_arrow.getWidth() * 2 + m_contentWidth;
}

float Chooser::getHeight() const
{
    return m_arrow.getHeight();
}

void Chooser::updateText()
{
    if (m_selection < m_dataList.size())
    {
        m_textField.setText(m_dataList[m_selection]);
        
        m_textField.setXPosition((getWidth() - m_textField.getWidth()) / 2);
        m_textField.setYPosition((getHeight() - m_textField.getHeight()) / 2);
    }
}
