#include "templatefile.h"
#include "epgsearchcfg.h"
#include "tools.h"

char menuWhatsOnNowTemplate[MAXTEMPLLEN] = "";
char menuWhatsOnNextTemplate[MAXTEMPLLEN] = "";
char menuWhatsOnElseTemplate[MAXTEMPLLEN] = "";
char menuScheduleTemplate[MAXTEMPLLEN] = "";
char menuSearchResultsTemplate[MAXTEMPLLEN] = "";
int menuWhatsOnNowTabs[10];
int menuWhatsOnNextTabs[10];
int menuWhatsOnElseTabs[10];
int menuScheduleTabs[10];
int menuSearchResultsTabs[10];

cTemplLine::cTemplLine(void)
{
  name = value = NULL;
}

cTemplLine::cTemplLine(const char *Name, const char *Value)
{
  name = strdup(Name);
  value = strdup(Value);
}

cTemplLine::~cTemplLine()
{
  free(name);
  free(value);
}

bool cTemplLine::Parse(char *s)
{
    if (s && s[0] == '#')
	return true;
    char *p = strchr(s, '=');
    if (p) 
    {
	*p = 0;
	char *Name  = compactspace(s);
	char *Value = compactspace(p + 1);
	if (*Name) 
	{
	    name = strdup(Name);
	    value = strdup(Value);
	    return true;
	}
    }
    return false;
}

cTemplFile::cTemplFile()
{
    Reset();
}

void cTemplFile::Reset()
{
  strcpy(menuWhatsOnNowTemplate, "");
  strcpy(menuWhatsOnNextTemplate, "");
  strcpy(menuWhatsOnElseTemplate, "");
  strcpy(menuScheduleTemplate, "");
  strcpy(menuSearchResultsTemplate, "");
}

bool cTemplFile::Load(const char *FileName)
{
  if (cConfig<cTemplLine>::Load(FileName, true)) 
  {
      bool result = true;
      for (cTemplLine *l = First(); l; l = Next(l)) 
      {
	  bool error = false;
	  if (!Parse(l->Name(), l->Value()))
	      error = true;
	  if (error) 
	  {
	      esyslog("ERROR: unknown parameter: %s = %s", l->Name(), l->Value());
	      result = false;
	  }
      }
      return result;
  }
  return false;
}

bool cTemplFile::Parse(const char *Name, const char *Value)
{
  if (!strcasecmp(Name, "MenuWhatsOnNow")) PrepareTemplate(Name, Value);
  else if (!strcasecmp(Name, "MenuWhatsOnNext")) PrepareTemplate(Name, Value);
  else if (!strcasecmp(Name, "MenuWhatsOnElse")) PrepareTemplate(Name, Value);
  else if (!strcasecmp(Name, "MenuSchedule")) PrepareTemplate(Name, Value);
  else if (!strcasecmp(Name, "MenuSearchResults")) PrepareTemplate(Name, Value);
  else if (!strcasecmp(Name, "WarEagleIcons")) EPGSearchConfig.WarEagle = atoi(Value);
  else if (Name && Name[0] == '#') return true;
  else return false;

  return true;
}

