/**
 *  GraphTFT plugin for the Video Disk Recorder 
 * 
 *  theme.c - A plugin for the Video Disk Recorder
 *
 *  (c) 2004 Lars Tegeler, Sascha Volkenandt  
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 * $Id: theme.c,v 1.4 2004/05/25 08:41:15 tegeler Exp $
 *
 **/

#include "theme.h"
#include "common.h"


// -- cGraphTFTThemeItem ----------------------------------------------------------------
cGraphTFTThemeItem::cGraphTFTThemeItem(void)
{
	_item = 0;
	_section = 0;
	_x=1;
	_y=1;
	_width=0;
	_height=0;
	_red=255;
	_green=255;
	_blue=255;
	_transperents=255;
	_lines=0;
	_size=32;
	_sw=0;
	_align=0;
	_animated=0;
	_delay=0;
	_count=0;

	_name = "Unnamed Theme";
	_version = "unknowen";
	_font = "Vera";
	_type = "";
	_path = "";
	_path2 = "";
	_text = "";
}

cGraphTFTThemeItem::~cGraphTFTThemeItem()
{
}

// Parse one line => one Item
bool cGraphTFTThemeItem::Parse(const char* s)
{
	std::string tmp;
	int posA = -1;
	int posB = -1;
	std::string toParse = strdup(s); 

	// check if it is an comment
	if ( toParse[0] == '#' )return true;
	// check if the line marks a section
	posA = toParse.find("[");
	posB = toParse.find("]",posA);
	if (posA == 0 && posB == ((int)toParse.size() - 1) )
	{
		//set section to none
		_section = 0;
		tmp = toParse.substr( posA + 1, posB-1 );
		//what section is it?
		if (tmp == "NormalTV") _section = sectionNormalTV;
		if (tmp == "NormalRadio") _section = sectionNormalRadio;
		if (tmp == "Menu") _section = sectionMenu;
		if (tmp == "ReplayNormal") _section = sectionReplayNormal;
		if (tmp == "ReplayDVD") _section = sectionReplayDVD;
		if (tmp == "ReplayMP3") _section = sectionReplayMP3;
		//set section for following items
		GraphTFTTheme.setSection(_section);

    // no section, so it must be an item
	}else{
	
		//check if it really an item, else return
		if (!ParseVar(toParse, "Item", &tmp)) return false;

		//get the current section value
		_section = GraphTFTTheme.getSection(); 


		//Look what Item is it
		if (tmp == "Date")
		{
			_item = itemDate;
			ParseText(toParse);
			
		}else if ( tmp == "DateOnly")
		{
			_item = itemDateOnly;
			ParseText(toParse);

		}else if ( tmp == "TimeOnly")
		{
			_item = itemTimeOnly;
			ParseText(toParse);

		}else if ( tmp == "Channel")
		{
			_item = itemChannel;
			ParseText(toParse);

		}else if ( tmp == "ChannelOnly")
		{
			_item = itemChannelOnly;
			ParseText(toParse);

		}else if ( tmp == "ChannelNumber")
		{
			_item = itemChannelNumber;
			ParseText(toParse);

		}else if ( tmp == "Rectangle")
		{
			_item = itemRectangle;
			ParseText(toParse);

		}else if ( tmp ==  "PresentTitle")
		{
			_item = itemPresentTitle;
			ParseText(toParse);
			
		}else if ( tmp == "PresentSubtitle")
		{
            _item = itemPresentSubtitle;
			ParseText(toParse);

		}else if ( tmp ==  "PresentTime")
		{
			_item = itemPresentTime;
			ParseText(toParse);
			
		}else if ( tmp ==  "FollowingTitle")
		{
			_item = itemFollowingTitle;
			ParseText(toParse);
			
		}else if ( tmp == "FollowingSubtitle")
		{
            _item = itemFollowingSubtitle;
			ParseText(toParse);

		}else if ( tmp ==  "FollowingTime")
		{
			_item = itemFollowingTime;
			ParseText(toParse);
							
		}else if ( tmp == "Recording")
		{
            _item = itemRecording;
			ParseText(toParse);
			
		}else if ( tmp ==  "Text" )
		{
			_item = itemText;
			ParseText(toParse);

		}else if (tmp=="Message")
		{
			_item=itemMessage;
			ParseText(toParse);

		}else if (tmp=="Volumebar")
                {
	  		_item=itemVolumebar;
			ParseText(toParse);

		}else if (tmp=="VolumeMuteSymbol")
		{
			_item=itemVolumeMuteSymbol;
			ParseText(toParse);

		}else if (tmp=="VolumeBackground")
		{
			_item=itemVolumeBackground;
			ParseText(toParse);

		}else if (tmp=="MessageBackground")
		{
			_item=itemMessageBackground;
			ParseText(toParse);

		}else if (tmp=="SymRecording")
		{
			_item=itemSymRecording;
			ParseText(toParse);

        }else if (tmp=="Sym2ch")
		{       
			_item=itemSym2ch;
			ParseText(toParse);

        }else if (tmp=="SymDD")
		{       
			_item=itemSymDD;
			ParseText(toParse);
		
        }else if (tmp=="SymVTX")
		{       
			_item=itemSymVTX;
			ParseText(toParse);

        }else if (tmp=="SymCrypt")
		{       
			_item=itemSymCrypt;
			ParseText(toParse);

		}else if (tmp=="Logo")
		{
			_item=itemLogo;
			ParseText(toParse);

		}else if (tmp=="Timebar")
	    {
			_item=itemTimebar;
			ParseText(toParse);

        }else if (tmp=="Image")
		{
			_item=itemImage;
			ParseText(toParse);
	}else if (tmp=="ImageCover")
	{
		_item=itemImageCover;
		ParseText(toParse);

        }else if (tmp=="ImageFile")
		                {
			_item=itemImageFile;
			ParseText(toParse);
				
	}else if (tmp=="ReplayName")
		{
			_item=itemReplayName;
			ParseText(toParse);

	}else if (tmp=="ReplayFolder")
		{
			_item=itemReplayFolder;
			ParseText(toParse);

        }else if (tmp=="ReplayProcessbar")
		{
			_item=itemReplayProcessbar;
			ParseText(toParse);
		
		}else if (tmp=="ReplayTotal")
		{
			_item=itemReplayTotal;
			ParseText(toParse);
		
		}else if (tmp=="ReplayCurrent")
		{
			_item=itemReplayCurrent;
			ParseText(toParse);
		
		}else if (tmp=="ReplayRemaining")
		{
			_item=itemReplayRemaining;
			ParseText(toParse);
		
		}else if (tmp=="ReplaySymbolPlay")
		{
			_item=itemSymRepPlay;
			ParseText(toParse);

		}else if (tmp=="ReplaySymbolPause")
		{
			_item=itemSymRepPause;
			ParseText(toParse);
		
		}else if (tmp=="ReplaySymbolFF")
		{
			_item=itemSymRepFF;
			ParseText(toParse);
		
		}else if (tmp=="ReplaySymbolFB")
		{
			_item=itemSymRepFB;
			ParseText(toParse);
		
		}else if (tmp=="ReplaySymbolSF")
		{
			_item=itemSymRepSF;
			ParseText(toParse);
		
		}else if (tmp=="ReplaySymbolSB")
		{
			_item=itemSymRepSB;
			ParseText(toParse);
					
		}else if (tmp=="MenuTitle")
		{
			_item=itemMenuTitle;
			ParseText(toParse);

		}else if (tmp=="Menu")
		{
			_item=itemMenu;
			ParseText(toParse);

		}else if (tmp=="MenuSelected")
		{
			_item=itemMenuSelected;
			ParseText(toParse);

		}else if (tmp=="MenuButtonRed")
		{
			_item=itemMenuButtonRed;
			ParseText(toParse);

		}else if (tmp=="MenuButtonGreen")
		{
			_item=itemMenuButtonGreen;
			ParseText(toParse);

		}else if (tmp=="MenuButtonYellow")
		{
			_item=itemMenuButtonYellow;
			ParseText(toParse);

		}else if (tmp=="MenuButtonBlue")
		{
			_item=itemMenuButtonBlue;
			ParseText(toParse);
                }else if (tmp=="MenuButtonBackgroundRed")
		{
			_item=itemMenuButtonBackgroundRed;
			ParseText(toParse);

		}else if (tmp=="MenuButtonBackgroundGreen")
		{
			_item=itemMenuButtonBackgroundGreen;
			ParseText(toParse);
		
		}else if (tmp=="MenuButtonBackgroundYellow")
		{
			_item=itemMenuButtonBackgroundYellow;
			ParseText(toParse);

		}else if (tmp=="MenuButtonBackgroundBlue")
		{
			_item=itemMenuButtonBackgroundBlue;
			ParseText(toParse);
			
                }else if (tmp=="MenuText")
		{
			_item=itemMenuText;
			ParseText(toParse);

		}else if (tmp=="MenuTextBackground")
		{
			_item=itemMenuTextBackground;
			ParseText(toParse);   
			
		}else if (tmp=="MenuImageMap")
		{
			_item=itemMenuImageMap;
			ParseText(toParse);
			
		// or is it the theme item, the check name, version, and dir
		}else if (tmp =="Theme")
		{
			std::string tmpdir;
			_item=itemTheme;
			if (! (ParseVar(toParse,"name",&_name) &&  ParseVar(toParse,"dir",&tmpdir) && ParseVar(toParse, "version", &_version) ) ) return false;
			GraphTFTTheme.setDir(tmpdir);
			ParseVar(toParse,"startImage",&_path);
			ParseVar(toParse,"endImage",&_path2);
		}
		// ups, none item knowen witch that name
		else
		{
			d(4 , "theme.c: Parse Item: Unkown Item = %s\n",tmp.c_str());
		}
	}
	// Return true afer parsing, so all was ok...
	return true;
}

