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

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

Chooser::Chooser(const std::vector<std::string> &data, unsigned int selection) :
Widget(), m_selection(0)
{
    setData(data, selection);
}

Chooser::Chooser(const TextButton &button, const std::vector<std::string> &data, unsigned int selection) :
Widget(), m_selection(0)
{
    setButton(button);
    setData(data, selection);
}

void Chooser::draw()
{
    //Update matrix
    Drawable::pushMatrix();
    Drawable::updateMatrix();
    
    m_button.draw();
    
    Drawable::popMatrix();
}

bool Chooser::mouseClick(const Vector &mouseCoords)
{
    Vector localMouse = Widget::localCoords(mouseCoords);
    
    m_button.mouseClick(localMouse);
    
    if ( Widget::contains(localMouse) )
    {
        if (m_selection == m_dataList.size() - 1) m_selection = 0;
        else m_selection++;
        
        if (m_selection < m_dataList.size())
            m_button.setText(m_dataList[m_selection]);
        return true;
    }
    
    return false;
}

bool Chooser::mouseMove(const Vector &mouseCoords)
{
    Vector localMouse = Widget::localCoords(mouseCoords);
    
    m_button.mouseMove(localMouse);
    
    if ( Widget::contains(localMouse) )
    {
        setFocus(true);
        return true;
    }
    
    setFocus(false);
    return false;
}

void Chooser::setFocus(bool focused)
{
    if (m_focused != focused)
    {
        Widget::setFocus(focused);
        
        float prev_width = m_button.getWidth(), prev_height = m_button.getHeight();
        
        //Center widget
        if (m_focused)
            m_button.setOrigin((m_button.getWidth() - prev_width) / 2, (m_button.getHeight() - prev_height) / 2);
        else
            m_button.setOrigin(0, 0);
    }
}

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

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

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

void Chooser::setButton(const TextButton &button)
{
    m_button = button;
    m_button.setText("");
    m_button.setPosition(0, 0);
    
    //Update text
    if (m_selection < m_dataList.size())
        m_button.setText(m_dataList[m_selection]);
}

const TextButton &Chooser::getButton() const
{
    return m_button;
}

float Chooser::getWidth() const
{
    return m_button.getWidth() * m_button.getXScale();
}

float Chooser::getHeight() const
{
    return m_button.getHeight() * m_button.getYScale();
}
