
#include <string>
#include <fstream>
#include <vdr/menu.h>

#include "vdr/osd.h"
#include "vdr/menuitems.h"

#include "soccerosd.h"
#include "soccertools.h"
#include "i18n.h"

cSoccer::cSoccer(void)
:cOsdMenu(tr("Ranking"), 6, 18, 4,8,8)
{
  std::string file;
  file = DataDir;
  file = file + "/tabelle.txt";

  SetHelp(tr("Results"),NULL,tr("Refresh"),tr("Exit"));

  if(loadRanking(file.c_str())) {
    dsyslog("%s: Loaded ranking from '%s'\n",PLUGIN_NAME_I18N, file.c_str());
    }
  else {
    esyslog("%s: Can't load ranking from '%s'\n",PLUGIN_NAME_I18N, file.c_str());
    Skins.Message(mtError, tr("Error while loading ranking"));
    Add(new cOsdItem(hk("HELP:")));
    Add(new cOsdItem(hk("Press 'Yellow' to refresh the data.")));
    Add(new cOsdItem(hk("Check your systemlog for errormessages.")));
    Add(new cOsdItem(hk("Turn on 'Autoupdate' (not recommened).")));
    Add(new cOsdItem(hk("Try other paths with commandline flags.")));
    Add(new cOsdItem(hk("If nothing helps, get an internetconnection ;).")));
  }
  
  Display();
}

cSoccer::~cSoccer()
{
}


int cSoccer::loadRanking(const char* file) {
  std::ifstream filestr;
  std::string line;
  std::string Verein;
  std::string Games;
  std::string Goals;
  std::string Points;
  char ranking[80];
  int count=0;
  int len=0;
  bool result=false;
  

  filestr.open(file);
  if(filestr) {
    while (getline(filestr,line,'\n')) {
      count++;
      len = line.length();
      //Verein
      Verein = line.substr(5, 20);
      //Games
      Games  = line.substr(32, 4);
      //Goals
      Goals  = line.substr(36, 7);
      //Points
      Points = line.substr(43, 3);
      
      snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s   %s   %s",count, Verein.c_str(), Games.c_str(), Goals.c_str(), Points.c_str());
      
      Add(new cOsdItem(hk(ranking)));
    }
    filestr.close();
    dsyslog("%s: Opened ranking '%s'\n",PLUGIN_NAME_I18N, file);
    result = true;
  }
  else {
    esyslog("%s: Can't open ranking '%s'\n",PLUGIN_NAME_I18N, file);
    result = false;
    }

  return result;
}


void cSoccer::reloadData() {
  std::string file;
  char *buffer;

  Skins.Message(mtStatus, tr("Reloading data..."));  
  
  file = DataDir;
  file = file + "/tabelle.txt";
  
  asprintf(&buffer, "%s%s \"%s\"", ScriptDir, "/getcontent.sh", DataDir);
  int reload = system(buffer);
  
  if(reload != 0) {  
    Skins.Message(mtError, tr("Error while reloading data"));  
    esyslog("%s: Can't reload data !  '%s'\n", PLUGIN_NAME_I18N, buffer);
    }
  else {
    dsyslog("%s: Reloaded data  '%s'\n", PLUGIN_NAME_I18N, buffer);
    Skins.Message(mtStatus, NULL);  
    Clear();
    Skins.Flush();
    loadRanking(file.c_str());

    Display();

    }   
  free(buffer);
}


eOSState cSoccer::ProcessKey(eKeys Key)
{

  bool hadSubmenu = HasSubMenu();

  eOSState state = cOsdMenu::ProcessKey(Key);

  if(hadSubmenu && !HasSubMenu())
    return osContinue;

  if(state==osUnknown) {
    switch (Key) {
        case kRed:	
   	   return AddSubMenu( new cResults());
	   break;
        case kGreen:	
           break;
        case kYellow:
	   reloadData();   	          
	   break;
        case kBlue:
	   return osEnd;
	   break;
        default:
	   state = osContinue;
    }
  }

 return state;
}



// ---------- cResults --------------------------------------------------------------/

cResults::cResults()
:cMenuText(tr("Results"),"")
{
  std::string file;
  file = DataDir;
  file = file + "/ergebnisse.txt";

  SetHelp(NULL,NULL,NULL,NULL);


  if(loadResults(file.c_str())) {
    SetText(text.c_str());
    dsyslog("%s: Loaded results from '%s'\n",PLUGIN_NAME_I18N, file.c_str());
    }
  else {
    esyslog("%s: Can't load results from '%s'\n",PLUGIN_NAME_I18N, file.c_str());
    Skins.Message(mtError, tr("Error while loading results"));  
  }
  
  Display();
}

int cResults::loadResults(const char *file) {

  std::ifstream filestr;
  std::string line;
  
  bool result = false;
  
  text = "";

  filestr.open(file);
  if(filestr) {
    while (getline(filestr,line,'\n')) {
    text = text + line + "\n";
    }
    filestr.close();
    dsyslog("%s: Opened results '%s'\n",PLUGIN_NAME_I18N, file);
    result = true;
  }
  else {
    esyslog("%s: Can't open '%s'\n",PLUGIN_NAME_I18N, file);
    result = false;
    }

  return result; 
}

eOSState cResults::ProcessKey(eKeys Key)
{
  eOSState state = cMenuText::ProcessKey(Key);

  if(state==osUnknown) {
    switch (Key) {
        case kOk:
	    return osBack;
	   break;
        default:
	   state=osContinue;
    }
  }

 return state;
}

