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

CharactersMenu::CharactersMenu(GameEngine *gameEngine) :
m_gameEngine(gameEngine), m_window(gameEngine->getWindow()), m_playersNo(2)
{
    
}

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

//Screen virtual method
void CharactersMenu::run()
{
    //Init variables
        //Get view size
    Vector viewSize = Vector(m_window->getViewWidth(), m_window->getViewHeight());
    
        //Titles
    FontManager fontManager;
    
    Text title("Characters 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 playersNoText;
    playersNoText.setFont(*fontManager.getFont("data/gfx/fonts/intuitive.ttf", 30));
    playersNoText.setColor(Color(102, 204, 255));
    
    updatePlayersNoText(playersNoText, 1, m_playersNo);

        //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, 1);
    menu.loadData("data/cfg/menus/charactersMenu.xml");
    menu.launch();
    
    menu.setXPosition((viewSize.x - menu.getWidth()) / 2);
    menu.setYPosition((viewSize.y - menu.getHeight()) / 2);
    
    
    std::vector<std::string> playersList;
    std::vector<int> playersType;
    
    int menuLaunchId = 0;
    
    //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();
            
            std::string playerName;
            int playerType;
            
            if (result == "Hikou-human")
            {
                playerName = "Hikou";
                playerType = 0;
            }
            else if (result == "Hikou-ai")
            {
                playerName = "Hikou";
                playerType = 1;
            }
            else if (result == "Takino-human")
            {
                playerName = "Takino";
                playerType = 0;
            }
            else if (result == "Takino-ai")
            {
                playerName = "Takino";
                playerType = 1;
            }
            
            //Insert result
            playersList.push_back(playerName);
            playersType.push_back(playerType);
            
            menuLaunchId++;
            if (menuLaunchId < m_playersNo) //Selection unfinished
            {
                updatePlayersNoText(playersNoText, menuLaunchId + 1, m_playersNo);
            }
            else if (menuLaunchId == m_playersNo) //Selection ended
            {
                m_gameEngine->confFight_players(playersList, playersType);
                m_gameEngine->initArenaMenu();
                return;
            }
                
            //Reload menu
            menu.launch();
        }
        
        //Display
            //Clear
        m_window->clear(clearColor);
        
            //Draw
        title.draw();
        playersNoText.draw();
        menu.draw();
        
            //Render
        m_window->flush();
    }
}

void CharactersMenu::updatePlayersNoText(Text &textField, int playerIx, int playersNo)
{
    std::ostringstream convertToString;
    convertToString << "Player " << playerIx << " / " << playersNo;
    
    //Update text
    textField.setText(convertToString.str());
    
    textField.setXPosition((m_window->getViewWidth() - textField.getWidth()) / 2);
    textField.setYPosition(100);
}
