/**
 *  GraphTFT plugin for the Video Disk Recorder 
 * 
 *  setup.c - A plugin for the Video Disk Recorder
 *
 *  (c) 2004 Lars Tegeler, Sascha Volkenandt  
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 * $Id: setup.c,v 1.6 2004/05/26 11:34:33 tegeler Exp $
 *
 **/

#include "common.h"
#include "setup.h"
#ifdef HAVE_DFB
#include "cache.h"
#endif

//for debugging
#ifdef DEBUG
static const char *loggers[] = {
	"none",
	"stdout",
	"syslog",
	"file"
};
#endif

cGraphTFTSetup GraphTFTSetup;

// --- cGraphTFTSetup -----------------------------------------------------------

//Constructor
cGraphTFTSetup::cGraphTFTSetup(void)
{
    Theme=0;
    Level=0;
    Logger=0;
    Iso2Utf = 1;
    ThemesFound = 0;
    HideMainMenu  = 0;
    ShowMutePermanent = 0;
    UseStillPicture = 1;
    DvbRefresh = 100;
    VdrHeight = 5;
    VdrWidht = 25;
    VdrX=0;
    VdrY=0;
    DumpImage = 0;
    DumpRefresh = 5;
    DumpImageX = 720;
    DumpImageY = 576;
    OSDOffsetX = 50;
    OSDOffsetY = 400;
    OSDMessageTime = 30;
    PluginConfPath = strdup("/tmp");
    CoverImage = NULL;

    for (int i = 0; i < MAXTHEME; i++)  //FIXME: see graphtft.c the theme and item 
										//should get own classes. so items should be
										//an list in theme. and the should contain all
										//data. so we wont need this any more.
    {
		Themes[i] = NULL;
		ThemesPath[i] = NULL;
    }
    
}

bool cGraphTFTSetup::SetupParse(const char *Name, const char *Value)
{
    // Take Cover Images
    if (!strcasecmp(Name, "CoverImage")) {
		    if (CoverImage) free(CoverImage);
		    CoverImage = strdup(Value);
    }
	
    // Parse your own setup parameters and store their values.
    else if      (!strcasecmp(Name, "Theme"))  Theme = atoi(Value);
    else if (!strcasecmp(Name, "HideMainMenu"))  HideMainMenu  = atoi(Value);
    else if (!strcasecmp(Name, "ShowMutePermanent"))  ShowMutePermanent = atoi(Value);
    else if (!strcasecmp(Name, "Iso2Utf"))  Iso2Utf  = atoi(Value);
    else if (!strcasecmp(Name, "UseStillePicture"))  UseStillPicture  = atoi(Value);
    else if (!strcasecmp(Name, "DvbRefresh"))  DvbRefresh  = atoi(Value);
    else if (!strcasecmp(Name, "VdrHeight"))  VdrHeight  = atoi(Value);
    else if (!strcasecmp(Name, "VdrWidht"))  VdrWidht  = atoi(Value);
    else if (!strcasecmp(Name, "VdrX"))  VdrX  = atoi(Value);
    else if (!strcasecmp(Name, "VdrY"))  VdrY  = atoi(Value);
    else if (!strcasecmp(Name, "DumpImage"))  DumpImage  = atoi(Value);
    else if (!strcasecmp(Name, "DumpRefresh"))  DumpRefresh  = atoi(Value);
    else if (!strcasecmp(Name, "ImageWidth"))  DumpImageX  = atoi(Value);
    else if (!strcasecmp(Name, "ImageHeight"))  DumpImageY  = atoi(Value);
    else if (!strcasecmp(Name, "OSDOffsetX"))  OSDOffsetX  = atoi(Value);
    else if (!strcasecmp(Name, "OSDOffsetY"))  OSDOffsetY  = atoi(Value);
    else if (!strcasecmp(Name, "OSDMessageTime"))  OSDMessageTime  = atoi(Value);
    else if (!strcasecmp(Name, "Logger"))  Logger  = atoi(Value);
    else if (!strcasecmp(Name, "Level"))  Level  = atoi(Value);
    else
	return false;
    return true;
}

// --- cMenuSetupGraphTFT --------------------------------------------------------

