//=============================================================================
// ComputerScreenATMWithdraw
//=============================================================================

class MMRPGATMMoney expands ComputerUIWindow;

var MenuUISmallLabelWindow   winInfo;

var MenuUIActionButtonWindow btnWithdraw;
var MenuUIActionButtonWindow btnDeposit;
var MenuUIEditWindow         editBalance;
var MenuUIEditWindow         editCardBalance;
var MenuUIEditWindow         editWithdraw;
var MenuUIEditWindow         editDeposit;

var localized String ButtonLabelWithdraw;
var localized String ButtonLabelDeposit;
var localized String BalanceLabel;
var localized String WithdrawAmountLabel;
var localized String InvalidAmountLabel;
var localized String InsufficientCreditsLabel;
var localized String CreditsWithdrawnLabel;
var localized string CreditsDepositedLabel;

var localized String ButtonLabelItems;
var MenuUIActionButtonWindow btnItems;

// ----------------------------------------------------------------------
// CreateControls()
// ----------------------------------------------------------------------

function CreateControls()
{
	Super.CreateControls();

	btnItems = winButtonBar.AddButton(ButtonLabelItems, HALIGN_Right);
	btnWithdraw = winButtonBar.AddButton(ButtonLabelWithdraw, HALIGN_Right);
	btnDeposit = winButtonBar.AddButton(ButtonLabelDeposit, HALIGN_Right);

	CreateMenuLabel(20, 31, "Bank Balance:", winClient);
	CreateMenuLabel(20, 61, "Card Balance:", winClient);
	CreateMenuLabel(20, 91, "Amount To Withdraw:", winClient);
	CreateMenuLabel(20, 121, "Amount To Deposit:", winClient);

	editBalance  = CreateMenuEditWindow(231, 29, 143, 10, winClient);
	editBalance.SetSensitivity(False);	// cannot edit balance!!

	editCardBalance  = CreateMenuEditWindow(231, 59, 143, 10, winClient);
	editCardBalance.SetSensitivity(False);	// cannot edit balance!!

	editWithdraw = CreateMenuEditWindow(231, 89, 143, 10, winClient);
	editDeposit = CreateMenuEditWindow(231, 119, 143, 10, winClient);

	CreateInfoWindow();

	winTitle.SetTitle(Title);
}


// ----------------------------------------------------------------------
// CreateInfoWindow()
// ----------------------------------------------------------------------

function CreateInfoWindow()
{
	winInfo = MenuUISmallLabelWindow(winClient.NewChild(Class'MenuUISmallLabelWindow'));

	winInfo.SetPos(8, 152);
	winInfo.SetSize(385, 25);
	winInfo.SetTextAlignments(HALIGN_Center, VALIGN_Center);
	winInfo.SetTextMargins(0, 0);
}

// ----------------------------------------------------------------------
// SetCompOwner()
// ----------------------------------------------------------------------

function SetCompOwner(ElectronicDevices newCompOwner)
{
	Super.SetCompOwner(newCompOwner);

	UpdateBalance();

	EnableButtons();
	SetFocusWindow(editWithdraw);
}

// ----------------------------------------------------------------------
// UpdateBalance()
// ----------------------------------------------------------------------

function UpdateBalance()
{
	_MMRPGPlayer(Player).GetOwnedCred();

	editBalance.SetText(String(_MMRPGPlayer(Player).BankMoney));
	editCardBalance.SetText(String(_MMRPGPlayer(Player).CredMoney));
}

// ----------------------------------------------------------------------
// ButtonActivated()
// ----------------------------------------------------------------------

function bool ButtonActivated( Window buttonPressed )
{
	local bool bHandled;

	if(MMRPGATMTerminal(WinTerm).moneytime > 0)
	{
		return false;
	}

	bHandled = True;

	switch( buttonPressed )
	{
		case btnWithdraw:
			WithdrawCredits();
			break;

		case btnDeposit:
			DepositCredits();
			break;

		case btnItems:
			MMRPGATMTerminal(WinTerm).ShowScreen(Class'Contender.MMRPGATMScreen');
			break;

		default:
			bHandled = False;
			break;
	}


	if (bHandled)
		return True;
	else
		return False;
}

