#include "vdrstatus.h"
#include "videostatus.h"
#include "service.h"
#include "setup.h"
#include "i18n.h"

extern int SKINVERSION;
cService SkinService;

cService::cService(void) {
#ifdef DEBUG
    isyslog("skinelchi: cService::cService() is called");
#endif
//    ServiceUpdate = new cServiceUpdate;
    }

cService::~cService() {
#ifdef DEBUG
    isyslog("skinelchi: cService::~cService() is called");
#endif
//    VideoStatusUpdate->Pid++;
    }

void cService::Update() {
//    ServiceUpdate->Update();
    }

bool cService::Service(const char *Id, void *Data) {
    skinelchi_serviceData* serviceData = (skinelchi_serviceData*) Data;
    if (strcmp(serviceData->service, "Version") == 0) {
#ifdef DEBUG
    	isyslog("skinelchi: Skinelchi-Service is called with \"Version\"");
#endif
	if (serviceData->number > SKINVERSION)
	    serviceData->error = 1;
	else
	    serviceData->error = 0;
	serviceData->number = SKINVERSION;
	return true;
	}
    if (strcmp(serviceData->service, "Change") == 0) {
#ifdef DEBUG
    	isyslog("skinelchi: Skinelchi-Service is called with \"Change\"");
#endif
	if (SkinElchiSetup.display == 2) {
	    if (!serviceData->number)
		SkinElchiSetup.changing = false;
	    else {
#ifdef DEBUG
    		isyslog("skinelchi: Skinelchi-Service is called with \"Change\" to show Color: %x", serviceData->color);
#endif
    		Theme.AddColor("changing", serviceData->color);
	        SkinElchiSetup.changing = true;
		}
	    serviceData->error = 0;
	    }
	else
	    serviceData->error = 1;
	return true;
	}
    return false;
    }	

cServiceUpdate::cServiceUpdate(void) {
#ifdef DEBUG
    isyslog("skinelchi: cServiceUpdate::cServiceUpdate()");
#endif
    Start();
}

cServiceUpdate::~cServiceUpdate() {
#ifdef DEBUG
    isyslog("skinelchi: cServiceUpdate::~cServiceUpdate()");
#endif
    Pid++;
}

void cServiceUpdate::Update() {
#ifdef DEBUG
    isyslog("skinelchi: cServiceUpdate::Update() is called");
#endif
    Lock();
//
    Unlock();
    Wait.Signal();
    }

void cServiceUpdate::Action(void) {
    pid = Pid = (int)getpid();
#ifdef DEBUG
    isyslog("skinelchi: cServiceUpdate::Action() - pid=%d", pid);
#endif
    while (Pid == pid) {
	Wait.Wait(0);
	}
#ifdef DEBUG
    isyslog("skinelchi: cServiceUpdate - thread end (pid=%d)", pid);
#endif
}
