/*
 * 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 "settings.h"
#include "setup.h"
#include "debug.h"
#include "setup_favourites.h"
#include "setup_info.h"
#include "net_hattrickconnector.h"
#include <map>

cSetupHattrick::cSetupHattrick ( void )
{
    /// Variablen auf Standardwerte setzen, nullen:
    strcpy ( newusername, cSettings::GetInstance().GetUsername() );
    newIsTimer = cSettings::GetInstance().IsTimer();
    newIsDebugmode = cSettings::GetInstance().IsDebugmode();

    /// Menue zeichnen:
    BuildOsdItems();
}


void cSetupHattrick::BuildOsdItems()
{
    /// newpassword leeren, damit es nicht im Menue als Klartext angezeigt wird.
    memset ( &newsecuritycode, 0, sizeof ( newsecuritycode ) );

    Add ( new cOsdItem          ( hk( tr( "favourites..." ) ) ) );
    Add ( new cMenuEditStrItem  ( tr( "username" ),
                                  newusername, sizeof ( newusername ), FileNameChars ) );
    Add ( new cMenuEditStrItem  ( tr( "securitycode" ),
                                  newsecuritycode, sizeof ( newsecuritycode ), FileNameChars ) );
    Add ( new cMenuEditBoolItem ( tr( "update automatically?" ),
                                  &newIsTimer, tr ( "no" ), tr ( "yes" ) ) );
    Add ( new cMenuEditBoolItem ( tr( "debugmode?" ),
                                  &newIsDebugmode, tr ( "no" ), tr ( "yes" ) ) );
    Add ( new cOsdItem          ( hk( tr( "info..." ) ) ) );
}


void cSetupHattrick::Store ( void )
{
    int c = 0;
    std::list<std::string> FavouriteList;
    FavouriteList = cSettings::GetInstance( ).GetFavouriteList( );

    rlog << "STORING...";

    if ( newusername != cSettings::GetInstance().GetUsername() )
    {
        cSettings::GetInstance().SetUsername ( newusername );
        SetupStore ( "username", cSettings::GetInstance().GetUsername() );
    }

    // Passwort nicht mitspeichern, wenn es leer gelassen wurde.
    if ( strcmp ( newsecuritycode, "" ) != 0 )
    {
        cSettings::GetInstance().SetSecuritycode ( newsecuritycode );
        SetupStore ( "securitycode", cSettings::GetInstance().GetSecuritycode() );
    }

    if ( newIsTimer != cSettings::GetInstance().IsTimer() )
    {
        cSettings::GetInstance().SetIsTimer ( newIsTimer );
        SetupStore ( "IsTimer", newIsTimer );
        // Timer starten/stoppen:
        if(!cSettings::GetInstance().GetTimer()->Active() )
            cSettings::GetInstance().GetTimer()->Init();
        else
            cSettings::GetInstance().GetTimer()->Stop();
    }

    if ( newIsDebugmode != cSettings::GetInstance().IsDebugmode() )
    {
        cSettings::GetInstance().SetIsDebugmode ( newIsDebugmode );
        SetupStore ( "IsDebugmode", newIsDebugmode );
    }

    SetupStore ( "Favour_ownteam", cSettings::GetInstance().IsFavour_ownteam() );

    // Im Folgenden werden die moeglichen (MAXFAVS) Favourite-Eintraege der setup.conf entfernt;
    // so werden doppelte Eintraege und Luecken vermieden.
    c = 0;
    for(int i = 0; i < MAXFAVS; i++)
    {
        char buff[32];
        snprintf(buff,sizeof (buff), "Favourite%d", i);
        SetupStore ( buff );
        
        c++;
    }

    c = 0;
    for(std::list<std::string>::iterator it = FavouriteList.begin(); it != FavouriteList.end(); it++)
    {
        std::string it_string = *it;
        char buff[32];
        snprintf(buff,sizeof (buff), "Favourite%d", c);

        if( strcmp( it_string.c_str(), "" ) )
        {
            SetupStore(buff, it_string.c_str());
        }
        
        c++;
    } // for-Schleife FavouriteList

}

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

    switch ( state )
    {
    case osUnknown:
        switch ( Key )
        {
        case kOk:
            if ( !strcmp ( Get ( Current() )->Text(),tr ( "favourites..." ) ) )
            {
                return AddSubMenu ( new cSetupFavourites );
            }
            else if ( !strcmp ( Get ( Current() )->Text(),tr ( "info..." ) ) )
            {
                return AddSubMenu ( new cSetupInfo );
            }
            Store();
            state = osBack;
            break;
        default:
            break;
        }
        break;
    case osBack:
        Store();
        state = osBack;
        break;

    default:
        break;
    }

    return state;
}