// ----------------------------------------------------------------------
// EditActivated()
// ----------------------------------------------------------------------

event bool EditActivated(window edit, bool bModified)
{
	//WithdrawCredits();
}

// ----------------------------------------------------------------------
// WithdrawCredits()
// ----------------------------------------------------------------------

function WithdrawCredits()
{
	local int numCredits;
	local int balance;
	local bool bWork;

	_MMRPGPlayer(Player).GetOwnedCred();

	numCredits = Int(editWithdraw.GetText());

	// withdrawal
	if (numCredits > 0)
	{
		balance = _MMRPGPlayer(Player).BankMoney;

		if (_MMRPGPlayer(Player).BankMoney >= numCredits)
		{
			balance -= numCredits;
			_MMRPGPlayer(Player).SetBankMoney(balance);
			_MMRPGPlayer(Player).CredMoney += numCredits;
			winInfo.SetText(String(numCredits) @ CreditsWithdrawnLabel);
		}
		else
		{
			winInfo.SetText(InsufficientCreditsLabel);
		}
	}
	else
	{
		winInfo.SetText(InvalidAmountLabel);
	}

	_MMRPGPlayer(Player).MoneyRecord();
	// Blank withdraw box and reset focus to that window
	editWithdraw.SetText("");
	SetFocusWindow(editWithdraw);
	UpdateBalance();
}



function DepositCredits()
{
	local int numCredits;
	local int balance;

	_MMRPGPlayer(Player).GetOwnedCred();

	numCredits = Int(editDeposit.GetText());

	// withdrawal
	if (numCredits > 0)
	{
		balance = _MMRPGPlayer(Player).BankMoney;
		if (_MMRPGPlayer(Player).CredMoney >= numCredits)
		{
			balance += numCredits;
			_MMRPGPlayer(Player).SetBankMoney(balance);
			_MMRPGPlayer(Player).CredMoney -= numCredits;
			winInfo.SetText(String(numCredits) @ CreditsDepositedLabel);
		}
		else
		{
			winInfo.SetText(InsufficientCreditsLabel);
		}
	}
	else
	{
		winInfo.SetText(InvalidAmountLabel);
	}

	_MMRPGPlayer(Player).MoneyRecord();
	// Blank withdraw box and reset focus to that window
	editDeposit.SetText("");
	SetFocusWindow(editWithdraw);
	UpdateBalance();
}

// ----------------------------------------------------------------------
// TextChanged() 
// ----------------------------------------------------------------------

event bool TextChanged(window edit, bool bModified)
{
	EnableButtons();

	return False;
}

// ----------------------------------------------------------------------
// EnableButtons()
// ----------------------------------------------------------------------

function EnableButtons()
{
	local float balance;
	local float bankbalance;

	bankbalance = _MMRPGPlayer(Player).BankMoney;
	balance = _MMRPGPlayer(Player).CredMoney;

	btnWithdraw.SetSensitivity((editWithdraw.GetText() != "") && (bankbalance > 0));

	btnDeposit.SetSensitivity((editDeposit.GetText() != "") && (balance > 0));
}

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
defaultproperties
{
    backgroundStyle=DSTY_Translucent
    ButtonLabelWithdraw="|&Withdraw"
    ButtonLabelDeposit="|&Deposit"
    ButtonLabelItems="|&Items"
    InvalidAmountLabel="INVALID AMOUNT ENTERED"
    InsufficientCreditsLabel="INSUFFICIENT CREDITS"
    CreditsWithdrawnLabel="CREDITS WITHDRAWN"
    CreditsDepositedLabel="CREDITS DEPOSITED"
    escapeAction="LOGOUT"
    Title="MainMan's City Banking System"
    ClientWidth=403
    ClientHeight=211
    verticalOffset=30
    clientTextures(0)=Texture'DeusExUI.UserInterface.ComputerGBSWithdrawBackground_1'
    clientTextures(1)=Texture'DeusExUI.UserInterface.ComputerGBSWithdrawBackground_2'
    textureRows=1
    textureCols=2
    bAlwaysCenter=True
    statusPosY=186
    ComputerNodeFunctionLabel="MainMansCityBank/Money"
}
