/*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 "Options.hpp"
#include "Engines/GameEngine.hpp"

Options::Options(GameEngine *gameEngine) :
m_gameEngine(gameEngine), m_window(gameEngine->getWindow()),
m_configuration(gameEngine->getConfiguration())
{
    
}

void Options::run()
{
    //Init variables
        //Get view size
    Vector viewSize = Vector(m_window->getViewWidth(), m_window->getViewHeight());
    
        //Title
    TextureManager textureManager;
    FontManager fontManager;
    
    Text title("Options", *fontManager.getFont("data/gfx/fonts/intuitive.ttf", 74));
    title.setColor(Color(102, 204, 255));
    
    title.setXPosition((viewSize.x - title.getWidth()) / 2);
    title.setYPosition(30);
    
        //Content
    Sprite buttonBackground;
    buttonBackground.setTexture(*textureManager.getTexture("data/gfx/ui/button.png"));
    buttonBackground.setSubRect( Box(0, 124.f / 128, 1.f / 256, 1.f / 128) );
    buttonBackground.setXScale(200);
    buttonBackground.setYScale(75);
    
    //Apply
    TextButton applyButton("Apply", buttonBackground, Box(0, 0, 200, 75));
    applyButton.setTextFont(*fontManager.getFont("data/gfx/fonts/default.ttf", 24));
    applyButton.setTextColor(Color(255, 255, 255));
    
    applyButton.setXPosition((viewSize.x - applyButton.getWidth()) / 2);
    applyButton.setYPosition((viewSize.y - applyButton.getHeight()) - 30);
    
    //Back
    TextButton backButton("< Back", buttonBackground, Box(0, 0, 200, 75));
    backButton.setTextFont(*fontManager.getFont("data/gfx/fonts/default.ttf", 24));
    backButton.setTextColor(Color(255, 255, 255));
    
    backButton.setXPosition(30);
    backButton.setYPosition(30);
    
    //Widgets
    MainTab mainTab(*fontManager.getFont("data/gfx/fonts/default.ttf", 15), *m_configuration, textureManager);
    mainTab.setXPosition(200);
    mainTab.setYPosition(300);
    
    //Start screen loop
    Vector mouse;
    Color clearColor(255, 255, 255);
    
    SDL_Event event;
    while (1)
    {
        //Manage events
        while (m_window->pollEvent(event))
        {
            switch (event.type)
            {
                case SDL_QUIT: //Application closed
                    m_gameEngine->exit();
                    return;
                break;
                
                case SDL_VIDEORESIZE: //Window resized
                    m_window->resize(event.resize.w, event.resize.h, false);
                break;
                
                case SDL_MOUSEBUTTONUP: //Mouse clicked
                    mouse.x = event.button.x;
                    mouse.y = event.button.y;
                    
                    if (applyButton.mouseClick(m_window->viewCoords(mouse)))
                    {
                        int windowWidth = m_configuration->getWindowWidth();
                        int windowHeight = m_configuration->getWindowHeight();
                        
                        mainTab.apply(*m_configuration);
                        
                        if (m_configuration->getWindowWidth() != windowWidth || windowHeight != m_configuration->getWindowHeight())
                        {
                            m_window->init(m_configuration->getWindowWidth(), m_configuration->getWindowHeight(), 32, m_configuration->getFullscreen());
                            m_window->initView(m_configuration->getWindowWidth(), m_configuration->getWindowHeight());
                            
                            //Reload
                            m_gameEngine->initOptions();
                            return;
                        }
                    }
                    else if (backButton.mouseClick(m_window->viewCoords(mouse)))
                    {
                        m_gameEngine->initMainMenu();
                        return;
                    }
                    
                    mainTab.mouseClick(m_window->viewCoords(mouse));
                    
                break;
                
                case SDL_MOUSEMOTION: //Mouse moved
                    mouse.x = event.motion.x;
                    mouse.y = event.motion.y;
                    
                    mainTab.mouseMove(m_window->viewCoords(mouse));
                break;
            }
        }
        
        //Display
            //Clear
        m_window->clear(clearColor);
        
            //Draw
        title.draw();
        
        applyButton.draw();
        backButton.draw();
        
        mainTab.draw();
        
            //Render
        m_window->flush();
    }
}
