
#include "myosd.h"

cMyOsd::cMyOsd():cOsdMenu("")
{
	page = 0;
	order = 0;
	cnt_entry = 0;
	
	// Anzahleinträge ermitteln
	FILE *cntFile;
	char line[255];	
	cntFile = fopen ("/tmp/sortbytitle","r");
        if (cntFile == NULL) 
		dsyslog("Error: Kann /tmp/sortbytitle nicht finden");
        else
	{
		while(!feof(cntFile))
                {
			fgets (line , 256 , cntFile);
			cnt_entry++;
		}
	}

	loadFile("/tmp/sortbytitle","Archiv");
	
	
	// Untermenü
//	SetHelp("SortByTitle","SortByNumber","Neuer Eintrag","Suche");

	SetHelp("SortByTitle",0,">>","SortByNumber");
}

cMyOsd::~cMyOsd()
{
}

eOSState cMyOsd::ProcessKey(eKeys Key)
{
	eOSState state = cOsdMenu::ProcessKey(Key);


//	int order = 0;
	
	// Ausgabe löschen
	Clear();

	if(state == osUnknown)
	{
	switch (Key)
		{
		case kRed:
			if (order == 1)
			{
				order = 0;
				page = 0;
			}
				
			loadFile("/tmp/sortbytitle","Archiv");
			
			//Untermenü
			if (page == 0)
				SetHelp("SortByTitle",0,">>","SortByNumber");
			else
				SetHelp("SortByTitle","<<",">>","SortByNumber");

			break;
		case kGreen:
			if (page > 0) 
				page--;
			
			if (order == 0)
				loadFile("/tmp/sortbytitle","Archiv");
			else
				loadFile("/tmp/sortbynumber","Archiv");

			//Untermenü
			if (page == 0)
			{
				SetHelp("SortByTitle",0,">>","SortByNumber");
			}
			if ((page > 0)&&(page < ((int)cnt_entry/13)))
			{
				SetHelp("SortByTitle","<<",">>","SortByNumber");
			}
			if (page == ((int)cnt_entry/13)) 
			{ 
				SetHelp("SortByTitle","<<",0,"SortByNumber");
			}

			break;
		case kYellow:
			if (page < ((int)cnt_entry/13)) 
				page++;			
			
			if (order == 0)
				loadFile("/tmp/sortbytitle","Archiv");
			else
				loadFile("/tmp/sortbynumber","Archiv");
			
			//Untermenü
			if (page == 0)
			{
				SetHelp("SortByTitle",0,">>","SortByNumber");
			}
			if ((page > 0)&&(page < ((int)cnt_entry/13)))
			{
				SetHelp("SortByTitle","<<",">>","SortByNumber");
			}
			if (page == ((int)cnt_entry/13)) 
			{ 
				SetHelp("SortByTitle","<<",0,"SortByNumber");
			}
			
			break;
		case kBlue:
			if (order == 0)
			{
				order = 1;
				page = 0;
			}
		
			loadFile("/tmp/sortbynumber","Archiv");

			//Untermenü
			if (page == 0)
				SetHelp("SortByTitle",0,">>","SortByNumber");
			else
				SetHelp("SortByTitle","<<",">>","SortByNumber");
			
			break;
		default:	
			break;
		}
	}
	return state;
}

void cMyOsd::loadFile(const char* file, const char* titel)
{
//	dsyslog("ICH ZEIGE Seite %d Order %d", page , order);

//	dsyslog("Now I should read the File %s",file);

	char line[255];
	FILE *inputFile;
	int entries = 0;

	inputFile = fopen (file,"r");
        if (inputFile == NULL) 
		dsyslog("Error: can't open %s file",file);
        else
	{
		// Einträge zählen
		while(!feof(inputFile))
		{
			fgets (line , 256 , inputFile);
			entries++;
		}
	}
	fclose(inputFile);
	
	
	inputFile = fopen (file,"r");
        if (inputFile == NULL) 
		dsyslog("Error: can't open %s file",file);
        else
	{
		int count = 0;
		for (int i = 0; i < entries-1; i++)
		//		while(!feof(inputFile))
                {
			fgets (line , 256 , inputFile);

			if ((count >= (page*13))&&(count <= ((page+1)*13)))
			{
				dsyslog("--> %s",line);
				cOsdItem *item=new cOsdItem(line);
				item->SetSelectable(false);
				Add(item);
			}
			count++;
		}
	}
	fclose(inputFile);
	
	
		
//        	dsyslog("Info: I open %d file",entries);

//		int count = 0;
//

		
/*
		for (int i = 0; i < entries; i++)
//		while(!feof(inputFile))
                {
			fgets (line , 256 , inputFile);

			if ((count >= (page*13))&&(count <= ((page+1)*13)))
			{
				dsyslog("--> %s",line);
				cOsdItem *item=new cOsdItem(line);
				item->SetSelectable(false);
				Add(item);
			}
			count++;
		}
	*/
//	}
	SetTitle(titel);
	Display();
}

