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

MainTab::MainTab(Font &labelFont, const Configuration &configuration, TextureManager &manager) : Tab()
{
    setLabelFont(labelFont);
    
    m_fullscreen = Checkbox(configuration.getFullscreen());
    m_fullscreen.setColor( Color(35, 136, 201) );
    
    m_debugMode = Checkbox(configuration.getDebugMode());
    m_debugMode.setColor( Color(35, 136, 201) );
    
    //Resolution
    std::vector<Vector> modes = Window::getAvailableModes();
    std::vector<std::string> items;
    
    unsigned int defaultItem = 0;
    for (unsigned int i = 0; i < modes.size(); i++)
    {
        if (modes[i].x == configuration.getWindowWidth() && modes[i].y == configuration.getWindowHeight())
            defaultItem = i;
        
        std::ostringstream itemText;
        itemText << modes[i].x << "x" << modes[i].y;
        
        items.push_back(itemText.str());
    }
    
    m_resolution.setTextFont(*m_labelFont);
    m_resolution.setData(items, defaultItem);
    
    //Init arrows
    Texture *arrows = manager.getTexture("data/gfx/ui/arrow.png");
    
    float arrowWidth = (arrows->getWidth() > 0) ? static_cast<float>(22) / arrows->getWidth() : 1;
    float arrowHeight = (arrows->getHeight() > 0) ? static_cast<float>(25) / arrows->getHeight() : 1;
    
    m_defaultArrow.setTexture(*arrows);
    m_defaultArrow.setSubRect( Box(0, 0, arrowWidth, arrowHeight) );
    
    m_hiddenArrow.setTexture(*arrows);
    m_hiddenArrow.setSubRect( Box(0, arrowHeight, arrowWidth, arrowHeight) );
    
    updateHide();
    
    addWidget("Fullscreen :", m_fullscreen, 0);
    addWidget("Resolution :", m_resolution, 5);
    addWidget("Debug :", m_debugMode, 20);
}

bool MainTab::mouseClick(const Vector &mouseCoords)
{
    Vector localMouse = Widget::localCoords(mouseCoords);
    
    m_fullscreen.mouseClick(localMouse);
    
    if (m_fullscreen.isChecked())
        m_resolution.mouseClick(localMouse);
    
    m_debugMode.mouseClick(localMouse);
    
    updateHide();
    
    return Widget::contains(localMouse);
}

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

void MainTab::apply(Configuration &configuration)
{
    configuration.setFullscreen(m_fullscreen.isChecked());
    
    if (m_fullscreen.isChecked())
    {
        std::string result = m_resolution.getData()[m_resolution.getSelection()];
        std::istringstream splitResult(result);
        
        std::string str_width, str_height;
        std::getline(splitResult, str_width, 'x');
        std::getline(splitResult, str_height, 'x');
        
        int width, height;
        std::istringstream convertWidth(str_width), convertHeight(str_height);
        convertWidth >> width;
        convertHeight >> height;
        
        configuration.setWindowWidth(width);
        configuration.setWindowHeight(height);
    }
    else
    {
        configuration.setWindowWidth(1024);
        configuration.setWindowHeight(576);
    }
    
    configuration.setDebugMode(m_debugMode.isChecked());
}

void MainTab::updateHide()
{
    if (m_fullscreen.isChecked())
        m_resolution.setArrow(m_defaultArrow);
    else
        m_resolution.setArrow(m_hiddenArrow);
}
