#include "StdAfx.h"
#include "location.h"

Location::Location()
{
	state = empty;
	haspopup = 1;
	item = NULL;
	editing = 0;
	checked = 0;
}

Location::~Location(void)
{
}

// these things change due to time 
void Location::ShallowRefresh()
{
	MapLocation * ml = theApp.mapwindow.FindLocation(record.LocationId);

	if(ml){
		record.nUsers = ml->UserCount();
	} else
		record.nUsers = 0;

	//record.nEvents = popup.events.length;

	if(theApp.CurrentLocation.LocationId==record.LocationId || theApp.CurrentLocation.LocationId == record.ParentId)
		state = State::useroccupied;
	else if(record.nEvents>0)
		state = State::event;
	else if (record.nUsers>0)
		state = State::active;
	else
		state = State::empty;

	TCHAR buffer[1023];
	// update the associated popup list
	popup.titlebar.title = record.LocationName;
	popup.titlebar.title += L" : ";
	switch(state){
		case State::active:
			_stprintf(buffer,
				env.GetWideString("locationpopup::subtitle::active",
				"formatstring",L"%d people %d events"),
				record.nUsers,record.nEvents);
			popup.titlebar.title += theApp.locationlist.Active.description;
			break;
		case State::empty:
			_stprintf(buffer,
				env.GetWideString("locationpopup::subtitle::empty",
				"formatstring",L"%d people %d events"),
				record.nUsers,record.nEvents);
			popup.titlebar.title += theApp.locationlist.Nobody.description;
			break;
		case State::event:
			_stprintf(buffer,
				env.GetWideString("locationpopup::subtitle::event",
				"formatstring",L"%d people %d events"),
				record.nUsers,record.nEvents);
			popup.titlebar.title += theApp.locationlist.Eventful.description;
			break;
		case State::useroccupied:
			_stprintf(buffer,
				env.GetWideString("locationpopup::subtitle::active",
				"formatstring",L"%d people %d events"),
				record.nUsers,record.nEvents);
			popup.titlebar.title += theApp.locationlist.Active.description;
			break;
	}
	popup.subtitle.title = buffer;
	popup.Redraw();

	// update the associated item's style
	if(item){
		if(editing){
			if(checked)
				item->Set(theApp.locationlist.Checked);
			else
				item->Set(theApp.locationlist.Unchecked);
		} else {
			switch(state){
				case Location::active:
					item->Set(theApp.locationlist.Active);
					break;
				case Location::empty:
					item->Set(theApp.locationlist.Nobody);
					break;
				case Location::event:
					item->Set(theApp.locationlist.Eventful);
					break;
				case Location::useroccupied:
					item->Set(theApp.locationlist.UserOccupied);
					break;
			}
		}
		item->Redraw();
	}
}

void Location::DeepRefresh(int locationId)
{
	if(haspopup && popup.hWnd==NULL){
		popup.Hide();
		popup.Create();
	}

	switch(db.GetCachedLocation(locationId,record)){
		case dbException:
			print(L"Location::DeepRefresh - DB exception\n");
			break;
		case dbInvalidParam:
			print(L"Location::DeepRefresh - Unknown location (locationId=%d)\n",locationId);
			break;
		case dbSuccess:
			if(haspopup)
				popup.DeepRefresh(record.LocationId);
			ShallowRefresh();
			break;
	}		
}

void Location::OnCustomNotify(int id, Notification& n)
{
	switch(id){
	case bcLocation:
		switch(n.reason){
		case bcModified:
			db.GetCachedLocation(record.LocationId,record);
		}
		break;
	case bcUser:
		break;
	}

	ShallowRefresh();
}
