#include <getopt.h>
#ifdef HAVE_IMAGEMAGICK
#include "bitmap.h"
#endif
#include "service.h"
#include "vdrstatus.h"
#ifdef VIDEOSTATUS
#include "videostatus.h"
#endif
#include "setup.h"
#include "DisplayChannel.h"
#include "DisplayMenu.h"
#include "DisplayReplay.h"
#include "DisplayVolume.h"
#include "DisplayTracks.h"
#include "DisplayMessage.h"
#include "skinelchi.h"
#include "i18n.h"
#include <vdr/font.h>
#include <vdr/osd.h>
#include <vdr/menu.h>
#include <vdr/themes.h>
#include <vdr/plugin.h>

static const char *VERSION	= "0.1.1pre2";
int SKINVERSION			= 00100;
static const char *DESCRIPTION	= "Elchi VDR Skin-Plugin";
const char *OSDSKIN		= "Elchi_Plugin";

#ifdef VIDEOSTATUS
int fd_video;
#endif
bool vdrosd;
// --- cSkinElchi ----------------------------------------------------------

cSkinElchi::cSkinElchi(void)
:cSkin(OSDSKIN, &::Theme)//XXX naming problem???
{
}

const char *cSkinElchi::Description(void)
{
  return tr("Elchi (Plugin)");
}

cSkinDisplayChannel *cSkinElchi::DisplayChannel(bool WithInfo)
{
  return new cSkinElchiDisplayChannel(WithInfo);
}

cSkinDisplayMenu *cSkinElchi::DisplayMenu(void)
{
  return new cSkinElchiDisplayMenu;
}

cSkinDisplayReplay *cSkinElchi::DisplayReplay(bool ModeOnly)
{
  return new cSkinElchiDisplayReplay(ModeOnly);
}

cSkinDisplayVolume *cSkinElchi::DisplayVolume(void)
{
  return new cSkinElchiDisplayVolume;
}

cSkinDisplayTracks *cSkinElchi::DisplayTracks(const char *Title, int NumTracks, const char * const *Tracks)
{
  return new cSkinElchiDisplayTracks(Title, NumTracks, Tracks);
}

cSkinDisplayMessage *cSkinElchi::DisplayMessage(void)
{
  return new cSkinElchiDisplayMessage;
}

// --- cPluginSkinElchi -----------------------------------------------------

class cPluginSkinElchi : public cPlugin {
private:
  cSkinElchiStatus *vdrStatus;
public:
  cPluginSkinElchi(void);
  virtual ~cPluginSkinElchi();
  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 Housekeeping(void);
  virtual const char *MainMenuEntry(void) { return ((!SkinElchiSetup.hidemenu || strcasecmp(OSDSKIN, Setup.OSDSkin)) ? NULL :SkinElchiSetup.menuentry); }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
#if VDRVERSNUM >= 10330 || defined(PATCH_CUSTOMSERVICES)
  bool Service(const char *Id, void *Data);
#endif
};

cPluginSkinElchi::cPluginSkinElchi(void)
//: vdrStatus(NULL)
{
    vdrStatus = NULL;
}

cPluginSkinElchi::~cPluginSkinElchi()
{
#ifdef VIDEOSTATUS
  if (fd_video != -1)
    close(fd_video);
#endif
  if(vdrStatus)
    delete vdrStatus;
}

const char *cPluginSkinElchi::CommandLineHelp(void)
{
#ifdef HAVE_IMAGEMAGICK
  return "  -c DIR,         --icache=DIR         optional path for epgimages\n"
         "                                       (default: '/video/epgimages')\n"
	 "  -l PathToLogos, --logos=PathToLogos  optional path for XPM channel logos\n"
         "                                       (default: '<configDir>/skinelchi/logos')\n"
         "  (if use a directory-option then this setting in setup only show)\n";
#else
  return "  -l PathToLogos, --logos=PathToLogos  optional path for XPM channel logos\n"
         "                                       (default: '<configDir>/skinelchi/logos')\n"
         "  (if use a directory-option then this setting in setup only show)\n";
#endif
}

bool cPluginSkinElchi::ProcessArgs(int argc, char *argv[])
{
  static struct option long_options[] = {
#ifdef HAVE_IMAGEMAGICK
      { "icache", required_argument, NULL, 'c' },
#endif
      { "logos", required_argument, NULL, 'l' },
      { NULL }
    };
  int c, option_index = 0;
  while ((c = getopt_long(argc, argv,
#ifdef HAVE_IMAGEMAGICK
   "c:l:"
#else
   "l:"
#endif
   , long_options, &option_index)) != -1) {
    switch (c) {
#ifdef HAVE_IMAGEMAGICK
      case 'c': strcpy(SkinElchiSetup.epgimagedir, optarg);
		SkinElchiSetup.commandlinedir = true;
#ifdef DEBUG
    		isyslog("skinelchi: PathToEpgImages '%s'", SkinElchiSetup.epgimagedir);
#endif
    		break;
#endif
      case 'l': strcpy(SkinElchiSetup.logodir, optarg);
		SkinElchiSetup.commandlinelogodir = true;
#ifdef DEBUG
    		isyslog("skinelchi: PathToLogos '%s'", SkinElchiSetup.logodir);
#endif
    		break;
      default:  
#ifdef DEBUG
    		isyslog("skinelchi: unknown command-line argument");
#endif
    		break;
      }
    }
  return true;
}

