//  SuperTuxKart - a fun racing game with go-kart
//  Copyright (C) 2009-2015 Marianne Gagnon
//
//  This program 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.
//
//  This program 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 this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#define DEBUG_MENU_ITEM 0

#include "map_selection_screen.h"
#include "main_menu_screen.h"




#include "../config/user_config.h"
#include "../graphics/irr_driver.h"
#include "../guiengine/scalable_font.h"
#include "../guiengine/widgets/label_widget.h"
#include "../guiengine/widgets/list_widget.h"
#include "../maps/map.h"
#include "../maps/map_manager.h"
#include "../game/game_manager.h"

#include "../input/device_manager.h"
#include "../input/input_manager.h"
#include "../input/keyboard_device.h"
#include "../io/file_manager.h"
#include "../main_loop.h"


#include "state_manager.h"

//#include "states_screens/dialogs/message_dialog.hpp"

#include "../utils/string_utils.h"



#include <string>



using namespace GUIEngine;

DEFINE_SCREEN_SINGLETON( MapSelectionScreen );


// ----------------------------------------------------------------------------

MapSelectionScreen::MapSelectionScreen() : Screen("map_selection.lsgui")
{
}   // MapSelectionScreen

// ----------------------------------------------------------------------------

void MapSelectionScreen::loadedFromFile()
{



}   // loadedFromFile

void MapSelectionScreen::beforeAddingWidget()
{
    //Log::info("mapselection", "beforeAddingWidget");
    GUIEngine::ListWidget* w_list =
        getWidget<GUIEngine::ListWidget>("maps");
    assert(w_list != NULL);
    w_list->clearColumns();
    w_list->addColumn(("Map"), 1);
    w_list->setSortable(false);
}

// ----------------------------------------------------------------------------
//
void MapSelectionScreen::init()
{
    Screen::init();

    m_must_delete_on_back = false;

// loading map-list
//to do: write function: input none
    const std::string map_file =
        file_manager->getAssetChecked(FileManager::MAP, "maps.xml");

    if(!file_manager->fileExists(map_file))
        return;

    std::vector<std::string> map_data;

    XMLNode *node           = file_manager->createXMLTree(map_file);

    std::vector<XMLNode*> map_nodes;

    node->getNodes("map", map_nodes);

    for (unsigned int i = 0; i < map_nodes.size(); i++)
    {
        std::string map_name;
        map_nodes[i]->get("name", &map_name);



        map_data.push_back( map_name.c_str() );

    }

    std::vector<std::string> mapidents = map_manager->getAllMapIdentifiers();
    //Log::warn("[map selection]", "getting map-idents");
    for (int i = 0; i < mapidents.size(); i++)
    {
        //Log::warn("[map selection]", "...'%s'",mapidents.at(i).c_str());

    }



    GUIEngine::ListWidget* maps =
    getWidget<GUIEngine::ListWidget>("maps");
    assert( maps != NULL );

    for (int i = 0; i <map_data.size(); i++)
    {
    addListItems(maps, "maps", map_data.at(i).c_str());
    //Log::info("[map selection/ map manager]", "...'%s'",map_data.at(i).c_str());
    }






}   // init

// -----------------------------------------------------------------------------

void MapSelectionScreen::addListItems(GUIEngine::ListWidget* actions,
                                               const char* id,
                                               const core::stringw& text)
{
    std::vector<GUIEngine::ListWidget::ListCell> row;
    row.push_back(GUIEngine::ListWidget::ListCell(text, -1, 1, false));
    row.push_back(GUIEngine::ListWidget::ListCell(L"", -1, 1, false));
    actions->addItem(id, row);
}   // addListItemSubheader




// ----------------------------------------------------------------------------
void MapSelectionScreen::onUpdate(float delta)

{


}   // onUpdate

// ----------------------------------------------------------------------------

void MapSelectionScreen::eventCallback(Widget* widget, const std::string& name,
                                   const int playerID)
{

    if(name=="back_to_main_menu")
    {
        //Log::warn("mss", "eventcallback map selection screen");
        StateManager::get()->escapePressed();
        return;
    }
    if(name=="ok")
    {
        Log::warn("MapSelectionScreen::eventCallback", "Pressed start");
        // use default test-map

        Map* default_map = map_manager->getMap(0); // needed to get map

        // use game_manager with test-map

        game_manager->startSingleGame(default_map->getIdent());


        default_map->loadMapModel(); // test to load the map failed
        scene::ICameraSceneNode * cam = irr_driver->addCameraSceneNode();

        // test map-loading

        // use selected map

        // check if selected map exists


        return;
    }



}   // eventCallback

// ----------------------------------------------------------------------------

void MapSelectionScreen::tearDown()
{
    Screen::tearDown();

}   // tearDown

// ----------------------------------------------------------------------------

void MapSelectionScreen::onDisabledItemClicked(const std::string& item)
{

}   // onDisabledItemClicked

bool MapSelectionScreen::onEscapePressed()
{
    m_must_delete_on_back = true; // delete the screen

    return true;
}
