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

MainMenu::MainMenu(GameEngine *gameEngine) :
m_gameEngine(gameEngine), m_window(gameEngine->getWindow())
{
    
}

void MainMenu::run()
{
    //Init variables
        //Get view size
    Vector viewSize = Vector(m_window->getViewWidth(), m_window->getViewHeight());
    
    
    TextureManager textureManager;
    FontManager fontManager;
    
    //Background ( + Title)
    Sprite background;
    background.setTexture(*textureManager.getTexture("data/gfx/ui/title.png"));
    background.setSubRect( Box(1, 1, 1366.f / 2048, 768.f / 1024) );
    
    background.setXScale(viewSize.x / background.getWidth());
    background.setYScale(viewSize.y / background.getHeight());
    
        //Menu
    Sprite menu_default, menu_hover;
    menu_default.setTexture(*textureManager.getTexture("data/gfx/ui/button.png"));
    menu_default.setSubRect( Box(0, 0, 237.f / 256, 60.f / 128) );
    
    menu_hover.setTexture(*textureManager.getTexture("data/gfx/ui/button.png"));
    menu_hover.setSubRect( Box(0, 62.f / 128, 248.f / 256, 60.f / 128) );
    
    TextButton menuTheme_default(menu_default, Box(47, 1, 173, 59)), menuTheme_hover(menu_hover, Box(59, 1, 173, 59));
    menuTheme_default.setTextFont(*fontManager.getFont("data/gfx/fonts/default.ttf", 30));
    menuTheme_default.setTextColor(Color(255, 255, 255));
    
    menuTheme_hover.setTextFont(*fontManager.getFont("data/gfx/fonts/default.ttf", 30));
    menuTheme_hover.setTextColor(Color(255, 255, 255));
    
    TextMenu menu(menuTheme_default, menuTheme_hover, 20, 0);
    menu.loadData("data/cfg/menus/mainMenu.xml");
    menu.launch();
    
    menu.setXPosition((viewSize.x - menu.getWidth()) / 10);
    menu.setYPosition((viewSize.y - menu.getHeight()) / 2 + 30);
    
    //Start screen loop
    Vector mouse;
    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;
                    
                    menu.mouseClick(m_window->viewCoords(mouse));
                break;
                
                case SDL_MOUSEMOTION: //Mouse moved
                    mouse.x = event.motion.x;
                    mouse.y = event.motion.y;
                    
                    menu.mouseMove(m_window->viewCoords(mouse));
                break;
            }
        }
            
        if (menu.browsed()) //Browsing ended
        {
            std::string result = menu.result().back();
            
            if (result == "play") //Want to play the game
            {
                m_gameEngine->confCharactersMenu_playersNo(2);
                m_gameEngine->initCharactersMenu();
                return;
            }
            else if (result == "options")
            {
                m_gameEngine->initOptions();
                return;
            }
            else if (result == "exit") //Want to exit
            {
                m_gameEngine->exit();
                return;
            }
            
            //Reload menu
            menu.launch();
        }
        
        //Display
            //Clear
        m_window->clear();
        
            //Draw
        background.draw();
        menu.draw();
        
            //Render
        m_window->flush();
    }
}