//An item can parse the line for the text values
bool cGraphTFTThemeItem::ParseText(std::string toParse)
{
	ParseVar(toParse,"x",&_x);
	ParseVar(toParse,"y",&_y);
	ParseVar(toParse,"width",&_width);
	ParseVar(toParse,"height",&_height);
	ParseVar(toParse,"red",&_red);
	ParseVar(toParse,"green",&_green);
	ParseVar(toParse,"blue",&_blue);
	ParseVar(toParse,"transperents",&_transperents);
	ParseVar(toParse,"lines",&_lines);
	ParseVar(toParse,"font",&_font);
	ParseVar(toParse,"size",&_size);
	ParseVar(toParse,"text",&_text);
	ParseVar(toParse,"switch",&_sw);
	ParseVar(toParse,"align",&_align);
	ParseVar(toParse,"path",&_path);
        ParseVar(toParse,"pathBACK",&_path);
	ParseVar(toParse,"pathFRONT",&_path2);
	ParseVar(toParse,"pathON",&_path);
	ParseVar(toParse,"pathOFF",&_path2);
	ParseVar(toParse,"name",&_path);
	ParseVar(toParse,"file",&_path2);
	ParseVar(toParse,"path2",&_path2);
	ParseVar(toParse,"type",&_type);
	ParseVar(toParse,"animated",&_animated);
	ParseVar(toParse,"delay",&_delay);
	ParseVar(toParse,"count",&_count);
	return true;
}

