/*
 * extb plugin to VDR (C++)
 *
 * (C) 2004 - 2006 Andreas Brachold <vdr04-at-deltab-dot-de>
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "setup.h"
#include "i18n.h"

#include <vdr/tools.h>

/// The one and only Stored setup data
cExtbSetup theSetup;
const char * cExtbSetup::m_szMuteModes[cExtbSetup::eMuteModes_Cnt];
const char * cExtbSetup::m_szIOModes[cExtbSetup::eIOModes_Cnt];



cIOFeature::cIOFeature(void) {
   for(unsigned int m=0;m<memberof(nState);++m) {
     nState[m] = cExtbSetup::eIOModes_Unused;
  	}
   nReverse = 0;
}

cIOFeature& cIOFeature::operator = (const cIOFeature& x) {
 for(unsigned int m=0;m<memberof(nState);++m) {
   nState[m] = x.nState[m];
  }
  
  nReverse = x.nReverse;
  return *this;
}


cLEDFeature::cLEDFeature(void) 
: cIOFeature() {

  for(unsigned int m=0;m<memberof(nFlash);++m) {
   nFlash[m] = cExtbSetup::eIOModes_Unused;
  }
  
  nFlashReverse = 0;
  nFlashImportant = 1;
}

cLEDFeature& cLEDFeature::operator = (const cLEDFeature& x) {

  cIOFeature::operator = (x);

  for(unsigned int m=0;m<memberof(nFlash);++m) {
   nFlash[m] = x.nFlash[m];
  }

  nFlashReverse = x.nFlashReverse;
  nFlashImportant = x.nFlashImportant;
  return *this;
}




/// Ctor, load default values
cExtbSetup::cExtbSetup(void)
{
  m_bHideMainMenuEntry = 0;
  m_nMuteMode = eMuteModes_AnalogDigital; 
  
  strn0cpy(m_szStartUp,"M1 M1",sizeof(m_szStartUp));
  strn0cpy(m_szShutDown,"M2",sizeof(m_szShutDown));
}

cExtbSetup::cExtbSetup(const cExtbSetup& x)
{
  *this = x;
}

cExtbSetup& cExtbSetup::operator = (const cExtbSetup& x)
{
  for(unsigned int n=0;n<memberof(m_ioLEDMode);++n) 
    m_ioLEDMode[n] = x.m_ioLEDMode[n];
  for(unsigned int n=0;n<memberof(m_ioOutputMode);++n) 
    m_ioOutputMode[n] = x.m_ioOutputMode[n];

  m_bHideMainMenuEntry = x.m_bHideMainMenuEntry;
  m_nMuteMode = x.m_nMuteMode; 
  strn0cpy(m_szStartUp,x.m_szStartUp,sizeof(m_szStartUp));
  strn0cpy(m_szShutDown,x.m_szShutDown,sizeof(m_szShutDown));

  return *this;
}

/// compare profilenames and load there value 
bool cExtbSetup::SetupParse(const char *szName, const char *szValue)
{
  unsigned int m;
  char sz[16];

  // HideMainMenuEntry
  if(!strcasecmp(szName, "HideMainMenuEntry")) {
    m_bHideMainMenuEntry = atoi(szValue) == 0?0:1;
    return true;
  }

  // MuteMode
  if(!strcasecmp(szName, "MuteMode")) {
    m_nMuteMode          = atoi(szValue) % eMuteModes_Cnt;
    return true;
  }

  const char* szDelim = ", ";
  // search parameter for all LEDs LEDMode1,LEDMode2...
  if(!strncasecmp(szName,"LEDMode",7)) {
    for(unsigned int n=0;n<memberof(m_ioLEDMode);++n) {
        snprintf(sz,sizeof(sz),"LEDMode%d",n + 1);  
        if(!strcasecmp(szName, sz)) {
          if(szValue){
            char* sbuf = strdup(szValue);
            if( sbuf ) {
              char* rbuf = strdup(sbuf);
              char* tmp = rbuf;
        
              if( rbuf ) {
                m = 0;
                char* next = strtok_r(sbuf, szDelim, &tmp);
                while(next) {
                  // parse "1,2,reverse" or "1,2" or only "1"
                  if(!strcasecmp(next,"reverse")) 
                    m_ioLEDMode[n].nReverse           = 1;
                  else if (m>=memberof(m_ioLEDMode[n].nState))
                        break;
                  else {
                      m_ioLEDMode[n].nState[m]        = atoi(next) % eIOModes_Cnt;
                      if(m_ioLEDMode[n].nState[m] != eIOModes_Unused)
                        ++m;
                  }
                  // check for next token
                  next = strtok_r(0, szDelim, &tmp);
                }
                free(rbuf);
              }
              free(sbuf);
            }
          }
          return true;
        }
    }
  }
  // search parameter for all LEDs FlashMode1,FlashMode2...
  if(!strncasecmp(szName,"FlashMode",7)) {
    for(unsigned int n=0;n<memberof(m_ioLEDMode);++n) {
        snprintf(sz,sizeof(sz),"FlashMode%d",n + 1);  
        if(!strcasecmp(szName, sz)) {
          if(szValue){
            m_ioLEDMode[n].nFlashImportant = 0;
            char* sbuf = strdup(szValue);
            if( sbuf ) {
              char* rbuf = strdup(sbuf);
              char* tmp = rbuf;
        
              if( rbuf ) {
                m = 0;
                char* next = strtok_r(sbuf, szDelim, &tmp);
                while(next) {
                  // parse "1,2,reverse" or "1,2" or only "1"
                  if(!strcasecmp(next,"reverse")) 
                    m_ioLEDMode[n].nFlashReverse           = 1;
                  else if(!strcasecmp(next,"important")) 
                    m_ioLEDMode[n].nFlashImportant         = 1;
                  else if (m>=memberof(m_ioLEDMode[n].nFlash))
                        break;
                  else {
                      m_ioLEDMode[n].nFlash[m]        = atoi(next) % eIOModes_Cnt;
                      if(m_ioLEDMode[n].nFlash[m] != eIOModes_Unused)
                        ++m;
                  }
                  // check for next token
                  next = strtok_r(0, szDelim, &tmp);
                }
                free(rbuf);
              }
              free(sbuf);
            }
          }
          return true;
        }
    }
  }

  // search parameter for all Outputs (Output1,Output2...)
  if(!strncasecmp(szName,"Output",7)) {
    for(unsigned int n=0;n<memberof(m_ioOutputMode);++n) {
        snprintf(sz,sizeof(sz),"Output%d",n + 1);  
        if(!strcasecmp(szName, sz)) {
          if(szValue){
            char* sbuf = strdup(szValue);
            if( sbuf ) {
              char* rbuf = strdup(sbuf);
              char* tmp = rbuf;
        
              if( rbuf ) {
                m = 0;
                char* next = strtok_r(sbuf, szDelim, &tmp);
                while(next) {
                  // parse "1,2,reverse" or "1,2" or only "1"
                  if(!strcasecmp(next,"reverse")) 
                    m_ioOutputMode[n].nReverse           = 1;
                  else if (m>=memberof(m_ioOutputMode[n].nState))
                        break;
                  else {
                      m_ioOutputMode[n].nState[m]        = atoi(next) % eIOModes_Cnt;
                      if(m_ioOutputMode[n].nState[m] != eIOModes_Unused)
                        ++m;
                  }
                  // check for next token
                  next = strtok_r(0, szDelim, &tmp);
                }
                free(rbuf);
              }
              free(sbuf);
            }
          }
          return true;
        }
    }
  }

  // StartUp
  if(!strcasecmp(szName, "Startup")) {
    strn0cpy(m_szStartUp,szValue,sizeof(m_szStartUp));
    return true;
  }

  // ShutDown
  if(!strcasecmp(szName, "Shutdown")) {
    strn0cpy(m_szShutDown,szValue,sizeof(m_szShutDown));
    return true;
  }

  //Unknow parameter
  return false;
}


// Assign parameter and translated String
void cExtbSetup::LoadTranslatedStings(void)
{
  m_szMuteModes[eMuteModes_Analog]           = tr("only analog");
  m_szMuteModes[eMuteModes_Digital]          = tr("only digital");
  m_szMuteModes[eMuteModes_AnalogDigital]    = tr("analog/digital");
  m_szMuteModes[eMuteModes_Unsed]            = tr("Unused");

  m_szIOModes[eIOModes_Unused]             = tr("Unused");
  m_szIOModes[eIOModes_Off]                = tr("off");
  m_szIOModes[eIOModes_AlwaysOn]           = tr("Always on");
  m_szIOModes[eIOModes_AnyRecording]       = tr("Any Recording");
  m_szIOModes[eIOModes_AllCardsRecording]  = tr("All Cards Recording");
  m_szIOModes[eIOModes_RecordingOnCard1]   = tr("1st Card Recording");
  m_szIOModes[eIOModes_RecordingOnCard2]   = tr("2nd Card Recording");
  m_szIOModes[eIOModes_RecordingOnCard3]   = tr("3rd Card Recording");
  m_szIOModes[eIOModes_RecordingOnCard4]   = tr("4th Card Recording");
  m_szIOModes[eIOModes_AnyReplay]          = tr("Any replay");
  m_szIOModes[eIOModes_ReplayVDR]          = tr("VDR replay");
  m_szIOModes[eIOModes_ReplayDVD]          = tr("DVD replay");
  m_szIOModes[eIOModes_ReplayMP3]          = tr("MP3 replay");
  m_szIOModes[eIOModes_ChannelHasCA]       = tr("Channel has CA");
  m_szIOModes[eIOModes_ChannelHasDpid]     = tr("Channel has DPID");
  m_szIOModes[eIOModes_ChannelHasApid]     = tr("Channel has APID");
  m_szIOModes[eIOModes_ChannelHasTpid]     = tr("Channel has TPID");
  m_szIOModes[eIOModes_ChannelHasVpid]     = tr("Channel has VPID");
  m_szIOModes[eIOModes_ChannelIsRadio]     = tr("Listen radio");

  m_szIOModes[eIOModes_ChannelSourceSat]   = tr("DVB-S Channel");
  m_szIOModes[eIOModes_ChannelSourceCable] = tr("DVB-C Channel");
  m_szIOModes[eIOModes_ChannelSourceTerr]  = tr("DVB-T Channel");
  m_szIOModes[eIOModes_ChannelSatVPolar]   = tr("Vertical Polarization");
  m_szIOModes[eIOModes_ChannelSatHPolar]   = tr("Horizontal Polarization");

  m_szIOModes[eIOModes_AudioMute]          = tr("Audio muted");
  m_szIOModes[eIOModes_MoreAudioTracks]    = tr("off"); ////Funkioniert nicht ohne Scanthread tr("More audio tracks");
  
  m_szIOModes[eIOModes_DVD_AC3]            = tr("DVD AC3 playback");
  m_szIOModes[eIOModes_DVD_DTS]            = tr("DVD DTS playback");
  m_szIOModes[eIOModes_DVD_PCM]            = tr("DVD PCM playback");
  m_szIOModes[eIOModes_DVD_MP2]            = tr("DVD MP2 playback");
  m_szIOModes[eIOModes_DVD_4_3]            = tr("DVD 4:3 playback");
  m_szIOModes[eIOModes_DVD_16_9]           = tr("DVD 16:9 playback");
  // MP3 Plugin Playbackmodes
  m_szIOModes[eIOModes_MP3_Shuffle]        = tr("MP3 shuffle play");
  m_szIOModes[eIOModes_MP3_Loop]           = tr("MP3 endless play");

  m_szIOModes[eIOModes_Cutting]            = tr("Editing process running");

  m_szIOModes[eIOModes_OsdActive]           = tr("OSD visible");
  m_szIOModes[eIOModes_OsdInActive]         = tr("OSD invisible");
  m_szIOModes[eIOModes_OsdMessage]          = tr("OSD message visible");
  m_szIOModes[eIOModes_OsdHelpKeyRed]       = tr("OSD key red visible");
  m_szIOModes[eIOModes_OsdHelpKeyGreen]     = tr("OSD key green visible");
  m_szIOModes[eIOModes_OsdHelpKeyYellow]    = tr("OSD key yellow visible");
  m_szIOModes[eIOModes_OsdHelpKeyBlue]      = tr("OSD key blue visible");
}