#if VDRVERSNUM >= 10330 || defined(PATCH_CUSTOMSERVICES)
bool cPluginSkinElchi::Service(const char *Id, void *Data)
{
  if (strcmp(Id, "Skin-Service") == 0) {
#ifdef DEBUG
    	isyslog("skinelchi: Skin-Service is called");
#endif
    if (Data == NULL)
#ifdef DEBUG
	{
    	isyslog("skinelchi: Skin-Service is called without data");
#endif
        return false;
#ifdef DEBUG
	}
#endif
    if (SkinElchiSetup.display != 2)
#ifdef DEBUG
	{
    	isyslog("skinelchi: Skin-Service is called without opened menu");
#endif
        return false;
#ifdef DEBUG
	}
#endif
    skin_serviceData* serviceData = (skin_serviceData*) Data;
    serviceData->servicename = strdup("Skinelchi-Service");
    serviceData->version = SKINVERSION;
    return true;
    }
  if (strcmp(Id, "Skinelchi-Service") == 0) {
#ifdef DEBUG
    	isyslog("skinelchi: Skinelchi-Service is called");
#endif
    if (Data == NULL)
#ifdef DEBUG
	{
    	isyslog("skinelchi: Skinelchi-Service is called without data");
#endif
        return true;
#ifdef DEBUG
	}
#endif
    return SkinService.Service(Id, Data);
    }
  return false;
}
#endif

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

