#include "stdafx.h"
#include "NewAccountWindow.h"
#include "SecurityV1.h"
#include "SecurityV2.h"

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

NewAccountWindow::NewAccountWindow()
{

}

NewAccountWindow::~NewAccountWindow()
{

}

int NewAccountWindow::Create(HWND parent)
{
	Load("NewAccountWindow");

	UserLogin.Load("NewAccountWindow::UserLogin");
	FirstName.Load("NewAccountWindow::FirstName");
	LastName.Load("NewAccountWindow::LastName");
	Email.Load("NewAccountWindow::Email");
	Year.Load("NewAccountWindow::Year");
	Password1.Load("NewAccountWindow::Password1");
	Password2.Load("NewAccountWindow::Password2");

	YearStyle.Load("NewAccountWindow::Year::ItemStyle");
	Year.scrollbar.Load("NewAccountWindow::ScrollBar");

	ok.Load("NewAccountWindow::ok");
	cancel.Load("NewAccountWindow::cancel");
	status.Load("NewAccountWindow::status");

	StaticWindow::Create(parent);
	UserLogin.Create(hWnd);
	FirstName.Create(hWnd);
	LastName.Create(hWnd);
	Email.Create(hWnd);
	Year.Create(hWnd);
	Password1.Create(hWnd,ES_PASSWORD);
	Password2.Create(hWnd,ES_PASSWORD);

	ok.Create(hWnd);
	cancel.Create(hWnd);
	status.Create(hWnd,ES_LEFT|ES_READONLY);

	TabOrder tab;
	tab.Add(UserLogin);
	tab.Add(FirstName);
	tab.Add(LastName);
	tab.Add(Email);
	tab.Add(Password1);
	tab.Add(Password2);
	tab.Commit();

	return 1;
}

void NewAccountWindow::DeepRefresh()
{
	Vector<YearRecord> years;
	int i;
	ListItem * item;

	Year.DeleteAllItems();
	db.GetCachedYears(years);
	item = Year.CreateItem(L"No Year",NullYearId);
	item->Set(YearStyle);
	Year.AddItem(item);
	for(i=0;i<years.length;i++){
		item = Year.CreateItem((TCHAR*)years[i].DegreeYear,years[i].YearId);
		item->Set(YearStyle);
		Year.AddItem(item);
	}
}

void NewAccountWindow::ClearContents()
{
	UserLogin.SetText(L"");
	FirstName.SetText(L"");
	LastName.SetText(L"");
	Email.SetText(L"");
	Year.ClearSelection();
	Password1.SetText(L"");
	Password2.SetText(L"");

	STATUS("DefaultStatus");
}

void NewAccountWindow::OnButton(HWND h)
{
	if(ok.hWnd==h){
		record.UserLogin = UserLogin.Data();
		record.FirstName = FirstName.Data();
		record.LastName = LastName.Data();
		record.Email = Email.Data();

		if(Year.selectedItem){
			record.YearId = Year.selectedItem->key;
		} else {
			STATUS("NeedYear");
			return;
		}

		std::wstring p1 = Password1.Data();
		std::wstring p2 = Password2.Data();
		if(p1!=p2){
			STATUS("PasswordMismatch");
			Password1.SetText(L"");
			Password2.SetText(L"");
			return;
		}

		if(p1.length()>15){
			STATUS("PasswordTooLong");
			Password1.SetText(L"");
			Password2.SetText(L"");
			return;
		}

		record.Password = securityV2.Encrypt(p1.data(),p1.data());

		// make sure this username isn't already taken
		int id;
		db.GetUserId(record.UserLogin,id);
		if(id!=NullUserId){
			STATUS("UsernameExists");
			UserLogin.SetText(L"");
			return;
		}

		STATUS("CreatingUser");
		DoEvents();
		db.CreateUser(record);

		theApp.loginwindow.username.SetText(record.UserLogin);

		Hide();
		ClearContents();
	} else if(cancel.hWnd==h){
		Hide();
		ClearContents();
	}
}