#include "stdafx.h"
#include "MapUser.h"

Appearance MapUser::ActiveWireless;
Appearance MapUser::IdleWireless;
Appearance MapUser::ActiveWired;
Appearance MapUser::IdleWired;
Appearance MapUser::AwayWired;
Appearance MapUser::AwayWireless;

Appearance MapUser::ActiveWirelessSelf;
Appearance MapUser::IdleWirelessSelf;
Appearance MapUser::ActiveWiredSelf;
Appearance MapUser::IdleWiredSelf;
Appearance MapUser::AwayWiredSelf;
Appearance MapUser::AwayWirelessSelf;

Appearance MapUser::ActiveWirelessBuddy;
Appearance MapUser::IdleWirelessBuddy;
Appearance MapUser::ActiveWiredBuddy;
Appearance MapUser::IdleWiredBuddy;
Appearance MapUser::AwayWiredBuddy;
Appearance MapUser::AwayWirelessBuddy;

MapUser::MapUser()
{
	ml = NULL;
	mus = gone;
	record.UserId = NullUserId;
	record.LocationId = NullLocationId;
}

MapUser::~MapUser()
{

}

// returns TRUE if the user's appearance has changed
int MapUser::Update()
{
	MapUserState newmus = unknown;

	// remove the user from his old location
	if(ml) ml->RemoveUser(this);

	// get the new location
	ml = theApp.mapwindow.FindLocation(record.LocationId);

	if(ml==NULL || !record.LoggedIn){ // unknown location
		Hide();
		newmus = gone;
	} else {
		Show();

		// determine where on the map the user's icon will display
		ml->AddUser(this);

		// now determine their visual appearance
		TCHAR buffer[255];
		_stprintf(buffer,L"%s %s\n",(TCHAR*)record.FirstName,(TCHAR*)record.LastName);
		title = buffer;
		// based on his wireless vs. wired, active vs. idle vs. away, and buddy vs. not buddy state
		if(record.UserId==theApp.user.UserId){
			if(record.Away){
				if(ml->record.Wireless) {
					Set(AwayWirelessSelf);
					newmus = awaywireless;
				} else {
					Set(AwayWiredSelf);
					newmus = awaywired;
				}
			} else if(record.Idle){
				if(ml->record.Wireless) {
					Set(IdleWirelessSelf);
					newmus = idlewireless;
				} else {
					Set(IdleWiredSelf);
					newmus = idlewired;
				}
			} else {
				if(ml->record.Wireless){
					Set(ActiveWirelessSelf);
					newmus = activewireless;
				} else {
					Set(ActiveWiredSelf);	
					newmus = activewired;
				}
			}			
		} else {
			Vector<int> buddies;
			db.GetBuddyList(theApp.user.UserId,buddies);
			
			if(buddies.find(record.UserId)!=-1){
				if(record.Away){
					if(ml->record.Wireless) {
						Set(AwayWirelessBuddy);
						newmus = awaywirelessbuddy;
					} else {
						Set(AwayWiredBuddy);
						newmus = awaywiredbuddy;
					}
				} else if(record.Idle){
					if(ml->record.Wireless) {
						Set(IdleWirelessBuddy);
						newmus = idlewirelessbuddy;
					} else {
						Set(IdleWiredBuddy);
						newmus = idlewiredbuddy;
					}
				} else {
					if(ml->record.Wireless){
						Set(ActiveWirelessBuddy);
						newmus = activewirelessbuddy;
					} else {
						Set(ActiveWiredBuddy);	
						newmus = activewiredbuddy;
					}
				}
			} else {
				if(record.PrivacyLevel==bcVisibleToBuddies){
					// take the user off the map
					ml->RemoveUser(this);
				} else {
					if(record.Away){
						if(ml->record.Wireless) {
							Set(AwayWireless);
							newmus = awaywireless;
						} else {
							Set(AwayWired);
							newmus = awaywired;
						}
					} else if(record.Idle){
						if(ml->record.Wireless){
							Set(IdleWireless);
							newmus = idlewireless;
						} else {
							Set(IdleWired);
							newmus = idlewired;
						}
					} else {
						if(ml->record.Wireless){
							Set(ActiveWireless);
							newmus = activewireless;
						} else {
							Set(ActiveWired);	
							newmus = activewired;
						}
					}
				}
			}
		}
	}

	if(newmus!=mus){
		mus = newmus;
		return 1;
	} else {
		mus = newmus;
		return 0;
	}
}

void MapUser::LoadStyles(const char * base)
{
	_bstr_t b = base;

	ActiveWireless.Load(b+"::ActiveWireless");
	IdleWireless.Load(b+"::IdleWireless");
	ActiveWired.Load(b+"::ActiveWired");
	IdleWired.Load(b+"::IdleWired");
	AwayWireless.Load(b+"::AwayWireless");
	AwayWired.Load(b+"::AwayWired");

	ActiveWirelessBuddy.Load(b+"::ActiveWirelessBuddy");
	IdleWirelessBuddy.Load(b+"::IdleWirelessBuddy");
	ActiveWiredBuddy.Load(b+"::ActiveWiredBuddy");
	IdleWiredBuddy.Load(b+"::IdleWiredBuddy");
	AwayWirelessBuddy.Load(b+"::AwayWirelessBuddy");
	AwayWiredBuddy.Load(b+"::AwayWiredBuddy");

	ActiveWirelessSelf.Load(b+"::ActiveWirelessSelf");
	IdleWirelessSelf.Load(b+"::IdleWirelessSelf");
	ActiveWiredSelf.Load(b+"::ActiveWiredSelf");
	IdleWiredSelf.Load(b+"::IdleWiredSelf");
	AwayWirelessSelf.Load(b+"::AwayWirelessSelf");
	AwayWiredSelf.Load(b+"::AwayWiredSelf");
}


void MapUser::SetState(AppearanceState s)
{
	if(s!=visual.state){
		visual.SetState(s);
		if(!hidden){
			//theApp.mapwindow.UpdateMapAround(this);
			if(s == AppearanceState::mouseover)
				theApp.mapwindow.SelectUser(record.UserId);
			theApp.mapwindow.UpdateMap();
		}
	}
}