/*Copyright (C) 2010 Deveaux Duncan

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_renderWindow(gameEngine->getRenderWindow())
{
    //Get view size
    sf::Vector2f viewSize = m_renderWindow->GetDefaultView().GetSize();
    
    //Create titles
    FontManager *fontManager = FontManager::getInstance();
    sf::Font *titleFont = fontManager->getFont("data/gfx/fonts/BradBunR.ttf");
    
        //Create screen title : "Arena selection"
    m_title.SetFont(*titleFont);
    m_title.SetString("Arena selection");
    m_title.SetCharacterSize(74);
    m_title.SetColor(sf::Color(102, 204, 255));
    m_title.SetPosition((viewSize.x - m_title.GetRect().Width) / 2, 10);
    
        //Create subtitle
    m_subtitle.SetString("Select the arena where the fight'll take place");
    m_subtitle.SetFont(*titleFont);
    m_subtitle.SetColor( sf::Color(102, 204, 255) );
    m_subtitle.SetPosition((viewSize.x - m_subtitle.GetRect().Width) / 2, 100);
    
    //Create menu
        //Style
    m_menuStyle = new TextButton("", 200, 75, sf::Color(82, 184, 255));
    m_menuStyle->enableBorder(false);
    
    m_menuStyle->setTextColor(sf::Color::White);
    
        //Menu
    m_menu.setStyleSample(*m_menuStyle);
    m_menu.setOrientation(0);
    m_menu.setButtonsSpace(30);
    m_menu.loadData("data/conf/menus/arenaMenu.xml");
    
    //Launch menu
    m_menu.launch();
    
    m_menu.SetX((viewSize.x - m_menu.getWidth()) / 2);
    m_menu.SetY(200);
}

ArenaMenu::~ArenaMenu()
{
    delete m_menuStyle;
}

void ArenaMenu::run()
{
    //Start screen loop
    while (1)
    {
        //Manage events
        sf::Event event;
        while (m_renderWindow->PollEvent(event))
        {
            if (event.Type == sf::Event::Closed)
            {
                m_gameEngine->exit();
                return;
            }
            
            //Update menu
                //Test mouse click
            bool mouseClicked = false;
            
            if (event.Type == sf::Event::MouseButtonReleased && event.MouseButton.Button == sf::Mouse::Left)
                mouseClicked = true;
            
                //Test mouse position
            sf::Vector2i mousePosition = sf::Mouse::GetPosition(*m_renderWindow);
            sf::Vector2f viewMousePosition = m_renderWindow->ConvertCoords(mousePosition.x, mousePosition.y);
                
                //Update
            m_menu.update(viewMousePosition, mouseClicked);
            
            if (m_menu.browsed()) //Browsing ended
            {
                std::string result = m_menu.result().back();
                
                m_gameEngine->confFight_arena(result);
                m_gameEngine->initFight();
                return;
            }
        }
        
        //Display
            //Clear
        m_renderWindow->Clear(sf::Color::White);
        
            //Draw
        m_renderWindow->Draw(m_title);
        m_renderWindow->Draw(m_subtitle);
        m_renderWindow->Draw(m_menu);
        
            //Render
        m_renderWindow->Display();
    }
}