cMenuSetupGraphTFT::cMenuSetupGraphTFT(cGraphTFTDisplay *graphTFTdisplay)
{
    char *buf = NULL;

	//the setupmenu
    _data = GraphTFTSetup;
    _graphTFTdisplay = graphTFTdisplay;

    SetSection(tr("GraphTFT"));

    if (_data.Theme >= _data.ThemesFound)
	_data.Theme = 0;
    Add(new cMenuEditStraItem(tr("Theme"), &_data.Theme,  _data.ThemesFound, _data.Themes));
    Add(new cMenuEditBoolItem(tr("Hide Mainmenu Entry"),        &_data.HideMainMenu));
    Add(new cMenuEditBoolItem(tr("Show Mute Symbol permanent"), &_data.ShowMutePermanent));
    Add(new cMenuEditBoolItem(tr("Dump image to file"),        &_data.DumpImage));
    Add(new cMenuEditIntItem(tr("Dump Refresh"),        	&_data.DumpRefresh,0,600));
    Add(new cMenuEditIntItem(tr("Dump image width"),        &_data.DumpImageX,1,1024));
    Add(new cMenuEditIntItem(tr("Dump image height"),        &_data.DumpImageY,1,768));
    Add(new cMenuEditBoolItem(tr("Convert Iso to UTF-8"),        &_data.Iso2Utf));
    Add(new cMenuEditIntItem(tr("X Offset for OSD"),        &_data.OSDOffsetX,0,719));
    Add(new cMenuEditIntItem(tr("Y Offset for OSD"),        &_data.OSDOffsetY,0,575));
    Add(new cMenuEditIntItem(tr("Timeout for OSD Messages"),        &_data.OSDMessageTime,0,3600));
#ifdef HAVE_IMLIB
    asprintf(&buf, "--- %s --------------------------------------------", tr("DVB Device"));
    Add(new cOsdItem(buf));
    free(buf);
    Add(new cMenuEditBoolItem(tr("Use StillPicture"),        &_data.UseStillPicture));
    Add(new cMenuEditIntItem(tr("Refresh"),        &_data.DvbRefresh,0,1024));
    asprintf(&buf, "--- %s --------------------------------------------", tr("DVB/FB Device"));
    Add(new cOsdItem(buf));
    free(buf);
    Add(new cMenuEditIntItem(tr("X Offset"),        &_data.VdrX,0,719));
    Add(new cMenuEditIntItem(tr("Y Offset"),        &_data.VdrY,0,575));
    Add(new cMenuEditIntItem(tr("Border to Height"),        &_data.VdrHeight,0,719));
    Add(new cMenuEditIntItem(tr("Border to Width"),        &_data.VdrWidht,0,576));
#endif
#ifdef HAVE_DFB
    asprintf(&buf, "--- %s --------------------------------------------", tr("directFB Device"));
    Add(new cOsdItem(buf));
    free(buf);
#endif
#ifdef DEBUG
    asprintf(&buf, "--- %s --------------------------------------------", tr("Debug"));
    Add(new cOsdItem(buf));
    free(buf);
    Add(new cMenuEditStraItem(tr("Debug Logger"), &_data.Logger, 4, loggers));
    Add(new cMenuEditIntItem(tr("Debug Level"),        &_data.Level,0,10));
#endif 

        SetHelp(        	NULL,
                               NULL, 
                               NULL,
                               NULL
                        );

}

eOSState cMenuSetupGraphTFT::ProcessKey(eKeys Key)
{
        d(4, "cMenuSetupGraphTFT::ProcessKey()");

        eOSState state = cOsdMenu::ProcessKey(Key);

        switch (Key) {
                case kRed    : state = osUser1;
                   break;

                case kGreen  : state = osUser2;
                   break;

                case kYellow : state = osUser3;
                   break;

                case kBlue   : state = osUser4;
                   break;

                default      : break;
        }

        switch (state) {
                case osUnknown:
                        if (Key == kOk) {
#ifdef HAVE_DFB	
    					// clear image cache
    					if (GraphTFTSetup.Theme != _data.Theme)
    						imageCache.Clear();
#endif
                                Store();
                                state = osBack;
                        }
                        break;

                case osUser1:   //Red
                        //start/stop update thread
                        if ( _graphTFTdisplay->Active() )
                                _graphTFTdisplay->Stop();
                        else
                                _graphTFTdisplay->Start();
                        state = osEnd;
                        break;

                case osUser2:   //Green
                        state = osContinue;
                        break;

                case osUser3:   //Yellow
                        state = osContinue;
            break;

                case osUser4:   //Blue
                        state = osContinue;
            break;

                case osBack:    //Back
                        break;

                default:
                        break;
        }
        return state;
}




void cMenuSetupGraphTFT::Store(void)
{

    //store the setupdata
    GraphTFTSetup = _data;
    SetupStore("Theme",  GraphTFTSetup.Theme  );
    GraphTFTTheme.Load(GraphTFTSetup.ThemesPath[GraphTFTSetup.Theme]);
    SetupStore("HideMainMenu", GraphTFTSetup.HideMainMenu );
    SetupStore("ShowMutePermanent", GraphTFTSetup.ShowMutePermanent );
    SetupStore("Iso2Utf", GraphTFTSetup.Iso2Utf );
    SetupStore("UseStillPicture", GraphTFTSetup.UseStillPicture );
    SetupStore("DvbRefresh", GraphTFTSetup.DvbRefresh );
    SetupStore("VdrHeight", GraphTFTSetup.VdrHeight );
    SetupStore("VdrWidht", GraphTFTSetup.VdrWidht );
    SetupStore("VdrX", GraphTFTSetup.VdrX );
    SetupStore("VdrY", GraphTFTSetup.VdrY );
    SetupStore("DumpImage", GraphTFTSetup.DumpImage );
    SetupStore("DumpRefresh", GraphTFTSetup.DumpRefresh );
    SetupStore("DumpImageX", GraphTFTSetup.DumpImageX );
    SetupStore("DumpImageY", GraphTFTSetup.DumpImageY );
    SetupStore("OSDOffsetX", GraphTFTSetup.OSDOffsetX );
    SetupStore("OSDOffsetY", GraphTFTSetup.OSDOffsetY );
    SetupStore("OSDMessageTime", GraphTFTSetup.OSDMessageTime );
    SetupStore("Logger", GraphTFTSetup.Logger );
    SetupStore("Level", GraphTFTSetup.Level );
}
