unit uPrefs;

{
This code is part of the TiCodEd Application, designed to develop for the
TI-99/4a Homecomputer in a modern, structured way.

BSD License
Copyright (c) 2026, Stefan Bauch, TiCodEd <at> lizardware <dot> de


Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons,
  ExtCtrls, Spin, EditBtn, ComCtrls, SpinEx, ExtendedNotebook;

type

  { TFormPrefs }

  TFormPrefs = class(TForm)
    BitBtn1: TBitBtn;
    BitBtnOK: TBitBtn;
    BnPatch: TButton;
    cbAsmList: TCheckBox;
    CbDevice: TComboBox;
    CbMemoryExp: TCheckBox;
    cbPatch: TComboBox;
    cbXBtoClipboard: TCheckBox;
    DirEditFIAD: TDirectoryEdit;
    DirEditInc: TDirectoryEdit;
    EdAssembler: TFileNameEdit;
    EdIntDel: TLabeledEdit;
    EdIntStr: TLabeledEdit;
    edGeminiURL: TEdit;
    edGeminiAPI: TEdit;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBoxAppearance: TGroupBox;
    GroupBoxAssembler: TGroupBox;
    GroupBoxEnvironment: TGroupBox;
    GroupBoxIntegration: TGroupBox;
    Label1: TLabel;
    LabelExample: TLabel;
    LabelFontsize: TLabel;
    pcSettings: TPageControl;
    Panel1: TPanel;
    rgWinFile: TRadioGroup;
    RGIntTarget: TRadioGroup;
    SpinEditFontSize: TSpinEditEx;
    SpinEditTimeout: TSpinEdit;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    stFIAD: TStaticText;
    stlibeay: TStaticText;
    stssleay: TStaticText;
    stTimeout: TStaticText;
    tsGemini: TTabSheet;
    tsCompiler: TTabSheet;
    tsGeneral: TTabSheet;
    tsIntegration: TTabSheet;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtnOKClick(Sender: TObject);
    procedure BnPatchClick(Sender: TObject);
    procedure DirEditIncExit(Sender: TObject);
    procedure EdAssemblerExit(Sender: TObject);
    procedure EdIntDelExit(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RGIntTargetClick(Sender: TObject);
    procedure SpinEditFontSizeChange(Sender: TObject);
    procedure StaticText4Click(Sender: TObject);
    procedure tsGeminiShow(Sender: TObject);
  private

  public

  end;

var
  FormPrefs: TFormPrefs;

  FontSize : Integer;

implementation

{$R *.lfm}

{ TFormPrefs }

uses IniFiles,FileUtil,StrUtils,uWinKeySend,LCLIntf,DynLibs;

var OldIncPath : String;

procedure TFormPrefs.SpinEditFontSizeChange(Sender: TObject);
begin
  LabelExample.Font.Size:=SpinEditFontSize.value;
end;

procedure TFormPrefs.StaticText4Click(Sender: TObject);
begin
  OpenURL('https://aistudio.google.com/apikey')
end;

procedure TFormPrefs.tsGeminiShow(Sender: TObject);
var s:string;
    LibHandle: TLibHandle;

begin
  {$IFDEF WINDOWS}
  s:=FileSearch('ssleay32.dll',GetEnvironmentVariable('PATH'));
  if s='' then begin
    stssleay.Caption:='ssleay32.dll not found';
    stssleay.Font.Color:=clRed;
  end
  else
  begin
    stssleay.Caption:='ssleay32.dll found: '+s;
    stssleay.Font.Color:=clGreen;
  end;
  s:=FileSearch('libeay32.dll',GetEnvironmentVariable('PATH'));
  if s='' then begin
    stlibeay.Caption:='libeay32.dll not found';
    stlibeay.Font.Color:=clRed;
  end
  else
  begin
    stlibeay.Caption:='libeay32.dll found: '+s;
    stlibeay.Font.Color:=clGreen;
  end;
  {$ELSE}
  LibHandle := SafeLoadLibrary('libssl.so');
  if LibHandle <> NilHandle then
  begin
    stssleay.Caption:='libssl,so found.';
    stssleay.Font.Color:=clGreen;
    UnloadLibrary(LibHandle);
  end
  else
  begin
      stssleay.Caption:='libssl.so not found. Is libssl-dev installed?';
      stssleay.Font.Color:=clRed;
  end;
  stlibeay.Caption:='';
  {$ENDIF}

end;

procedure TFormPrefs.BitBtnOKClick(Sender: TObject);
begin
  FontSize := SpinEditFontSize.Value;
  FormPrefs.Hide;
end;

procedure TFormPrefs.BnPatchClick(Sender: TObject);
var p : TStringList;
    r : Integer;
    s : shortstring;
    h : THandle;
begin
  if cbPatch.Text<>'' then begin
    p := TStringList.create;
    p.LoadFromFile(cbPatch.Text);
    r:=MessageDlg('TiCodEd Patching','Make sure your emulator is ready to receive commands to patch.',mtConfirmation,[mbYes,mbCancel],0);
    if r=mrYes then begin
        h := GetWindowHandle(FormPrefs.RGIntTarget.ItemIndex, FormPrefs.EdIntStr.Text);
        for s in p do begin
          SendString(h,s);
          if IndexText(s,['OLD ','SAVE '])>0 then sleep(3000) else sleep(500)
        end;
        ShowMessage('Patching completed.');
    end;
    p.free;
  end;
end;

procedure TFormPrefs.DirEditIncExit(Sender: TObject);
var ini : TIniFile;
    s : String;
begin
  if Pos('xas99.py',LowerCase(EdAssembler.Text))>0 then begin
     if not FileExists(trim(DirEditInc.text)+'/RUNTIME1.TXT') then
        ShowMessage('Error: Include-File not found: '+trim(DirEditInc.text)+'\RUNTIME1.TXT');
  end else begin
    s := ChangeFileExt(EdAssembler.Text,'.ini');
    if FileExists(s) then begin
      ini := TIniFile.Create(s);
      s := ini.ReadString('CONFIG','INCLUDE','');
      if trim(UpperCase(OldIncPath))<>trim(UpperCase(DirEditInc.text)) then
        if FileExists(trim(DirEditInc.text)+'/RUNTIME1.TXT') then begin
          s:=StringReplace(s,OldIncPath,'',[rfIgnoreCase]);
          s:=StringReplace(s,';;',';',[rfIgnoreCase]);
          if RightStr(s,1)<>';' then s := s + ';';
          s := s + trim(DirEditInc.text);
          ini.WriteString('CONFIG','INCLUDE',s);
          ShowMessage('Success: Include path updated.');
        end else ShowMessage('Error: Include-File not found: '+trim(DirEditInc.text)+'\RUNTIME1.TXT');
      ini.Destroy;
    end else ShowMessage('Error: Assembler INI-File not found: '+s);
  end;
 end;

procedure TFormPrefs.EdAssemblerExit(Sender: TObject);
begin
  if (EdAssembler.Text<>'') and not FileExists(EdAssembler.Text) then
    ShowMessage('Warning: '+EdAssembler.Text+' not found!');
end;

procedure TFormPrefs.EdIntDelExit(Sender: TObject);
var i,rc : Integer;
begin
  val(EdIntDel.Text,i,rc);
  if rc<>0 then EdIntDel.Text:= '20';
end;

procedure TFormPrefs.FormShow(Sender: TObject);
var s : String;
begin
  OldIncPath:=DirEditInc.Text;
//  {$IFDEF WINDOWS}
  GroupBoxIntegration.Enabled:=true;
  if EdAssembler.Text='' then begin
    s:=ExtractFileDir(Paramstr(0))+'\asm\xa99.exe';
    if FileExists(s) then EdAssembler.Text:=s;
  end;
//  {$ELSE}
  //GroupBoxIntegration.Enabled:=true;
  //GroupBoxIntegration.hint:='Full integration only available in Windows';
  //GroupBoxIntegration.ShowHint:=true;
  //RGIntTarget.Enabled:=false;
  //EdIntStr.Enabled:=false;
  //EdIntDel.Enabled:=false;
  //CbDevice.Enabled:=false;
  //GroupBoxAssembler.Enabled:=false;
  //{$ENDIF}
  cbPatch.Clear;
  chdir(ProgramDirectory);
  FindAllFiles(cbPatch.Items,'.'+PathDelim+'LIB','*.patch',false,faDirectory) ;
end;

procedure TFormPrefs.RGIntTargetClick(Sender: TObject);
begin
  if RgIntTarget.ItemIndex<2 then EdIntStr.Enabled:=false
                             else EdIntStr.Enabled:=true;
end;

procedure TFormPrefs.BitBtn1Click(Sender: TObject);
begin
  DirEditIncExit(NIL);
  FormPrefs.Hide;
end;

end.

