//indent
//#include "vdrstatus.h"
#include "videostatus.h"
#include "service.h"
#include "setup.h"
#include "common.h"
#if APIVERSNUM < 10507
#include "i18n.h"
#endif

extern int SKINVERSION;
//cService SkinService;

cService::cService(void) {
    DSYSLOG("skinelchi: cService::cService() is called")
    ServiceUpdate = new cServiceUpdate;
    }

cService::~cService() {
    DSYSLOG("skinelchi: cService::~cService() is called")
    isyslog("skinelchi: cService::~cService() is called");
    if (ServiceUpdate) delete ServiceUpdate;
    }

void cService::Stop(void) {
    isyslog("skinelchi: cService::Stop() is called");
    ServiceUpdate->Stop();
    }

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) {
	DSYSLOG("skinelchi: Skinelchi-Service is called with \"Version\"")
	if (serviceData->number > SKINVERSION)
	    serviceData->error = 1;
	else
	    serviceData->error = 0;
	serviceData->number = SKINVERSION;
	return true;
	}
    if (strcmp(serviceData->service, "Change") == 0) {
	DSYSLOG("skinelchi: Skinelchi-Service is called with \"Change\"")
	if (SkinElchiSetup.display == 2) {
	    if (!serviceData->number)
		SkinElchiSetup.changing = false;
	    else {
		DSYSLOG("skinelchi: Skinelchi-Service is called with \"Change\" to show Color: %x", serviceData->color)
		Theme.AddColor("changing", serviceData->color);
	        SkinElchiSetup.changing = true;
		}
	    serviceData->error = 0;
	    }
	else
	    serviceData->error = 1;
	return true;
	}
    return false;
    }

cServiceUpdate::cServiceUpdate(void)
:cThread("skinelchi-cServiceUpdate")
{
    DSYSLOG("skinelchi: cServiceUpdate::cServiceUpdate()")
    Start();
}

cServiceUpdate::~cServiceUpdate() {
    DSYSLOG("skinelchi: cServiceUpdate::~cServiceUpdate()")
    isyslog("skinelchi: cServiceUpdate::~cServiceUpdate()");
    Stop();
    //Pid++;
}

void cServiceUpdate::Stop() {
    DSYSLOG("skinelchi: cServiceUpdate::Stop()")
    isyslog("skinelchi: cServiceUpdate::Stop()");
    if (Running())
        Cancel(2);
}

void cServiceUpdate::Update() {
    DSYSLOG("skinelchi: cServiceUpdate::Update() is called")
    Lock();
//
    Unlock();
    Wait.Signal();
    }

void cServiceUpdate::Action(void) {
    //pid = Pid = (int)getpid();
    DSYSLOG("skinelchi: cServiceUpdate::Action()")
    //while (Pid == pid) {
    while (Running()) {
        Wait.Wait(1000);
        }
    DSYSLOG("skinelchi: cServiceUpdate::Action() end")
}