void cTemplFile::PrepareTemplate(const char* templateType, const char* templateLine)
{
    if (!templateLine || strlen(templateLine) == 0) return;

    char *pstrSearchToken, *pptr;
    char *pstrSearch=strdup(templateLine);
    pstrSearchToken=strtok_r(pstrSearch, "|", &pptr);
    char* stripped = NULL;
    int iToken = 0;
    while(pstrSearchToken) 
    {
	char* tmp = strchr(pstrSearchToken,':');
	if (tmp)
	{
	    int tabWidth = atoi(tmp+1);
	    if (!strcasecmp(templateType, "MenuWhatsOnNow")) menuWhatsOnNowTabs[iToken++] = tabWidth;
	    if (!strcasecmp(templateType, "MenuWhatsOnNext")) menuWhatsOnNextTabs[iToken++] = tabWidth;
	    if (!strcasecmp(templateType, "MenuWhatsOnElse")) menuWhatsOnElseTabs[iToken++] = tabWidth;
	    if (!strcasecmp(templateType, "MenuSchedule")) menuScheduleTabs[iToken++] = tabWidth;
	    if (!strcasecmp(templateType, "MenuSearchResults")) menuSearchResultsTabs[iToken++] = tabWidth;
	}
	*tmp = 0;
	if (!stripped)
	    stripped = strdup(pstrSearchToken);
	else
	{
	    char* tmp = stripped;
	    stripped = NULL;
	    asprintf(&stripped, "%s|%s", tmp, pstrSearchToken);
	    free(tmp);
	}
	pstrSearchToken=strtok_r(NULL, "|", &pptr);
    }
    free(pstrSearch);

    if (stripped)
    {
	if (!strcasecmp(templateType, "MenuWhatsOnNow")) strcpy(menuWhatsOnNowTemplate, stripped);
	if (!strcasecmp(templateType, "MenuWhatsOnNext")) strcpy(menuWhatsOnNextTemplate, stripped);
	if (!strcasecmp(templateType, "MenuWhatsOnElse")) strcpy(menuWhatsOnElseTemplate, stripped);
	if (!strcasecmp(templateType, "MenuSchedule")) strcpy(menuScheduleTemplate, stripped);
	if (!strcasecmp(templateType, "MenuSearchResults")) strcpy(menuSearchResultsTemplate, stripped);
	free(stripped);
    }
}

void cTemplFile::PrepareDefaultTemplates()
{
    char channelnr[20] = "";
    sprintf(channelnr, "%%chnr%%:%d|", CHNUMWIDTH); 

    bool text2skin = !(strcmp(Setup.OSDSkin, "soppalusikka") == 0 ||
		       strcmp(Setup.OSDSkin, "classic") == 0 ||
		       strcmp(Setup.OSDSkin, "sttng") == 0);

    char menutemplate[MAXTEMPLLEN] = "";
    // What's on now
    if (strlen(menuWhatsOnNowTemplate) == 0)
    {
	sprintf(menutemplate, "%s%%chsh%%:12|%%time%%:6|%s%s%%status%%:3|%%title%% ~ %%subtitle%%:30", 
		EPGSearchConfig.showChannelNr?channelnr:"",
		EPGSearchConfig.showProgress==0?"":(EPGSearchConfig.showProgress==1?"%progrT2S%:4|":"%progr%:5|"),
		text2skin?" ":"");
	PrepareTemplate("MenuWhatsOnNow", menutemplate);
    }
	
    // What's on next and else
    sprintf(menutemplate, "%s%%chsh%%:12|%%time%%:6|%%status%%:4|%%title%% ~ %%subtitle%%:30", 
	    EPGSearchConfig.showChannelNr?channelnr:"");
    if (strlen(menuWhatsOnNextTemplate) == 0)
	PrepareTemplate("MenuWhatsOnNext", menutemplate);
    if (strlen(menuWhatsOnElseTemplate) == 0)
	PrepareTemplate("MenuWhatsOnElse", menutemplate);

    // Schedule
    if (strlen(menuScheduleTemplate) == 0)
    {
	strcpy(menutemplate, "%time_w% %time_d%:7|%time%:6|%status%:4|%title% ~ %subtitle%:30");
	PrepareTemplate("MenuSchedule", menutemplate);
    }
    // Search results
    if (strlen(menuSearchResultsTemplate) == 0)
    {
	sprintf(menutemplate, "%s%%chsh%%:12|%%datesh%%:6|%%time%%:6|%%status%%:3|%%title%% ~ %%subtitle%%:30", 
		EPGSearchConfig.showChannelNr?channelnr:"");
	PrepareTemplate("MenuSearchResults", menutemplate);
    }
}