bool cPluginSkinElchi::Start(void)
{
    vdrStatus = new cSkinElchiStatus/*()*/;
#ifdef VIDEOSTATUS
    fd_video = -1;
#endif
    RegisterI18n(Phrases);
    Theme.AddColor("clrBackground",         	0x77000066);//  0
    Theme.AddColor("clrButtonRedFg",        	clrWhite);  //  1
    Theme.AddColor("clrButtonRedBg",        	0xCCCC1111);//  2
    Theme.AddColor("clrButtonGreenFg",      	clrBlack);  //  3
    Theme.AddColor("clrButtonGreenBg",      	0xCC22BB22);//  4
    Theme.AddColor("clrButtonYellowFg",     	clrBlack);  //  5
    Theme.AddColor("clrButtonYellowBg",     	0xCCEEEE22);//  6
    Theme.AddColor("clrButtonBlueFg",       	clrWhite);  //  7
    Theme.AddColor("clrButtonBlueBg",       	0xCC2233CC);//  8
    Theme.AddColor("clrMessageStatusFg",    	clrBlack);  //  9
    Theme.AddColor("clrMessageStatusBg",    	0xCC2BA7F1);// 10
    Theme.AddColor("clrMessageInfoFg",      	clrBlack);  // 11
    Theme.AddColor("clrMessageInfoBg",      	0xCC22BB22);// 12
    Theme.AddColor("clrMessageWarningFg",   	clrBlack);  // 13
    Theme.AddColor("clrMessageWarningBg",   	clrYellow); // 14
    Theme.AddColor("clrMessageErrorFg",     	clrWhite);  // 15
    Theme.AddColor("clrMessageErrorBg",     	clrRed);    // 16
    Theme.AddColor("clrVolumePrompt",       	clrWhite);  // 17
    Theme.AddColor("clrVolumeBarUpper",     	0xFFCCBB22);// 18
    Theme.AddColor("clrVolumeBarLower",     	0x77000066);// 19
    Theme.AddColor("clrVolumeSymbolMuteBg", 	clrWhite);  // 20
    Theme.AddColor("clrVolumeSymbolMuteFg", 	0xCCCC1111);// 21
    Theme.AddColor("clrVolumeSymbolVolumeBg",   0x77000066);// 22
    Theme.AddColor("clrVolumeSymbolVolumeFg",   0xFFCCBB22);// 23
    Theme.AddColor("clrChannelNameFg",      	clrWhite);  // 24
    Theme.AddColor("clrChannelNameBg",      	0xCC2BA7F1);// 25
    Theme.AddColor("clrChannelNumberFg",    	clrBlack);  // 26
    Theme.AddColor("clrChannelNumberBg",    	clrWhite);  // 27 - currently not used
    Theme.AddColor("clrChannelDateFg",      	clrBlack);  // 28
    Theme.AddColor("clrChannelDateBg",      	clrWhite);  // 29
    Theme.AddColor("clrChannelEpgTimeFg",   	0xFFDDDDDD);// 30
    Theme.AddColor("clrChannelEpgTimeBg",   	0xAABB0000);// 31
    Theme.AddColor("clrChannelEpgTitle",    	0xFF00FCFC);// 32
    Theme.AddColor("clrChannelEpgTitleBg",  	0x88000000);// 33
    Theme.AddColor("clrChannelEpgShortText",	0xFFCCBB22);// 34
    Theme.AddColor("clrChannelSymbolOn",    	clrYellow); // 35
    Theme.AddColor("clrChannelSymbolOff",   	0x77777777);// 36
    Theme.AddColor("clrChannelSymbolRecFg", 	clrWhite);  // 37
    Theme.AddColor("clrChannelSymbolRecBg", 	clrRed);    // 38
    Theme.AddColor("clrChannelTimebarSeen", 	clrYellow); // 39
    Theme.AddColor("clrChannelTimebarRest", 	clrGray50); // 40
    Theme.AddColor("clrMenuTitleFg",        	clrBlack);  // 41
    Theme.AddColor("clrMenuTitleBg",        	0xCC2BA7F1);// 42
    Theme.AddColor("clrMenuDate",           	clrBlack);  // 43
    Theme.AddColor("clrMenuItemCurrentFg",  	clrBlack);  // 44
    Theme.AddColor("clrMenuItemCurrentBg",  	0xCC2BA7F1);// 45
    Theme.AddColor("clrMenuItemSelectable", 	clrWhite);  // 46
    Theme.AddColor("clrMenuItemNonSelectable",  0xCC2BA7F1);// 47
    Theme.AddColor("clrMenuEventTime",      	clrWhite);  // 48
    Theme.AddColor("clrMenuEventVpsFg",     	clrBlack);  // 49
    Theme.AddColor("clrMenuEventVpsBg",     	clrWhite);  // 50
    Theme.AddColor("clrMenuEventTitle",     	clrYellow); // 51
    Theme.AddColor("clrMenuEventShortText", 	clrWhite);  // 52
    Theme.AddColor("clrMenuEventDescription",   clrYellow); // 53
    Theme.AddColor("clrMenuScrollbarTotal", 	clrWhite);  // 54
    Theme.AddColor("clrMenuScrollbarShown", 	clrYellow); // 55
    Theme.AddColor("clrMenuText",           	clrWhite);  // 56
    Theme.AddColor("clrReplayTitleFg",      	clrBlack);  // 57
    Theme.AddColor("clrReplayTitleBg",      	clrWhite);  // 58
    Theme.AddColor("clrReplayCurrent",      	clrWhite);  // 59
    Theme.AddColor("clrReplayTotal",        	clrWhite);  // 60
    Theme.AddColor("clrReplayModeJump",     	clrWhite);  // 61
    Theme.AddColor("clrReplayProgressSeen", 	0xCC22BB22);// 62
    Theme.AddColor("clrReplayProgressRest", 	clrWhite);  // 63
    Theme.AddColor("clrReplayProgressSelected", 0xCCCC1111);// 64
    Theme.AddColor("clrReplayProgressMark", 	clrBlack);  // 65
    Theme.AddColor("clrReplayProgressCurrent",  0xCCCC1111);// 66
    Theme.AddColor("clrReplaySymbolOn",     	clrYellow); // 67
    Theme.AddColor("clrReplaySymbolOff",    	0x77777777);// 68
    Theme.AddColor("Transparent",	    	0x00000000);// 69
    Theme.AddColor("changing",	    	    	0x00000000);// 70

  char *filename;
  asprintf(&filename, "%s/../../themes/%s%s", cPlugin::ConfigDirectory("skinelchi"), OSDSKIN,"-change.theme");
  Theme.Load(filename);
  free(filename);

  for (int i = 0; i < MAX_ELCHI_THEME_COLORS; i++) {
    SkinElchiSetup.ThemeColors[i] = Theme.Color(i);
    }
  //not possible to set this in setup.c cSkinElchiSetup::cSkinElchiSetup(void),
  //because get '(NULL)/plugins/skinelchi' if call cPlugin::ConfigDirectory("skinelchi")
  if (!SkinElchiSetup.commandlinelogodir && (strcmp(SkinElchiSetup.logodir, "") == 0)) {
    strcpy(SkinElchiSetup.logodir, cPlugin::ConfigDirectory("skinelchi"));
    strcat(SkinElchiSetup.logodir, "/logos");
    }
  new cSkinElchi;
  return true;
}

void cPluginSkinElchi::Housekeeping(void)
{
}

cOsdObject *cPluginSkinElchi::MainMenuAction(void)
{
  if (SkinElchiSetup.hidemenu) //{
//    if (SkinElchiSetup.hidemenu == 1)
	return new cSkinElchiSetupPage;
/*    else {
#ifdef HAVE_YAEPG_PATCH
	if (SkinElchiSetup.vidwin) {
	    SkinElchiSetup.vidwin = 0;
	    Skins.Message(mtInfo, tr("Menu without video-window"));
	    }
	else {
	    SkinElchiSetup.vidwin = 1;
	    Skins.Message(mtInfo, tr("Menu with video-window"));
	    }
#endif
	return NULL;
	}
    }*/
  else
    return new cSkinElchiSetupPage;
}

cMenuSetupPage *cPluginSkinElchi::SetupMenu(void)
{
  return new cSkinElchiSetupPage;
}

bool cPluginSkinElchi::SetupParse(const char *Name, const char *Value)
{
  return SkinElchiSetup.SetupParse(Name, Value);
}

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

