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

CharactersMenu::CharactersMenu(GameEngine *gameEngine) :
m_gameEngine(gameEngine), m_renderWindow(gameEngine->getRenderWindow()), m_playersNo(2)
{
    //Get view size
    sf::Vector2f viewSize = m_renderWindow->GetDefaultView().GetSize();
    
    //Create Screen title : "Characters selection"
    m_title.SetString("Characters selection");
    m_title.SetCharacterSize(75);
    m_title.SetColor(sf::Color(102, 204, 255));
    m_title.SetPosition((viewSize.x - m_title.GetRect().Width) / 2, 10);
        
    //Create Players number text
    m_playersNoText.SetColor(sf::Color(102, 204, 255));
    
    //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(1);
    m_menu.setButtonsSpace(30);
    m_menu.loadData("data/conf/menus/charactersMenu.xml");
    
    //Launch Menu
    m_menu.launch();
    
    m_menu.SetPosition((viewSize.x - m_menu.getWidth()) / 2, 300);
    
    //Create graphics engine
    m_renderEngine.addObject(&m_title);
    m_renderEngine.addObject(&m_playersNoText);
    m_renderEngine.addObject(&m_menu);
}

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

void CharactersMenu::setPlayersNo(int playersNo)
{
    m_playersNo = playersNo;
}

void CharactersMenu::run()
{
    //Update subtitle
    updatePlayersNoText(1, m_playersNo);
    
    //Create players std::vector
    std::vector<std::string> playersList;
    
    //Create menu iterator
    int menuLaunchId = 0;
    
    //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)
            {
                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::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();
                playersList.push_back(result); //Insert result
                
                menuLaunchId++;
                if (menuLaunchId < m_playersNo) //Selection unfinished
                {
                    updatePlayersNoText(menuLaunchId + 1, m_playersNo);
                }
                else if (menuLaunchId == m_playersNo) //Selection ended
                {
                    m_gameEngine->confFightPlayers(playersList);
                    m_gameEngine->initFight();
                    return;
                }
                
                //Reload menu
                m_menu.launch();
            }
        }
        
        //Render
        m_renderWindow->Clear(sf::Color::White);
        
        m_renderEngine.draw(*m_renderWindow);
        
        m_renderWindow->Display();
    }
}

void CharactersMenu::updatePlayersNoText(int playerId, int playersNo)
{
    //Get view size
    sf::Vector2f viewSize = m_renderWindow->GetDefaultView().GetSize();
    
    std::ostringstream convertToString;
    convertToString << "Player " << playerId << " / " << playersNo;
    
    //Update text
    m_playersNoText.SetString(convertToString.str());
    
    //Update position
    m_playersNoText.SetPosition((viewSize.x - m_playersNoText.GetRect().Width) / 2, 100);
}