/*
    CaPriCe Forever - Amstrad CPC 464/664/6128 emulator
    Copyright (C) 2014-2016 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 "TFormDiskCatalog.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormDiskCatalog *FormDiskCatalog;
//---------------------------------------------------------------------------
__fastcall TFormDiskCatalog::TFormDiskCatalog(TComponent* Owner)
  : TForm(Owner)
{
  mNbMinEntries = ListBoxCatalogEntries->Items->Count;
  mSizeDiff = ClientHeight - ListBoxCatalogEntries->Height;
  mUser = 0;
  CheckBoxReset->Checked = true;

  // Apply theme
  Font->Name = "Amstrad CPC464 Regular";
  Font->Size = 6;
  //PanelFormatUser->Font->Size = 12;
  PanelFormatUser->Font->Color = clBlack;
  //EditUser->Font->Size = 12;
  EditUser->Height = 10;
  EditUser->Font->Color = clBlack;
}
//---------------------------------------------------------------------------
void __fastcall TFormDiskCatalog::FormShow(TObject *Sender)
{
  mRUNRequested = false;
  mLOADRequested = false;
  mCPMRequested = false;

  if (mEmulatorP == 0)
  {
    Close();
    return;
  }

  // Apply theme
  if (mSettingsP)
  {
    Color = mSettingsP->ThemeBackgroundColor;
    LabelUser->Font->Color = mSettingsP->ThemeText1Color;
  }

  EditUser->Text = AnsiString(mUser);

  PopulateCatalogue();

  // Adapt list size
  int NbEntries = ListBoxCatalogEntries->Items->Count;
  if (NbEntries > 16) NbEntries = 16;
  if (NbEntries < mNbMinEntries) NbEntries = mNbMinEntries;
  ClientHeight = NbEntries * ListBoxCatalogEntries->ItemHeight + mSizeDiff;

  ActiveControl = ListBoxCatalogEntries;

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


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

  ShowHint = mSettingsP->ShowHints;

  // Apply theme
  Color = mSettingsP->ThemeBackgroundColor;
  ListBoxCatalogEntries->Color = mSettingsP->ThemeBackgroundColor;
  Font->Color = mSettingsP->ThemeText1Color;
  GroupBoxCatalogue->Font->Color = mSettingsP->ThemeText2Color;
  PanelCancel->Color = mSettingsP->ThemeButtonColor;
  PanelCancel->Font->Color = mSettingsP->ThemeButtonTextColor;
  PanelRUN->Color = mSettingsP->ThemeButtonColor;
  PanelRUN->Font->Color = mSettingsP->ThemeButtonTextColor;
  PanelLOAD->Color = mSettingsP->ThemeButtonColor;
  PanelLOAD->Font->Color = mSettingsP->ThemeButtonTextColor;
  PanelCPM->Color = mSettingsP->ThemeButtonColor;
  PanelCPM->Font->Color = mSettingsP->ThemeButtonTextColor;
}
//---------------------------------------------------------------------------


AnsiString __fastcall TFormDiskCatalog::GetSelectedFilename(void)
{
  return ListBoxCatalogEntries->Items->Strings[ListBoxCatalogEntries->ItemIndex];
}
//---------------------------------------------------------------------------


void __fastcall TFormDiskCatalog::PanelRUNClick(TObject *Sender)
{
	mRUNRequested = true;
  ModalResult = mrOk;

  NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormDiskCatalog::PanelLOADClick(TObject *Sender)
{
	mLOADRequested = true;
  ModalResult = mrOk;

  NOT_USED(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormDiskCatalog::PanelCPMClick(TObject *Sender)
{
  mCPMRequested = true;
  ModalResult = mrOk;

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


void __fastcall TFormDiskCatalog::ListBoxCatalogEntriesDblClick(
      TObject *Sender)
{
  mRUNRequested = true;
  ModalResult = mrOk;

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

void __fastcall TFormDiskCatalog::FormKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  if (Key == VK_F1)
  {
    PanelRUNClick(Sender);
  }
  else if (Key == VK_F2)
  {
    PanelLOADClick(Sender);
  }
  else if (Key == VK_F3)
  {
    PanelCPMClick(Sender);
  }
  else if (Key == VK_RETURN)
  {
    if (PanelRUN->Visible == true)
    {
      PanelRUNClick(Sender);
    }
    else
    {
      PanelCPMClick(Sender);
    }
  }

  NOT_USED(Shift);
}
//---------------------------------------------------------------------------

void __fastcall TFormDiskCatalog::FormKeyPress(TObject *Sender, System::WideChar &Key)
{
  if (Key == VK_ESCAPE)
  {
    PanelCancelClick(Sender);
  }
}
//---------------------------------------------------------------------------


void __fastcall TFormDiskCatalog::ListBoxCatalogEntriesDrawItem(TWinControl *Control,
          int Index, TRect &Rect, TOwnerDrawState State)
{
  TCanvas* Canvas = ListBoxCatalogEntries->Canvas;

  if (State.Contains(odSelected))
  {
    Canvas->Font->Color = clWhite;
  }
  else
  {
    Canvas->Brush->Color = mSettingsP->ThemeBackgroundColor;
    Canvas->Font->Color = mSettingsP->ThemeText1Color;
  }

  // Draw cell
  Canvas->FillRect(Rect);
  AnsiString Text = ListBoxCatalogEntries->Items->Strings[Index];
  int TextHeight = Canvas->TextHeight(Text);
  int TextPos = (ListBoxCatalogEntries->ItemHeight - TextHeight) / 2;
  Canvas->TextOut(Rect.Left+5, Rect.Top+TextPos, Text);
  if (State.Contains(odFocused))
  {
    Canvas->DrawFocusRect(Rect);
  }

  NOT_USED(Index);
  NOT_USED(Control);
}
//---------------------------------------------------------------------------


void __fastcall TFormDiskCatalog::FormMouseWheelUp(TObject *Sender, TShiftState Shift,
          TPoint &MousePos, bool &Handled)
{
  if (ListBoxCatalogEntries->ItemIndex > 0)
  {
    ListBoxCatalogEntries->ItemIndex--;
  }

  NOT_USED(Sender);
  NOT_USED(Shift);
  NOT_USED(MousePos);
  NOT_USED(Handled);
}
//---------------------------------------------------------------------------

void __fastcall TFormDiskCatalog::FormMouseWheelDown(TObject *Sender, TShiftState Shift,
          TPoint &MousePos, bool &Handled)
{
  if (ListBoxCatalogEntries->ItemIndex < (ListBoxCatalogEntries->Items->Count-1))
  {
    ListBoxCatalogEntries->ItemIndex++;
  }

  NOT_USED(Sender);
  NOT_USED(Shift);
  NOT_USED(MousePos);
  NOT_USED(Handled);
}
//---------------------------------------------------------------------------

void __fastcall TFormDiskCatalog::PanelCancelClick(TObject *Sender)
{
  ModalResult = mrCancel;

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

void __fastcall TFormDiskCatalog::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 TFormDiskCatalog::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 TFormDiskCatalog::PanelFormatUserClick(TObject *Sender)
{
  if (PanelFormatUser->Caption == "")
  {
    PanelFormatUser->Caption = "&&";
    EditUser->Text = AnsiString::IntToHex(mUser, 2);
  }
  else
  {
    PanelFormatUser->Caption = "";
    EditUser->Text = mUser;
  }

  EditUser->SetFocus();
  EditUser->SelectAll();

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

void __fastcall TFormDiskCatalog::EditUserChange(TObject *Sender)
{
  if (EditUser->Text.Length() > 0)
  {
    try
    {
      if (PanelFormatUser->Caption == "")
      {
        mUser = EditUser->Text.ToInt();
      }
      else
      {
        mUser = StrToInt("0x" + EditUser->Text);
      }

      PopulateCatalogue();
    }
    catch (...)
    {
      mUser = 0;
    }
  }
  else
  {
    // Clear catalogue
    ListBoxCatalogEntries->Clear();
  }

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


void __fastcall TFormDiskCatalog::PopulateCatalogue(void)
{
  // Populate files list
  ListBoxCatalogEntries->Clear();
  mEmulatorP->ReadDiskCatalogue(DriveA, mUser);
  for (int Loop=0; Loop < mEmulatorP->NbCatalogEntries; Loop++)
  {
    ListBoxCatalogEntries->Items->Add(mEmulatorP->CatalogEntry[Loop]);
  }

  // Select Autostart entry
  AnsiString AutoStartCommand = mEmulatorP->DetectAutoStart(DriveA, User);
  if (AutoStartCommand.Pos("RUN\"") > 0)
  {
    AnsiString AutoStartEntry = AutoStartCommand.SubString(5, AutoStartCommand.Length() - 5);
    ListBoxCatalogEntries->ItemIndex = ListBoxCatalogEntries->Items->IndexOf(AutoStartEntry);
  }

  // Prepare controls
  if (ListBoxCatalogEntries->Items->Count)
  {
    if (ListBoxCatalogEntries->ItemIndex == -1)
    {
      ListBoxCatalogEntries->ItemIndex = 0;
    }

    PanelRUN->Visible = true;
    PanelLOAD->Visible = true;
  }
  else
  {
    PanelRUN->Visible = false;
    PanelLOAD->Visible = false;
  }
}
//---------------------------------------------------------------------------

