/*
 * 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 "setup.h"
#include "settings.h"
#include "debug.h"
#include "osd_userinterface.h"
#include "net_hattrickconnector.h"
#include "timer.h"
#include <vdr/plugin.h>
#include <string>
#include "i18n.h"

static const char *VERSION        = PLUGIN_VERSION;
static const char *DESCRIPTION    = "This is a match viewer for the online game hattrick";
static const char *MAINMENUENTRY  = "Hattrick";

class cPluginHattrick : public cPlugin
{
private:
    // Add any member variables or functions you may need here.
    cTimerThread *Timer;
public:
    cPluginHattrick(void);
    virtual ~cPluginHattrick();
    virtual const char *Version(void)
    {
        return VERSION;
    }
    virtual const char *Description(void)
    {
        return tr(DESCRIPTION);
    }
    virtual const char *CommandLineHelp(void);
    virtual bool ProcessArgs(int argc, char *argv[]);
    virtual bool Initialize(void);
    virtual bool Start(void);
    virtual void Stop(void);
    virtual void Housekeeping(void);
    virtual void MainThreadHook(void);
    virtual cString Active(void);
    virtual time_t WakeupTime(void);
    virtual const char *MainMenuEntry(void)
    {
        return MAINMENUENTRY;
    }
    virtual cOsdObject *MainMenuAction(void);
    virtual cMenuSetupPage *SetupMenu(void);
    virtual bool SetupParse(const char *Name, const char *Value);
    virtual bool Service(const char *Id, void *Data = NULL);
    virtual const char **SVDRPHelpPages(void);
    virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
};

cPluginHattrick::cPluginHattrick(void)
{
    // Initialize any member variables here.
    // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
    // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
}

cPluginHattrick::~cPluginHattrick()
{
    // Clean up after yourself!
}

const char *cPluginHattrick::CommandLineHelp(void)
{
    // Return a string that describes all known command line options.
    return NULL;
}

bool cPluginHattrick::ProcessArgs(int argc, char *argv[])
{
    // Implement command line argument processing here if applicable.
    return true;
}


bool cPluginHattrick::Initialize(void)
{
    // Initialize any background activities the plugin shall perform.

    RegisterI18n(Phrases);

    return true;
}


bool cPluginHattrick::Start(void)
{
    // Start any background activities the plugin shall perform.
	rlog << "Starting...";
	
    // Der Timer soll immer initialisiert werden und auch immer geloescht werden;
    // das soll eine weitere, globale Verwendung, z.B. aus dem Setup heraus, ermoeglichen.
    Timer = cSettings::GetInstance().SetTimer( new cTimerThread() );

    if( cSettings::GetInstance( ).IsTimer() )
    {
        // TODO: auf neue Matches pruefen:
        // Erst kurz vor Spielbeginn starten (ansonsten wird der Timer :
        ////   Timer = new cTimerThread();
        Timer->Init();
    }
    
	rlog << "...Started";

    return true;
}


void cPluginHattrick::Stop(void)
{
    // Stop any background activities the plugin shall perform.
    rlog << "cPluginHattrick::Stop";
    
    Timer->Stop();
    Timer = 0;
    cHattrickConnector hc;
    hc.Logout();
}

void cPluginHattrick::Housekeeping(void)
{
    // Perform any cleanup or other regular tasks.
}

void cPluginHattrick::MainThreadHook(void)
{
    // Perform actions in the context of the main program thread.
    // WARNING: Use with great care - see PLUGINS.html!
}

cString cPluginHattrick::Active(void)
{
    // Return a message string if shutdown should be postponed
    return NULL;
}

time_t cPluginHattrick::WakeupTime(void)
{
    // Return custom wakeup time for shutdown script
    return 0;
}

cOsdObject *cPluginHattrick::MainMenuAction(void)
{
    // Perform the action when selected from the main VDR menu.

    /// Wenn noch kein User/Sicherheitscode definiert wurden, soll eine Info darauf hinweisen:
    if ( 	!strcmp(cSettings::GetInstance().GetUsername(), "") ||
            !strcmp(cSettings::GetInstance().GetSecuritycode(), "") )
        Skins.Message(mtError, tr("Please provide user information in setup of vdr-hattrick."));
    else
        return new cUserInterface;

    return NULL;
}

cMenuSetupPage *cPluginHattrick::SetupMenu(void)
{
    // Return a setup menu in case the plugin supports one.
    return new cSetupHattrick;
}

bool cPluginHattrick::SetupParse(const char *Name, const char *Value)
{
    // Parse your own setup parameters and store their values.

    std::list<std::string> FavouriteList;

    if (std::string(Name) == "username")
    {
        cSettings::GetInstance().SetUsername(Value);
    }

    if (std::string(Name) == "securitycode")
    {
        cSettings::GetInstance().SetSecuritycode(Value);
    }

    if (std::string(Name) == "Favour_ownteam")
    {
        cSettings::GetInstance().SetIsFavour_ownteam( ( atoi( Value ) ) );
    }

    if (std::string(Name) == "IsTimer")
    {
        cSettings::GetInstance().SetIsTimer( ( atoi( Value ) ) );
    }

    if (std::string(Name) == "IsDebugmode")
    {
        cSettings::GetInstance().SetIsDebugmode( ( atoi( Value ) ) );
    }

    for(int i=0;i<MAXFAVS;i++)
    {
        char buff[32];
        snprintf(buff,sizeof (buff), "Favourite%d", i);
        if (!strcasecmp(Name, buff ))
        {
            FavouriteList = cSettings::GetInstance().GetFavouriteList();
            FavouriteList.push_back(Value);
            cSettings::GetInstance().SetFavouriteList(FavouriteList);
        }
    }

    if ( 	!(std::string(Name) == "username") ||
            !(std::string(Name) == "password_md5") ||
            !(std::string(Name) == "show_osdatstartup") )
        return false;

    return true;
}

bool cPluginHattrick::Service(const char *Id, void *Data)
{
    // Handle custom service requests from other plugins
    return false;
}

const char **cPluginHattrick::SVDRPHelpPages(void)
{
    // Return help text for SVDRP commands this plugin implements
    return NULL;
}

cString cPluginHattrick::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
    // Process SVDRP commands this plugin implements
    return NULL;
}

VDRPLUGINCREATOR(cPluginHattrick); // Don't touch this!
