//
// C++ Interface: userinterface
//
// Description:
//
//
// Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "settings.h"
#include "osd_userinterface.h"
#include "osd_userinterface_matchdetails.h"
#include "debug.h"
#include "net_hattrickconnector.h"


cUserInterface::cUserInterface(void)
        :cOsdMenu("Hattrick", 37, 5), cThread("Hattrick_Osd_Timer")
{
    // Wenn die Timerfunktion deaktiviert wurde,
    // muessen jetzt die Daten von hattrick.org geladen werden.
    if(!cSettings::GetInstance( ).IsTimer())
    {
        cHattrickConnector hc;
        hc.LoadHattrickData( );
    }

    cCondWait::SleepMs(250); // Etwas Zeit einplanen, damit in BuildMenu die Daten bereitstehen und angezeigt werden k�nnen
    BuildMenu();
    // Thread starten, der die MetaDaten aktualisiert
    Activate( true );
}


cUserInterface::~cUserInterface()
{
    Activate(false);
}


void cUserInterface::BuildMenu()
{
rlog << "cUserInterface::BuildMenu()";

    std::map<long, cTeamData> TeamdataMap;
    std::map<const long, cTeamData>::iterator el;
    cTeamData teamdata;

    TeamdataMap = cSettings::GetInstance( ).GetTeamContainer( );

    if(TeamdataMap.empty( ) )
        return;

    for (el = TeamdataMap.begin(); el != TeamdataMap.end(); ++el)
    {
        teamdata = el->second;

        char buff[4096];
        snprintf(buff, sizeof(buff), "%s-%s\t%d-%d\t(%d.)",
                 teamdata.HomeTeamName.c_str(), teamdata.AwayTeamName.c_str(),
                 teamdata.HomeTeamGoals, teamdata.AwayTeamGoals,
                 teamdata.TimePlayed);

        Add( new cOsdItem( buff ) );
    }

    Display();
}


eOSState cUserInterface::ProcessKey(eKeys Key)
{
    eOSState state = cOsdMenu::ProcessKey(Key);

    switch (Key)
    {
    case kOk:
        {
            // Wir lassen uns den Text der aktuellen Cursorposition uebergeben und
            // vergleichen in einer for-Schleife die Vorkommnise mit el-first aus
            // TeamContainer und uebergeben letzteres an das neue Untermenue.

            std::map<long, cTeamData> TeamdataMap = cSettings::GetInstance( ).GetTeamContainer( );
            std::map<const long, cTeamData>::iterator el;

            for (el = TeamdataMap.begin(); el != TeamdataMap.end(); ++el)
            {
                std::string s = Get( Current() )->Text();
                std::string::size_type loc = s.find( el->second.HomeTeamName, 0 );

                if( loc != std::string::npos )
                {
                    AddSubMenu( new cUserInterface_matchdetails( el->first ) );
                }
            }
            break;
        }
        return osContinue;
    default:
        break;
    }

    return state;
}


// --- Thread -----------------------
void cUserInterface::Activate(bool On)
{
    if (On)

        Start();
    else
    {
        Cancel( );
    }
}


void cUserInterface::Action()
{
    while ( Running() )
    {
        cCondWait::SleepMs(REFRESHINTERVAL);

 ////       cLogger::Out( "OSDTIMER" );
        Clear();
        BuildMenu();
        Display();
    }

}

