#include "iostream"


//this
#include "SerLEDMonitor.H"

//vdr
#include "vdr/cutter.h"

using namespace std;


//inline bool cutter_active()
//	{
//	if(cCutter :: cuttingThread) return true;
//	return false;
//	}

SerLEDMonitor :: SerLEDMonitor() : cStatus(), _n_rec(0), _dev_recording(MAXDEVICES, 0), _all_recording(false), _cutting(false)
	{
	}


void SerLEDMonitor :: deactivate(std::vector<SerLED *> &led, std::vector<int> &nr)
	{
	_mutex.Lock();
	for(unsigned int i=0; i<led.size(); i++)
		{
		led[i]->DeActivate(nr[i]);
		}
	_mutex.Unlock();
	}
	
void SerLEDMonitor :: activate(std::vector<SerLED *> &led, std::vector<int> &nr)
	{
	_mutex.Lock();
	for(unsigned int i=0; i<led.size(); i++)
		{
		cout<<"activating "<<i<<endl;
		led[i]->Activate(nr[i]);
		}
	_mutex.Unlock();
	}
		
void * SerLEDMonitor :: ThreadEntry(SerLEDMonitor *cl)
	{
	while(1)
		{
		if(cl->_cutting_on && !cCutter :: Active())
			{
			cl->_cutting_on=false;
			cl->deactivate(cl->_cutting, cl->_cutting_nr);
			}
		else if(!cl->_cutting_on && cCutter :: Active())
			{
			cl->_cutting_on=true;
			cl->activate(cl->_cutting, cl->_cutting_nr);
			}
		sleep(1);
		}
	return NULL;
	}
	
#if defined(VDRVERSNUM) && VDRVERSNUM >= 10344
void SerLEDMonitor :: Recording(const cDevice *Device, const char *Name, const char *FileName, bool On)
#else
void SerLEDMonitor :: Recording(const cDevice *Device, const char *Name)
#endif
	{
	cerr<<"SerLEDMonitor :: Recording called"<<endl;
		cerr<<"n_rec="<<_n_rec<<" nev="<<cDevice :: NumDevices()<<endl;
//recording stoped
#if defined(VDRVERSNUM) && VDRVERSNUM >= 10344
	if(!On)
#else
	if(Name==NULL)
#endif	
		{
		_n_rec--;
		cerr<<"n_rec="<<_n_rec<<" nev="<<cDevice :: NumDevices()<<endl;
	//we are no longer recording anything
		if(_n_rec==0)
			{
			cerr<<"recording stopped"<<endl;
			deactivate(_rec, _rec_nr);
			}
		_dev_recording[Device->DeviceNumber()]--;
	//we are not using all devices anymore
		if(_dev_recording[Device->DeviceNumber()]==0 && _all_recording)
			{
			_all_recording=false;
			cerr<<"all recording stopped"<<endl;
			deactivate(_all_rec, _all_rec_nr);
			}
		return;
		}
//recording started
	_n_rec++;
	cerr<<"n_rec="<<_n_rec<<" nev="<<cDevice :: NumDevices()<<endl;
	if(_n_rec==1) 
		{
		cerr<<"started recording"<<endl;
		activate(_rec, _rec_nr);
		}
	_dev_recording[Device->DeviceNumber()]++;
	if(!_all_recording)
		{
		for(int i=0; i<cDevice :: NumDevices(); i++)
			{
		//there is another free device
			if(_dev_recording[i]==0) return;
			}
		_all_recording=true;
		cerr<<"all recording started"<<endl;
		activate(_all_rec, _all_rec_nr);
		}
	}

#if defined(VDRVERSNUM) && VDRVERSNUM >= 10344
void SerLEDMonitor :: Replaying(const cControl *Device, const char *Name, const char *FileName, bool On)
#else
void SerLEDMonitor :: Replaying(const cControl *Control, const char *Name)
#endif
	{
#if defined(VDRVERSNUM) && VDRVERSNUM >= 10344
	if(!On) deactivate(_replay, _replay_nr);
#else
	if(Name==NULL) deactivate(_replay, _replay_nr);
#endif
	else activate(_replay, _replay_nr);
	}
	
void SerLEDMonitor :: OsdHelpKeys(const char *Red, const char *Green, const char *Yellow, const char *Blue)
	{
//check red
	if(Red==NULL && _color_active[red]) 
		{
		_color_active[red]=false;
		deactivate(_color[red], _color_nr[red]);
		}
	if(Red!=NULL && !_color_active[red]) 
		{
		_color_active[red]=true;
		activate(_color[red], _color_nr[red]);
		}
//check green
	if(Green==NULL && _color_active[green])
		{
		_color_active[green]=false;
		deactivate(_color[green], _color_nr[red]);
		}
	if(Green!=NULL && !_color_active[green])
		{
		_color_active[green]=true;
		activate(_color[green], _color_nr[red]);
		}
//check yellow
	if(Yellow==NULL && _color_active[yellow])
		{
		_color_active[yellow]=false;
		deactivate(_color[yellow], _color_nr[yellow]);
		}
	if(Yellow!=NULL && !_color_active[yellow])
		{
		_color_active[yellow]=true;
		activate(_color[yellow], _color_nr[yellow]);
		}
//check blue
	if(Blue==NULL && _color_active[blue])
		{
		_color_active[blue]=false;
		deactivate(_color[blue], _color_nr[blue]);
		}
	if(Blue!=NULL && !_color_active[blue])
		{
		_color_active[blue]=true;
		activate(_color[blue], _color_nr[blue]);
		}
	}

void SerLEDMonitor :: OSDClear()
	{
	cerr<<"SerLEDMonitor :: OSDClear()"<<endl;
	for(unsigned int i=0; i<4; i++)
		{
		if(_color_active[i]) 
			{
			cerr<<"Clear "<<i<<endl;
			deactivate(_color[i], _color_nr[i]);
			}
		}
	}


void  SerLEDMonitor :: StartCutWatch()
	{
//start thread only if a status led ist set for indicating cutting
	if(_cutting.size()>0)
		{
		if(pthread_create(&_cut_watch, NULL, (void *(*) (void *))&ThreadEntry, (void *)this)==0)
			{
			pthread_detach(_cut_watch);
			pthread_setschedparam(_cut_watch, SCHED_RR, 0);
			}
		}
	}
