#include "StdAfx.h"
#include "event.h"
#include "ATLComTime.h"

Event::Event()
{
	active = 0;
	expired = 0;
	popup.Hide();
	popup.Create();
	item = NULL;
	editing = 0;
}

Event::~Event(void)
{
}

// these things change due to time 
void Event::ShallowRefresh()
{
	COleDateTime StartTime(record.StartTime);
	COleDateTime EndTime(record.EndTime);
	COleDateTime now = COleDateTime::GetCurrentTime();
	COleDateTimeSpan TimeRemaining;

	// adjust the local time to match the server time
	now += rpc.TimeAdjustment;

	if(record.Eternal || (StartTime < now && EndTime > now)){
		active = 1;
		expired = 1;
		TimeRemaining = (EndTime-now);
	} else if(now < StartTime){
		active = 0;
		expired = 0;
		TimeRemaining = (StartTime-now);
	} else if(now > EndTime){
		active = 0;
		expired = 1;
		TimeRemaining = (now-EndTime);
	}

	TCHAR buffer[1023];
	// update the associated popup list
	popup.titlebar.title = theApp.GetDisplayName(lrecord);
	popup.titlebar.title += L" : ";
	if(active){
		if(record.Eternal){
			_stprintf(buffer,L"no schedule");
		} else {
			_stprintf(buffer,L"%s remaining",ConvertTimeSpanToCompactString(TimeRemaining));
			popup.titlebar.title += StartTime.Format(theApp.DateFormat);
		}
	} else if (expired) {
		_stprintf(buffer,L"expired %s ago",ConvertTimeSpanToCompactString(TimeRemaining));
		popup.titlebar.title += StartTime.Format(theApp.DateFormat);		
	} else {
		_stprintf(buffer,L"%s until start",ConvertTimeSpanToCompactString(TimeRemaining));
		popup.titlebar.title += StartTime.Format(theApp.DateFormat);
	}
	popup.subtitle.title = buffer;
	popup.Redraw();

	// update the associated item's style
	if(item){
		if(record.Private && record.OwnerId==theApp.user.UserId){ //private event
			if(editing){
				if(checked)
					item->Set(theApp.eventlist.CheckedPrivate);
				else
					item->Set(theApp.eventlist.UncheckedPrivate);
			} else {
				if(active)
					item->Set(theApp.eventlist.ActivePrivate);
				else
					item->Set(theApp.eventlist.PendingPrivate);
			}
		} else { // public event
			if(editing){
				if(checked)
					item->Set(theApp.eventlist.CheckedPublic);
				else
					item->Set(theApp.eventlist.UncheckedPublic);
			} else {
				if(active)
					item->Set(theApp.eventlist.ActivePublic);
				else
					item->Set(theApp.eventlist.PendingPublic);
			}
		}

		item->Redraw();
	}
}

void Event::DeepRefresh(int eventId)
{
	int result = db.GetCachedEvent(eventId,record);
	
	if(result==dbSuccess)
		popup.DeepRefresh(record);
	else if(result==dbInvalidParam)
		print(L"Unknown event in the event list!\n");

	if(record.LocationId==OtherLocationId){
		lrecord.LocationId = OtherLocationId;
		lrecord.LocationName = record.OtherLocation;
	} else
		db.GetCachedLocation(record.LocationId,lrecord);

	ShallowRefresh();
}

void Event::OnCustomNotify(int id, Notification& n)
{
	switch(id){
	case bcEvent:
		switch(n.reason){
		case bcModified:
			db.GetCachedEvent(record.EventId,record);
			db.GetCachedLocation(record.LocationId,lrecord);
			ShallowRefresh();
			break;
		}
		break;
	}

}
