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

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

void ArenaMenu::run()
{
    //Init variables
        //Get view size
    Vector viewSize = Vector(m_window->getViewWidth(), m_window->getViewHeight());
    
        //Titles
    FontManager fontManager;
    
    Text title("Arena selection", *fontManager.getFont("data/gfx/fonts/intuitive.ttf", 74));
    title.setColor(Color(102, 204, 255));
    
    title.setXPosition((viewSize.x - title.getWidth()) / 2);
    title.setYPosition(30);
    
    Text subtitle("Select the arena where the fight'll take place", *fontManager.getFont("data/gfx/fonts/intuitive.ttf", 30));
    subtitle.setColor(Color(102, 204, 255));
    
    subtitle.setXPosition((viewSize.x - subtitle.getWidth()) / 2);
    subtitle.setYPosition(100);
    
        //Menu
    TextButton menuStyle("", *fontManager.getFont("data/gfx/fonts/default.ttf", 30), 200, 75, Color(82, 184, 255));
    menuStyle.enableBorder(false);
    
    menuStyle.setTextColor(Color(255, 255, 255));
    
    TextMenu menu(menuStyle, 30, 0);
    menu.loadData("data/cfg/menus/arenaMenu.xml");
    menu.launch();
    
    menu.setXPosition((viewSize.x - menu.getWidth()) / 2);
    menu.setYPosition(155);
    
    //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();
            
            m_gameEngine->confFight_arena(result);
            m_gameEngine->initFight();
            return;
        }
        
        //Display
            //Clear
        m_window->clear(clearColor);
        
            //Draw
        title.draw();
        subtitle.draw();
        menu.draw();
        
            //Render
        m_window->flush();
    }
}
