unit umainfrm;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, RxVersInfo, Forms, Controls, Graphics, Dialogs,
  StdCtrls, Menus, ExtCtrls, EditBtn, FileCtrl, INIFiles, Process;

type

  { TMainForm }

  TMainForm = class(TForm)
    BtnSetEdit: TButton;
    BtnAddDb: TButton;
    BtnRmvDb: TButton;
    BtnStartEdit: TButton;
    BtnClrDb: TButton;
    BtnClrBS: TButton;
    BtnAddScen: TButton;
    BtnClrScen: TButton;
    BtnRmvScen: TButton;
    TxBxEdit: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    LbxDb: TListBox;
    LBxBS: TListBox;
    LBxScen: TListBox;
    MainMenu1: TMainMenu;
    MenuClose: TMenuItem;
    OpenDialogSC: TOpenDialog;
    OpenDialogEd: TOpenDialog;
    OpenDialogDB: TOpenDialog;
    verInfo: TRxVersionInfo;
    procedure BtnSetEditClick(Sender: TObject);
    procedure BtnAddDbClick(Sender: TObject);
    procedure BtnRmvDbClick(Sender: TObject);
    procedure BtnStartEditClick(Sender: TObject);
    procedure BtnClrDbClick(Sender: TObject);
    procedure BtnClrBSClick(Sender: TObject);
    procedure BtnAddScenClick(Sender: TObject);
    procedure BtnClrScenClick(Sender: TObject);
    procedure BtnRmvScenClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure LbxDbClick(Sender: TObject);
    procedure LBxScenClick(Sender: TObject);
    procedure MenuCloseClick(Sender: TObject);
    procedure SaveToIni;
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  MainForm: TMainForm;
  Inifile:TINIfile;
  RunProcess:TProcess;
  list1:TStringList;
  EditName: String;
  DBName: string;
  i: integer;
  n: integer;
  a: string;

implementation

{$R *.lfm}

{ TMainForm }




procedure TMainForm.MenuCloseClick(Sender: TObject);
begin
  SaveToIni;
//save data to ini.file
  Application.Terminate;
end;

procedure TMainForm.SaveToIni;
//intenal procedure to save data to ini.file
begin
  Inifile:=Tinifile.Create(Application.Location+'\SELaunch.ini');
     Inifile.EraseSection('General');
     //don't want to have to check for fewer
     //or rearranged databse files
     Inifile.WriteString('General','EditorPath',EditName);
     n:=LbxDb.Count;
     inifile.WriteInteger('General','DataBaseCount',n);
     i:=0;
     while i<=n-1 do
           begin
                a:=IntToStr(i);
                Inifile.WriteString('General','DataBasePath'+a,LbxDb.Items.Strings[i]);
                i:=i+1;
           end;
     //...........
     n:=LBxScen.Count;
     inifile.WriteInteger('General','ScenFileCount',n);
     i:=0;
     while i<=n-1 do
           begin
                a:=IntToStr(i);
                Inifile.WriteString('General','ScenFilePath'+a,LBxScen.Items.Strings[i]);
                i:=i+1;
           end;

     //............
     Inifile.Free;

end;

procedure TMainForm.FormActivate(Sender: TObject);
begin
  MainForm.Caption := MainForm.Caption + ' v.' + verInfo.FileVersion;
  Inifile:=TInifile.Create(Application.Location+'\SELaunch.ini');
     //first read last used editor file from ini file
     EditName:=IniFile.ReadString('General','EditorPath','unset');
     TxBxEdit.Text:=EditName;
     //now read databases from ini file
     i:=0;
     n:=Inifile.ReadInteger('General','DataBaseCount',0);
     //default (none stored) = zero to holt process
     while i<=n-1 do
     begin
          a:=IntToStr(i);
          DBName:=Inifile.ReadString('General','DataBasePath'+a,'');
          LbxDb.Items.Append(DBName);
          i:=i+1;
     end;
//     ...........
//now read stored scenario files from ini file
    i:=0;
    n:=Inifile.ReadInteger('General','ScenFileCount',0);
    //default (none stored) = zero to holt process
    while i<=n-1 do
    begin
         a:=IntToStr(i);
         LBxScen.Items.Append(Inifile.ReadString('General','ScenFilePath'+a,''));
         i:=i+1;
    end;

//...........
Inifile.Free;
end;

procedure TMainForm.LbxDbClick(Sender: TObject);
begin
     if not fileExists(LbxDb.GetSelectedText) then
     messagedlg('selected database no longer valid, Press RMV',mtWarning,[mbOK],0);

end;


