/*
 * hattrick.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
 *
 * Copyright: See COPYING file that comes with this distribution
 */


#include <vector>
#include <vdr/interface.h>
#include <vdr/menu.h>
#include <vdr/plugin.h>

#include "setup_favourites.h"
#include "debug.h"
#include "settings.h"
#include <map>
#include "net_hattrickconnector.h"

// --- cSetupFavourites ---------------------------------------------------------


cSetupFavourites::cSetupFavourites(void):cOsdMenu( tr("enter your favourite teams"), 20)
{
    // OSD-Elemente aufbauen
    strncpy(newfavourite, "", 511);
    newIsFavour_ownteam = cSettings::GetInstance().IsFavour_ownteam();
    BuildItems();
}


void cSetupFavourites::BuildItems()
{

    SetHelp(0, 0, tr("Remove"));
    Add (new cMenuEditStrItem (tr("New entry"),
                               newfavourite, sizeof(newfavourite), FileNameChars) );
    Add ( new cMenuEditBoolItem ( tr ( "add own team automatically?" ),
                                  &newIsFavour_ownteam, tr ( "no" ), tr ( "yes" ) ) );

    std::list<std::string> FavouriteList;
    FavouriteList = cSettings::GetInstance( ).GetFavouriteList( );

    for(std::list<std::string>::iterator it = FavouriteList.begin(); it != FavouriteList.end(); it++)
    {
        std::string it_string = *it;

        Add(new cOsdItem(it_string.c_str() ));
        Display( );
    }

}


eOSState cSetupFavourites::ProcessKey(eKeys Key)
{
    std::map<long, cTeamData> TeamdataMap = cSettings::GetInstance( ).GetTeamContainer( );
    std::map<const long, cTeamData>::iterator el;
    cHattrickConnector hc;
    cTeamData teamdata;
    long TeamID;
    std::list<std::string> FavouriteList;
    eOSState state = cOsdMenu::ProcessKey(Key);

    switch (state)
    {
    case osUnknown:
        switch (Key)
        {
        case kOk:
            if( strcmp( newfavourite, "" ) ) // pruefen, ob eine neue Eingabe stattgefunden hat
            {
                FavouriteList = cSettings::GetInstance().GetFavouriteList();
                FavouriteList.push_back(newfavourite);
                cSettings::GetInstance().SetFavouriteList(FavouriteList);

                // Optionen in cSettings speichern:
                cSettings::GetInstance().SetIsFavour_ownteam(newIsFavour_ownteam);

                if(cSettings::GetInstance( ).IsTimer())
                {
                    hc.Login( cSettings::GetInstance().GetUsername(), cSettings::GetInstance().GetSecuritycode() );
                    ////////////////////////////////////////////////////////////////////////
                    //Neues Lieblingsteam gleich in Live hinzufuegen:
                    hc.SearchTeam( TeamID, newfavourite );
                    hc.GetNextMatch( TeamID, teamdata.NextMatchID,
                                     teamdata.time_nextMatch,
                                     teamdata.HomeTeamName, teamdata.HomeTeamID,
                                     teamdata.AwayTeamName, teamdata.AwayTeamID );

                    // Eintrag mit Index MatchID in den TeamContainer mit bereits gewonnenen Daten:
                    TeamdataMap[ teamdata.NextMatchID ] = teamdata;
                    cSettings::GetInstance().SetTeamContainer(TeamdataMap);

                    // if( Timer
                    hc.Live_addMatch( teamdata.NextMatchID );

                    // eigenes Team automatisch einbinden?:
                    if(newIsFavour_ownteam)
                    {
                        hc.GetNextMatch( 0, teamdata.NextMatchID,
                                         teamdata.time_nextMatch,
                                         teamdata.HomeTeamName, teamdata.HomeTeamID,
                                         teamdata.AwayTeamName, teamdata.AwayTeamID );

                        // Eintrag mit Index MatchID in den TeamContainer mit bereits gewonnenen Daten:
                        TeamdataMap[ teamdata.NextMatchID ] = teamdata;
                        cSettings::GetInstance().SetTeamContainer(TeamdataMap);

                        // if( Timer
                        hc.Live_addMatch( teamdata.NextMatchID );
                    }
                } //If-Abfrage IsTimer
                ////////////////////////////////////////////////////////////////////////
            } // if_newfavourite==0

            state = osBack;
            break;

        case kYellow:
            FavouriteList = cSettings::GetInstance().GetFavouriteList();
            FavouriteList.remove(Get(Current())->Text());
            cSettings::GetInstance().SetFavouriteList(FavouriteList);
            Del(Current());  /// Im aktuellen OSD ebenfalls entfernen
            Display();
            break;


        default:
            break;
        }
        break;
    case osBack:

        break;
    default:
        break;
    }
    return state;
}
