#include "tools.h"
#include "recstatus.h"
#include "recdone.h"


extern int updateForced;

cRecStatusMonitor* gl_recStatusMonitor = NULL;

cRecStatusMonitor::cRecStatusMonitor()
{
}

#if VDRVERSNUM >= 10338
void cRecStatusMonitor::Recording(const cDevice *Device, const char *Name, const char*, bool On) 
#else
void cRecStatusMonitor::Recording(const cDevice *Device, const char* Name)
#endif
{
    // insert new timers currently recording in TimersRecording
#if VDRVERSNUM >= 10338
    if (On && Name)
#else
    if (Name)
#endif
    {
	for (cTimer *ti = Timers.First(); ti; ti = Timers.Next(ti)) 
	    if (ti->Recording())
	    {
		// check if this is a new entry
		bool bFound = false;
		for (cRecDoneTimerObj *tiR = TimersRecording.First(); tiR; tiR = TimersRecording.Next(tiR)) 
		    if (tiR->timer == ti)
			bFound = true;

		if (bFound) continue; // already handled
		
		cRecDoneTimerObj* timerObj = new cRecDoneTimerObj(ti, Device->CardIndex());
		TimersRecording.Add(timerObj);

		cSearchExt* search = TriggeredFromSearchTimer(ti);
		if (!search || search->avoidRepeats == 0) // ignore 
		    continue;

		LogFile.Log(1,"recording started '%s' on device %d (search timer '%s')", Name, Device->CardIndex(), search->search);
		const cEvent* event = ti->Event();
		if (!event)
		{
		    event = GetEvent(ti);
		    if (event)
			LogFile.Log(3,"timer had no event: assigning '%s'", event->Title());
		}
		if (!event)
		{
		    LogFile.Log(1,"no event for timer found! will be ignored in done list");
		    continue;
		}
		time_t now = time(NULL);
		if (now < ti->StartTime() + 60) // allow a delay of one minute
		{
		    timerObj->recDone = new cRecDone(ti, event, search);
		    return;
		}
		else
		    LogFile.Log(1,"recording started too late! will be ignored");
	    }
    }
    
#if VDRVERSNUM >= 10338
    if (!On)
#else
    if (!Name)
#endif
    {
	// remove timers that finished recording from TimersRecording
	for (cRecDoneTimerObj *tiR = TimersRecording.First(); tiR; tiR = TimersRecording.Next(tiR)) 
	{
	    if (!tiR->timer->Recording())
	    {
		if (tiR->recDone)
		{
		    cSearchExt* search = SearchExts.GetSearchFromID(tiR->recDone->searchID);
		    if (!search) return;
		    
		    // check if recording has ended before timer end
		    time_t now = time(NULL);
		    bool complete = true;
		    if (now < tiR->timer->StopTime())
		    {
			complete = false;
			LogFile.Log(1,"finished: '%s' (not complete!); search timer: '%s'", tiR->timer->File(), search->search);
		    }
		    else
			LogFile.Log(1,"finished: '%s'; search timer: '%s'", tiR->timer->File(), search->search);
		    if (complete)
		    {
			RecsDone.Add(tiR->recDone);
			LogFile.Log(1,"added rec done for '%s~%s';%s", tiR->recDone->title?tiR->recDone->title:"unknown title", 
				    tiR->recDone->shortText?tiR->recDone->shortText:"unknown subtitle",
				    search->search);
			RecsDone.Save();
			// trigger a search timer update
			updateForced = 1;			
		    }
		    else
			delete tiR->recDone; // clean up
		}
		TimersRecording.Del(tiR);
		break;
	    }
	}
    }
}

int cRecStatusMonitor::TimerRecDevice(cTimer* timer)
{
    for (cRecDoneTimerObj *tiR = TimersRecording.First(); tiR; tiR = TimersRecording.Next(tiR)) 
	if (tiR->timer == timer) return tiR->deviceNr+1;
    return 0;
}
