#include <vdr/plugin.h>

#include "channelmenu.h"
#include "channelswitcher.h"
#include "channelsetup.h"

#include <string.h>
#include <unistd.h>

cSwitchConfig config;

cSwitcher::cSwitcher( void )
{
}

cSwitcher::~cSwitcher( void )
{
}

cOsdObject *cSwitcher::MainMenuAction( void )
{
    return new cSwitchMenu( this );
}

bool cSwitcher::Initialize( void )
{
    return true;
}

cMenuSetupPage *cSwitcher::SetupMenu( void )
{
    return new cSwitchSetup(  );
}

bool cSwitcher::SetupParse( const char *name, const char *value )
{
    // Parse setup parameters and store their values.
    if ( !strcasecmp( name, "startConf" ) ) config.startConf = atoi( value );
    else if ( !strcasecmp( name, "channelsConf" ) ) config.channelsConf = value;
    else
        return false;
    return true;
}

bool cSwitcher::Start( void )
{
    char *ToLoad = NULL;
    
    // Check whether we want to load last channels.conf, filename is not NULL and is not "Default"
    if ( config.startConf && ( config.channelsConf != NULL ) && ( strcmp( config.channelsConf,  "Default" ) != 0 ) )
        {
            // Add full path to filename
            asprintf( &ToLoad, "%s/../%s", cPlugin::ConfigDirectory(  ), config.channelsConf );
            
            // Check file presence
            if ( ( access( ToLoad, F_OK ) == 0 ) )
                {
                    // Load selected channels.conf
                    Channels.Load( ToLoad );
                    free( ToLoad );
                }
            }
    return true;
}

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