procedure TMainForm.LBxScenClick(Sender: TObject);
begin
    if not fileExists(LBxScen.GetSelectedText) then
          messagedlg('selected scenario no longer valid, Press RMV',mtWarning,[mbOK],0);
end;



procedure TMainForm.BtnSetEditClick(Sender: TObject);
begin
  //sets the file name and path for the scenario editor
  OpenDialogEd.InitialDir:=Application.Location;
  with OpenDialogEd do
          begin
               if Execute then
               begin
                 EditName:=OpenDialogEd.FileName;
                 TxBxEdit.Text:=EditName;
               end;
          end;
end;

procedure TMainForm.BtnAddDbClick(Sender: TObject);
begin
  //adds a database file to list window
     with OpenDialogDB do
          begin
               If Execute then
               begin
                 DBName:=OpenDialogDB.FileName;
                 LbxDb.Items.Append(DBName);
               end;
          end;
end;

procedure TMainForm.BtnRmvDbClick(Sender: TObject);
begin
  //removes a selected databasefrom the list window
     if LbxDb.SelCount>0 then
     //a database is selected
     begin
       n:=LbxDb.Count;
       i:=n-1;  //move backwards thru list to prevent out of range error
       while i>=0 do
             begin
                  if LbxDb.Selected[i] then
                  LbxDb.Items.Delete(i);
                  i:=i-1;
             end;
     end
     else messagedlg('select the database to be removed from the list',mterror,[mbOK],0);
end;

procedure TMainForm.BtnStartEditClick(Sender: TObject);
begin
//save data to ini.file
  SaveToIni;
//check editor exists and [if so] execute
     if FileExists(EditName) then
     begin
        RunProcess:=TProcess.Create(Self);
        RunProcess.CurrentDirectory:=ExtractFilePath(EditName);
        RunProcess.Executable:=EditName;
        //check if any databse selected
        if LbxDb.SelCount>0 then
       RunProcess.Parameters.Add('-d '+'"'+LbxDb.GetSelectedText+'"') ;
        //else no DB selected = do nothing
        //........................
         //check if any scenario selected
        if LBxScen.SelCount>0 then
        RunProcess.Parameters.Add('-c '+ '"' + LBxScen.GetSelectedText + '"');
        //.else no scenario selected = do nothing
        //check if any battleset selected
        if LBxBS.SelCount>0 then
        begin
          list1:=TStringList.Create;
          list1.Delimiter:='-';
          list1.DelimitedText:=LBxBS.GetSelectedText;
          // test for .res file for custom battlesets
             case list1[0] of
                '27':  begin  if fileexists(extractfilepath(EditName)+'\hdsm.res') then
                       RunProcess.Parameters.Add('-b '+ list1[0])
                       else   messagedlg('selected custom battleset not present & wont load',mtWarning,[mbOK],0);
                       end;
                '35':  begin  if fileExists(extractfilepath(EditName)+'\hdsu.res') then
                       RunProcess.Parameters.Add('-b '+ list1[0])
                       else   messagedlg('selected custom battleset not present & wont load',mtWarning,[mbOK],0);
                       end;
             else      RunProcess.Parameters.Add('-b '+ list1[0]);
        end;
          list1.Free;
        end;

        RunProcess.Execute;
        RunProcess.Free;
        Application.Terminate;
     end
     else messagedlg('selected editor no longer valid, reset',mtError,[mbOK],0);

end;

procedure TMainForm.BtnClrDbClick(Sender: TObject);
begin
  //check if any database selected
     if LbxDb.SelCount>0 then
     LbxDb.ClearSelection;
end;

procedure TMainForm.BtnClrBSClick(Sender: TObject);
begin
  //check if battleset selected
     if LBxBS.SelCount>0 then
     LBxBS.ClearSelection;
end;

procedure TMainForm.BtnAddScenClick(Sender: TObject);
begin
  with OpenDialogSC do
       begin
            if Execute then
               LBxScen.Items.Append(OpenDialogSC.FileName);
            //else no fileselected = do nothing
       end;
end;

procedure TMainForm.BtnClrScenClick(Sender: TObject);
begin
  //check if any scenario file selected
     if LBxScen.SelCount>0 then
     LBxScen.ClearSelection;
end;

procedure TMainForm.BtnRmvScenClick(Sender: TObject);
begin
  //removes a selected scenario file from the list window
     if LBxScen.SelCount>0 then
     //a scenario is selected
     begin
       n:=LBxScen.Count;
       i:=n-1;  //move backwards thru list to prevent out of range error
       while i>=0 do
             begin
                  if LBxScen.Selected[i] then
                  LBxScen.Items.Delete(i);
                  i:=i-1;
             end;
     end
     else messagedlg('select the scenario file to be removed from the list',mtError,[mbOK],0);
end;



end.

