#include "StdAfx.h"
#include "editprofilewindow.h"

#define STATUS(x) status.SetText(env.GetWideString("editprofile::status",x,_T(x)))

EditProfileWindow::EditProfileWindow(void)
{
}

EditProfileWindow::~EditProfileWindow(void)
{
}

int EditProfileWindow::Create(HWND parent)
{
	Load("editprofile");
	more.Load("editprofile::more");
	update.Load("editprofile::update");
	cancel.Load("editprofile::cancel");
	weapons.Load("editprofile::weapons");
	interests.Load("editprofile::interests");
	background.Load("editprofile::background");
	wpcheck.Load("editprofile::weapons_check");
	itcheck.Load("editprofile::interests_check");
	bgcheck.Load("editprofile::background_check");
	login.Load("editprofile::login");
	name.Load("editprofile::name");
	year.Load("editprofile::year");
	status.Load("editprofile::status");

	StaticWindow::Create(parent);
	more.Create(hWnd);
	update.Create(hWnd);
	cancel.Create(hWnd);
	weapons.Create(hWnd,ES_LEFT|ES_MULTILINE);
	interests.Create(hWnd,ES_LEFT|ES_MULTILINE);
	background.Create(hWnd,ES_LEFT|ES_MULTILINE);
	wpcheck.Create(hWnd);
	itcheck.Create(hWnd);
	bgcheck.Create(hWnd);
	login.Create(hWnd,ES_READONLY);
	name.Create(hWnd,ES_READONLY);
	year.Create(hWnd,ES_READONLY);

	status.Create(hWnd,ES_LEFT|ES_READONLY);
	status.Disable();

	login.Disable();
	name.Disable();
	year.Disable();

	background.SetTabTo(weapons.hEditCtrl);
	weapons.SetTabTo(interests.hEditCtrl);
	interests.SetTabTo(background.hEditCtrl);

	STATUS("DefaultStatus");

	return 1;
}

void EditProfileWindow::DeepRefresh()
{
	db.GetCachedUser(theApp.user.UserId,record);
 
	login.SetText(record.UserLogin);
	name.SetText(record.FirstName);
	name.AppendText(L" ");
	name.AppendText(record.LastName);

	std::wstring tmp;
	db.GetYear(record.YearId,tmp);
	year.SetText(tmp.data());

	weapons.SetText(record.SecretWeapon);
	interests.SetText(record.Interest);
	background.SetText(record.Background);

	wpcheck.SetValue(0);
	itcheck.SetValue(0);
	bgcheck.SetValue(0);

	STATUS("DefaultStatus");

}

void EditProfileWindow::OnButton(HWND h)
{
	if(cancel.hWnd==h){
		Hide();
	} else if(h==update.hWnd){

		rpc.BeginTransaction();

		STATUS("UpdatingProfile");
		DoEvents();

		record.Background = background.Data();
		record.Interest = interests.Data();
		record.SecretWeapon = weapons.Data();
		db.UpdateUser(record);

		// optionally publish events
		if(wpcheck.checked){
			STATUS("PublishingSecretWeapon");
			DoEvents();

			EventRecord er;

			TCHAR buffer[100];
			_stprintf(buffer,L"%s's weapons",(TCHAR*)record.UserLogin);

			// see if it exists already
			db.GetEventId(buffer,er.EventId);

			er.EventName = buffer;
			er.Eternal = 1;
			er.Description = weapons.Data();
			er.OwnerId = theApp.user.UserId;
			er.Private = 0;

			if(er.EventId==NullEventId){
				db.CreateEvent(er);
				rpc.ServerNotify(bcEvent,er.EventId,bcAdd,theApp.user.UserId);
			} else {
				db.UpdateEvent(er);
				rpc.ServerNotify(bcEvent,er.EventId,bcModified,theApp.user.UserId);
			}
		} else {
			STATUS("UnpublishingSecretWeapon");
			DoEvents();

			TCHAR buffer[100];
			_stprintf(buffer,L"%s's weapons",(TCHAR*)record.UserLogin);
			int eventId;
			db.GetEventId(buffer,eventId);
			db.DeleteEvent(eventId);
			rpc.ServerNotify(bcEvent,eventId,bcRemove,theApp.user.UserId);
		}

		if(itcheck.checked){
			STATUS("PublishingInterests");
			DoEvents();

			EventRecord er;
			TCHAR buffer[100];
			_stprintf(buffer,L"%s's interests",(TCHAR*)record.UserLogin);

			// see if it exists already
			db.GetEventId(buffer,er.EventId);

			er.EventName = buffer;
			er.Eternal = 1;
			er.Description = interests.Data();
			er.OwnerId = theApp.user.UserId;
			er.Private = 0;

			if(er.EventId==NullEventId){
				db.CreateEvent(er);
				rpc.ServerNotify(bcEvent,er.EventId,bcAdd,theApp.user.UserId);
			}else {
				db.UpdateEvent(er);
				rpc.ServerNotify(bcEvent,er.EventId,bcModified,theApp.user.UserId);
			}
		} else {
			STATUS("UnpublishingInterests");
			DoEvents();

			TCHAR buffer[100];
			_stprintf(buffer,L"%s's interests",(TCHAR*)record.UserLogin);
			int eventId;
			db.GetEventId(buffer,eventId);
			db.DeleteEvent(eventId);
			rpc.ServerNotify(bcEvent,eventId,bcRemove,theApp.user.UserId);
		}

		if(bgcheck.checked){
			STATUS("PublishingBackground");
			DoEvents();

			EventRecord er;
			TCHAR buffer[100];
			_stprintf(buffer,L"%s's background",(TCHAR*)record.UserLogin);

			// see if it exists already
			db.GetEventId(buffer,er.EventId);

			er.EventName = buffer;
			er.Eternal = 1;
			er.Description = background.Data();
			er.OwnerId = theApp.user.UserId;
			er.Private = 0;

			if(er.EventId==NullEventId){
				db.CreateEvent(er);
				rpc.ServerNotify(bcEvent,er.EventId,bcAdd,theApp.user.UserId);
			}else {
				db.UpdateEvent(er);
				rpc.ServerNotify(bcEvent,er.EventId,bcModified,theApp.user.UserId);
			}
		} else {
			STATUS("UnpublishingBackground");
			DoEvents();

			TCHAR buffer[100];
			_stprintf(buffer,L"%s's background",(TCHAR*)record.UserLogin);
			int eventId;
			db.GetEventId(buffer,eventId);
			db.DeleteEvent(eventId);
			rpc.ServerNotify(bcEvent,eventId,bcRemove,theApp.user.UserId);
		}

		Hide();

		rpc.EndTransaction();

	} else if(h==more.hWnd){
			STATUS("not implemented yet");
	}
}