//------------------------------------------------------------------------------
// emClockFileModel.cpp
//
// Copyright (C) 2006-2008 Oliver Hamann.
//
// Homepage: http://eaglemode.sourceforge.net/
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 3 as published by the
// Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
// more details.
//
// You should have received a copy of the GNU General Public License version 3
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

#include <emClock/emClockFileModel.h>


emRef<emClockFileModel> emClockFileModel::Acquire(
	emContext & context, const emString & name, bool common
)
{
	EM_IMPL_ACQUIRE(emClockFileModel,context,name,common)
}


const char * emClockFileModel::GetFormatName() const
{
	return "emClock";
}


bool emClockFileModel::IsStopwatchRunning() const
{
	return StopwatchRunning;
}


emInt64 emClockFileModel::GetStopwatchTimeMS(bool * pMillisecsValid) const
{
	emInt64 t,c,d;

	// Hint: With emGetClockMS(), the stopwatch is not correct after a
	// reboot of the system. And with the time() function, we do not have
	// milliseconds. Therefore both functions are used, and the "reboot"
	// case is detected by a too large difference between the results.
	t=GetStopwatchStateT();
	c=GetStopwatchStateC();
	if (StopwatchRunning) {
		t=((emInt64)time(NULL))*1000-t;
		c=emGetClockMS()-c;
	}
	d=t-c;
	if (d<-1500 || d>1500) {
		*pMillisecsValid=false;
		return t;
	}
	else {
		*pMillisecsValid=true;
		return c;
	}
}


void emClockFileModel::StartStopwatch()
{
	emInt64 t,c,s;
	bool msValid;

	if (!StopwatchRunning) {
		t=((emInt64)time(NULL))*1000;
		c=emGetClockMS();
		s=GetStopwatchTimeMS(&msValid);
		t-=s;
		c-=s;
		if (!msValid) c+=((emInt64)1)<<63;
		StopwatchRunning=true;
		SetStopwatchStateT(t);
		SetStopwatchStateC(c);
	}
}


void emClockFileModel::StopStopwatch()
{
	emInt64 t,c,s;
	bool msValid;

	if (StopwatchRunning) {
		s=GetStopwatchTimeMS(&msValid);
		t=s;
		c=s;
		if (!msValid) c+=((emInt64)1)<<63;
		StopwatchRunning=false;
		SetStopwatchStateT(t);
		SetStopwatchStateC(c);
	}
}


void emClockFileModel::ClearStopwatch()
{
	StopwatchRunning=false;
	SetStopwatchStateT(0);
	SetStopwatchStateC(0);
}


emInt64 emClockFileModel::GetStopwatchStateT() const
{
	const char * str;
	emInt64 t;

	str=StopwatchStateT.Get().Get();
	if (emStrToInt64(str,strlen(str),&t)<=0) t=0;
	return t;
}


void emClockFileModel::SetStopwatchStateT(emInt64 t)
{
	char str[64];
	int l;

	l=emInt64ToStr(str,sizeof(str),t);
	str[l]=0;
	StopwatchStateT=str;
}


emInt64 emClockFileModel::GetStopwatchStateC() const
{
	const char * str;
	emInt64 c;

	str=StopwatchStateC.Get().Get();
	if (emStrToInt64(str,strlen(str),&c)<=0) c=0;
	return c;
}


void emClockFileModel::SetStopwatchStateC(emInt64 c)
{
	char str[64];
	int l;

	l=emInt64ToStr(str,sizeof(str),c);
	str[l]=0;
	StopwatchStateC=str;
}


emClockFileModel::emClockFileModel(emContext & context, const emString & name)
	: emRecFileModel(context,name),
	emStructRec(),
	ClockBorderColor(this,"ClockBorderColor",emColor(192,176,96),true),
	ClockBackgroundColor(this,"ClockBackgroundColor",emColor(224,224,224),true),
	ClockForegroundColor(this,"ClockForegroundColor",emColor(16,16,16),true),
	ClockHandsColor(this,"ClockHandsColor",emColor(0,0,0),true),
	UTCClockBorderColor(this,"UTCClockBorderColor",emColor(96,96,96),true),
	UTCClockBackgroundColor(this,"UTCClockBackgroundColor",emColor(208,208,208),true),
	UTCClockForegroundColor(this,"UTCClockForegroundColor",emColor(64,32,16),true),
	UTCClockHandsColor(this,"UTCClockHandsColor",emColor(48,32,32),true),
	WorldClockBorderColor(this,"WorldClockBorderColor",emColor(224,224,160),true),
	WorldClockBackgroundColor(this,"WorldClockBackgroundColor",emColor(224,224,224,160),true),
	WorldClockForegroundColor(this,"WorldClockForegroundColor",emColor(16,16,16),true),
	WorldClockHandsColor(this,"WorldClockHandsColor",emColor(0,0,0),true),
	WorldClockRadius(this,"WorldClockRadius",1.0,0.01,100.0),
	WorldWaterColor(this,"WorldWaterColor",emColor(96,96,208),true),
	WorldLandColor(this,"WorldLandColor",emColor(128,192,0),true),
	WorldShadowColor(this,"WorldShadowColor",emColor(0,0,48,128),true),
	AlarmHour(this,"AlarmHour",0),
	AlarmMinute(this,"AlarmMinute",0),
	AlarmSecond(this,"AlarmSecond",0),
	StopwatchRunning(this,"StopwatchRunning",false),
	StopwatchStateT(this,"StopwatchStateT","0"),
	StopwatchStateC(this,"StopwatchStateC","0")
{
	PostConstruct(*this);
}


emClockFileModel::~emClockFileModel()
{
}
