/*
		CaPriCe Forever - Amstrad CPC 464/664/6128 emulator
		Copyright (C) 2014-2021 by Frédéric Coste

		This program is free software: you can redistribute it and/or modify
		it under the terms of the GNU General Public License as published by
		the Free Software Foundation, either version 3 of the License, or
		(at your option) any later version.

		This program is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.

		You should have received a copy of the GNU General Public License
		along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <vcl.h>
#pragma hdrstop
#include "TFormPoke.h"
#include "TFormMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


//=================================
// Options

//=================================


#define MIN_ADDRESS			(0U)
#define MAX_ADDRESS			(65535U)
#define MIN_VALUE				(0U)
#define MAX_VALUE				(255U)


TFormPoke *FormPoke;
//---------------------------------------------------------------------------
__fastcall TFormPoke::TFormPoke(TComponent* Owner)
	: TForm(Owner)
{
  mSettingsP = NULL;
	mAddress = MIN_ADDRESS;
	mValue = MIN_VALUE;

  // Apply theme
  Font->Name = "Amstrad CPC464 Regular";
  Font->Size = 6;
	EditAddress->Font->Size = 12;
	EditAddress->Height = 16;
	EditValue->Font->Size = 12;
	EditValue->Height = 16;
	PanelFormatAddress->Font->Size = 12;
	PanelFormatValue->Font->Size = 12;
}
//---------------------------------------------------------------------------


void __fastcall TFormPoke::FormShow(TObject *Sender)
{
	if (FormMain == NULL)
	{
		Close();
		return;
	}

	FormMain->OnPokeFormShow();

	if (mSettingsP)
	{
		mSettingsP->PokeFormVisible = true;
	}

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormPoke::FormHide(TObject *Sender)
{
	if (FormMain != NULL)
	{
		FormMain->OnPokeFormHide();
	}

	if (mSettingsP)
	{
		mSettingsP->PokeFormVisible = false;
	}


	NOT_USED(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TFormPoke::SetSettings(TSettings* Value)
{
  mSettingsP = Value;

	if (    (mSettingsP->PokeFormLeft != 0)
			 || (mSettingsP->PokeFormTop != 0)
			 || (mSettingsP->PokeFormWidth != 0)
       || (mSettingsP->PokeFormHeight != 0) )
  {
    Position = poDesigned;
		Left = mSettingsP->PokeFormLeft;
		Top = mSettingsP->PokeFormTop;
		Width = mSettingsP->PokeFormWidth;
    Height = mSettingsP->PokeFormHeight;
	}

  ShowHint = mSettingsP->ShowHints;

	// Apply theme
	Color = mSettingsP->ThemeBackgroundColor;
	Font->Color = mSettingsP->ThemeText1Color;
	PanelFormatAddress->Font->Color = clBlack;
	PanelFormatValue->Font->Color = clBlack;

	for (int Loop=0; Loop < ComponentCount; Loop++)
	{
		if (Components[Loop]->Name.Pos("Label") == 1)
		{
			((TLabel*)Components[Loop])->Font->Color = mSettingsP->ThemeText1Color;
		}
		else if (Components[Loop]->Name.Pos("PanelButton") == 1)
		{
			((TPanel*)Components[Loop])->Color = mSettingsP->ThemeButtonColor;
			((TPanel*)Components[Loop])->Font->Color = mSettingsP->ThemeButtonTextColor;
		}

		// Refresh display
		if ( (Visible == true) && (((TControl*)Components[Loop])->Visible == true) )
		{
			((TControl*)Components[Loop])->Invalidate();
		}
	}

  if (mSettingsP->HideAll == false)
  {
		Visible = mSettingsP->PokeFormVisible;
  }
}
//---------------------------------------------------------------------------


void __fastcall TFormPoke::WMMove(TMessage &Message)
{
	if ( (Visible == true) && (mSettingsP) )
  {
		mSettingsP->PokeFormLeft = Left;
    mSettingsP->PokeFormTop = Top;
	}

  NOT_USED(Message);
}
//---------------------------------------------------------------------------


void __fastcall TFormPoke::PanelButtonMouseDown(TObject *Sender, TMouseButton Button,
					TShiftState Shift, int X, int Y)
{
	((TPanel*)Sender)->Color = mSettingsP->ThemeButtonTextColor;
	((TPanel*)Sender)->Font->Color = mSettingsP->ThemeButtonColor;

	NOT_USED(Button);
	NOT_USED(Shift);
	NOT_USED(X);
	NOT_USED(Y);
}
//---------------------------------------------------------------------------
void __fastcall TFormPoke::PanelButtonMouseUp(TObject *Sender, TMouseButton Button,
					TShiftState Shift, int X, int Y)
{
	((TPanel*)Sender)->Color = mSettingsP->ThemeButtonColor;
	((TPanel*)Sender)->Font->Color = mSettingsP->ThemeButtonTextColor;

	NOT_USED(Button);
	NOT_USED(Shift);
	NOT_USED(X);
	NOT_USED(Y);
}
//---------------------------------------------------------------------------


void __fastcall TFormPoke::PanelFormatAddressClick(TObject *Sender)
{
	EditAddress->SetFocus();

	if (PanelFormatAddress->Caption == "")
	{
		PanelFormatAddress->Caption = "&&";
		EditAddress->Text = AnsiString::IntToHex(mAddress, 4);
	}
	else
	{
		PanelFormatAddress->Caption = "";
		EditAddress->Text = mAddress;
	}

	EditAddress->SelectAll();

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormPoke::EditAddressChange(TObject *Sender)
{
	if ( (EditAddress->Focused() == true)
			 && (EditAddress->Text.Trim().Length() > 0) )
	{
		// Presence of char '&'
		bool Hexa = (PanelFormatAddress->Caption == "") ? false : true;
		AnsiString StringAddress = EditAddress->Text.Trim();

		try
		{
			if (Hexa == true)
			{
				mAddress = StrToInt("0x" + StringAddress);
			}
			else
			{
				mAddress = StringAddress.ToInt();
			}

			// Check valid value
			if ( (mAddress >= MIN_ADDRESS) && (mAddress <= MAX_ADDRESS)
					 && (mValue >= MIN_VALUE) && (mValue <= MAX_VALUE)
					 && (EditAddress->Text.Trim().Length() > 0)
					 && (EditValue->Text.Trim().Length() > 0) )
			{
				PanelButtonPoke->Visible = true;
			}
			else
			{
				PanelButtonPoke->Visible = false;
			}
		}
		catch(...)
		{
			mAddress = 0U;
			PanelButtonPoke->Visible = false;
		}
	}
	else if (EditAddress->Text.Trim().Length() == 0)
	{
		mAddress = 0U;
		PanelButtonPoke->Visible = false;
	}

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------



void __fastcall TFormPoke::PanelFormatValueClick(TObject *Sender)
{
	EditValue->SetFocus();

	if (PanelFormatValue->Caption == "")
	{
		PanelFormatValue->Caption = "&&";
		EditValue->Text = AnsiString::IntToHex(mValue, 2);
	}
	else
	{
		PanelFormatValue->Caption = "";
		EditValue->Text = mValue;
	}

	EditValue->SelectAll();

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormPoke::EditValueChange(TObject *Sender)
{
	if ( (EditValue->Focused() == true)
			 && (EditValue->Text.Trim().Length() > 0) )
	{
		// Presence of char '&'
		bool Hexa = (PanelFormatValue->Caption == "") ? false : true;
		AnsiString StringValue = EditValue->Text.Trim();

		try
		{
			if (Hexa == true)
			{
				mValue = StrToInt("0x" + StringValue);
			}
			else
			{
				mValue = StringValue.ToInt();
			}

			// Check valid value
			if ( (mAddress >= MIN_ADDRESS) && (mAddress <= MAX_ADDRESS)
					 && (mValue >= MIN_VALUE) && (mValue <= MAX_VALUE)
					 && (EditAddress->Text.Trim().Length() > 0)
					 && (EditValue->Text.Trim().Length() > 0) )
			{
				PanelButtonPoke->Visible = true;
			}
			else
			{
				PanelButtonPoke->Visible = false;
			}
		}
		catch(...)
		{
			mValue = 0U;
			PanelButtonPoke->Visible = false;
		}
	}
	else if (EditValue->Text.Trim().Length() == 0)
	{
		mValue = 0U;
		PanelButtonPoke->Visible = false;
	}

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormPoke::PanelButtonPokeClick(TObject *Sender)
{
	Engine_WriteBankedWrite(FormMain->NativeCPC, mAddress, mValue);

	FormMain->Refresh();

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFormPoke::FormKeyPress(TObject *Sender, System::WideChar &Key)
{
  if (Key == VK_RETURN)
  {
		PanelButtonPoke->OnClick(PanelButtonPoke);
		Key = 0; // Stop key press propagation to focused control
	}
	else if (Key == '+')
	{
		if (EditAddress->Focused() == true)
		{
			if (mAddress < MAX_ADDRESS) mAddress++;

			if (PanelFormatAddress->Caption == "&&")
			{
				EditAddress->Text = AnsiString::IntToHex(mAddress, 4);
			}
			else
			{
				EditAddress->Text = mAddress;
			}
			EditAddress->SelectAll();

			Key = 0; // Stop key press propagation to focused control
		}
		else if (EditValue->Focused() == true)
		{
			if (mValue < MAX_VALUE) mValue++;

			if (PanelFormatValue->Caption == "&&")
			{
				EditValue->Text = AnsiString::IntToHex(mValue, 2);
			}
			else
			{
				EditValue->Text = mValue;
			}
			EditValue->SelectAll();

			Key = 0; // Stop key press propagation to focused control
		}
		else
		{
		}
	}
	else if (Key == '-')
	{
		if (EditAddress->Focused() == true)
		{
			if (mAddress > MIN_ADDRESS) mAddress--;

			if (PanelFormatAddress->Caption == "&&")
			{
				EditAddress->Text = AnsiString::IntToHex(mAddress, 4);
			}
			else
			{
				EditAddress->Text = mAddress;
			}
			EditAddress->SelectAll();

			Key = 0; // Stop key press propagation to focused control
		}
		else if (EditValue->Focused() == true)
		{
			if (mValue > MIN_VALUE) mValue--;

			if (PanelFormatValue->Caption == "&&")
			{
				EditValue->Text = AnsiString::IntToHex(mValue, 2);
			}
			else
			{
				EditValue->Text = mValue;
			}
			EditValue->SelectAll();

			Key = 0; // Stop key press propagation to focused control
		}
		else
		{
		}
	}

	NOT_USED(Sender);
}
//---------------------------------------------------------------------------

