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

MainMenu::MainMenu(GameEngine *gameEngine) :
m_gameEngine(gameEngine), m_renderWindow(gameEngine->getRenderWindow())
{
    //Get view size
    sf::Vector2f viewSize = m_renderWindow->GetDefaultView().GetSize();
    
    //Create Screen Title : "Hikou no mizu"
    m_hnmTitle.SetString("Hikou no mizu");
    m_hnmTitle.SetCharacterSize(75);
    m_hnmTitle.SetColor(sf::Color(102, 204, 255));
    m_hnmTitle.SetPosition((viewSize.x - m_hnmTitle.GetRect().Width) / 2, 10);
    
    //Create m_menu
        //Style
    m_menuStyle = new TextButton("", 200, 75, sf::Color::White);
    m_menuStyle->enableBackground(false);
    m_menuStyle->setBorderSize(1);
    m_menuStyle->setBorderColor(sf::Color(100, 100, 100));
    
    m_menuStyle->setTextColor(sf::Color(82, 184, 255));
    
        //m_menu
    m_menu.setStyleSample(*m_menuStyle);
    m_menu.setOrientation(0);
    m_menu.setButtonsSpace(30);
    m_menu.loadData("data/conf/menus/mainMenu.xml");
    
    //Launch m_menu
    m_menu.launch();
    
    m_menu.SetX((viewSize.x - m_menu.getWidth()) / 2);
    m_menu.SetY((viewSize.y - m_menu.getHeight()) / 2);
    
    //Create graphics engine
    m_renderEngine.addObject(&m_hnmTitle);
    m_renderEngine.addObject(&m_menu);
}

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

void MainMenu::run()
{
    //Start screen loop
    while (1)
    {
        //Manage events
        sf::Event event;
        while (m_renderWindow->PollEvent(event))
        {
            const sf::Input &input = m_renderWindow->GetInput();
            
            if (event.Type == sf::Event::Closed) //Application closed
            {
                m_gameEngine->exit();
                return;
            }
            
            //Update m_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::Vector2f mousePosition = m_renderWindow->ConvertCoords(input.GetMouseX(), input.GetMouseY());
            
                //Update
            m_menu.update(mousePosition, mouseClicked);
            
            if (m_menu.browsed()) //Browsing ended
            {
                std::string result = m_menu.result().back();
                
                if (result == "play") //Want to play the game
                {
                    m_gameEngine->confCMPlayersNo(5);
                    m_gameEngine->initCharactersMenu();
                    return;
                }
                else if (result == "exit") //Want to exit
                {
                    m_gameEngine->exit();
                    return;
                }
                
                //Reload menu
                m_menu.launch();
            }
        }
        
        //Render
        m_renderWindow->Clear(sf::Color::White);
        
        m_renderEngine.draw(*m_renderWindow);
        
        m_renderWindow->Display();
    }
}