/*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());
    
        //Title
    FontManager fontManager;
    
    Text hnmTitle("Hikou no mizu", *fontManager.getFont("data/gfx/fonts/intuitive.ttf", 85));
    hnmTitle.setColor(Color(102, 204, 255));
    
    hnmTitle.setXPosition((viewSize.x - hnmTitle.getWidth()) / 2);
    hnmTitle.setYPosition(30);
    
        //Menu
    TextButton menuStyle("", *fontManager.getFont("data/gfx/fonts/default.ttf", 30), 200, 75, Color(255, 255, 255));
    menuStyle.enableBackground(false);
    menuStyle.setBorderSize(1);
    menuStyle.setBorderColor(Color(100, 100, 100));
    
    menuStyle.setTextColor(Color(82, 184, 255));
    
    TextMenu menu(menuStyle, 30, 0);
    menu.loadData("data/cfg/menus/mainMenu.xml");
    menu.launch();
    
    menu.setXPosition((viewSize.x - menu.getWidth()) / 2);
    menu.setYPosition((viewSize.y - menu.getHeight()) / 2);
    
    //Start screen loop
    Vector mouse;
    Color clearColor(255, 255, 255);
    
    SDL_Event event;
    while (1)
    {
        //Manage events
        m_window->waitEvent(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.update(m_window->viewCoords(mouse), true);
            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 == "exit") //Want to exit
            {
                m_gameEngine->exit();
                return;
            }
            
            //Reload menu
            menu.launch();
        }
        
        //Display
            //Clear
        m_window->clear(clearColor);
        
            //Draw
        hnmTitle.draw();
        menu.draw();
        
            //Render
        m_window->flush();
    }
}