//Search the parameter named by name in toParse, and returns the value into the value pointer
bool cGraphTFTThemeItem::ParseVar(std::string toParse, std::string name, int *value)
{
	int posA = -1;
	int posB = -1;
	
	name += "=";
	posA = toParse.find(name);
	posB = toParse.find(",",posA);
	if (posB < 0)  posB = toParse.find(";",posA);
	if (posA < 0 || posB <0) return false; //Error parsing Variable 
	*value = atoi( (toParse.substr( posA + name.size(), posB-posA-name.size() )).c_str() );
	return true;
}

//Search the parameter named by name in toParse, and returns the value into the value pointer
bool cGraphTFTThemeItem::ParseVar(std::string toParse, std::string name, std::string *value)
{
	int posA = -1;
	int posB = -1;
	name += "=";
	posA = toParse.find(name);
	posB = toParse.find(",",posA);
	if (posB < 0)  posB = toParse.find(";",posA);
	if (posA < 0 || posB <0) return false; //Error parsing Variable 
	*value = toParse.substr( posA + name.size(), posB-posA-name.size() );
	return true;
}


// -- cGraphTFTTheme ---------------------------------------------------------------


//this holds the list of item, ...
cGraphTFTTheme GraphTFTTheme;

//To get an spezial item out of the list in the theme class
cGraphTFTThemeItem *cGraphTFTTheme::Get(int item)
{
 for (cGraphTFTThemeItem *p = First(); p; p = Next(p)) {
      if ( p->Item() == item)
        return p;
      }
  return NULL;
}
