/*
 * icetft plugin for VDR
 *
 * setup.c - class for icetft plugin setup menu
 *
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 */

#include "setup.h"

cIcetftSetup IcetftSetup;

// --- cIcetftSetup ------------------------------------------------------------

cIcetftSetup::cIcetftSetup(void)
{
    stateAtStartup = 0;
    stateAtShutdown = 1;
    hidemenu=0;
    wakeup=0;
    poweroff=0;
    message=1;
    poweroffTime=3600;
}


bool cIcetftSetup::Parse(const char *Name, const char *Value)
{
    if (!strcasecmp(Name, "Icetft_stateatstartup"))  IcetftSetup.stateAtStartup = atoi(Value);
    else if (!strcasecmp(Name, "Icetft_stateatshutdown")) IcetftSetup.stateAtShutdown = atoi(Value);
    else if (!strcasecmp(Name, "Icetft_hidemenu")) IcetftSetup.hidemenu = atoi(Value);
    else if (!strcasecmp(Name, "Icetft_wakeup")) IcetftSetup.wakeup = atoi(Value);
    else if (!strcasecmp(Name, "Icetft_poweroff")) IcetftSetup.poweroff = atoi(Value);
    else if (!strcasecmp(Name, "Icetft_powerofftime")) IcetftSetup.poweroffTime = atoi(Value);
    else if (!strcasecmp(Name, "Icetft_message")) IcetftSetup.message = atoi(Value);
    else return false;
    return true;
}


// --- cMenuIcetftSetup -------------------------------------------------------

cMenuIcetftSetup::cMenuIcetftSetup(void)
{
    SetupPage();
}


void cMenuIcetftSetup::SetupPage()
{
    int current = Current();
    Clear();

    Add(new cMenuEditBoolItem( tr("Hide main menu entry"), &IcetftSetup.hidemenu, tr("no"), tr("yes")));
    Add(new cMenuEditBoolItem( tr("TFT power on at startup"), &IcetftSetup.stateAtStartup, tr("no"), tr("yes")));
    Add(new cMenuEditBoolItem( tr("TFT power down at shutdown"), &IcetftSetup.stateAtShutdown, tr("no"), tr("yes")));
    Add(new cMenuEditBoolItem( tr("Power off"), &IcetftSetup.poweroff, tr("no"), tr("yes")));
    if (IcetftSetup.poweroff)                   
    {
        Add(new cMenuEditIntItem( tr("Timeout for power off"), &IcetftSetup.poweroffTime, 5, 32000));
    }
    Add(new cMenuEditBoolItem( tr("Wake up"), &IcetftSetup.wakeup, tr("no"), tr("yes")));
    Add(new cMenuEditBoolItem( tr("Message"), &IcetftSetup.message, tr("no"), tr("yes")));

    SetCurrent(Get(current));
    Display();
}


eOSState cMenuIcetftSetup::ProcessKey(eKeys key)
{
    int oldPoweroff = IcetftSetup.poweroff;
    eOSState state = cMenuSetupPage::ProcessKey(key);

    if (key != kNone && (IcetftSetup.poweroff != oldPoweroff))
    {
        SetupPage();
    }

    return state;
}


void cMenuIcetftSetup::Store(void)
{
    SetupStore("Icetft_hidemenu", IcetftSetup.hidemenu);
    SetupStore("Icetft_stateatstartup", IcetftSetup.stateAtStartup);
    SetupStore("Icetft_stateatshutdown", IcetftSetup.stateAtShutdown);
    SetupStore("Icetft_poweroff", IcetftSetup.poweroff);
    SetupStore("Icetft_powerofftime", IcetftSetup.poweroffTime);
    SetupStore("Icetft_wakeup", IcetftSetup.wakeup);
    SetupStore("Icetft_message", IcetftSetup.message);
}
