unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Menus, ExtCtrls, EditBtn, FileCtrl, XMLPropStorage, INIFiles, Process;

type

  { TMainForm }

  TMainForm = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    ListBox1: TListBox;
    ListBox2: TListBox;
    ListBox3: TListBox;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    OpenDialogSC: TOpenDialog;
    OpenDialogEd: TOpenDialog;
    OpenDialogDB: TOpenDialog;
    XMLPropStorage1: TXMLPropStorage;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
  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.MenuItem1Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TMainForm.FormActivate(Sender: TObject);
begin
  Inifile:=TInifile.Create(Application.Location+'\SELaunch.ini');
     //first read last used editor file from ini file
     EditName:=IniFile.ReadString('General','EditorPath','unset');
     Edit1.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,'');
          ListBox1.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);
         ListBox3.Items.Append(Inifile.ReadString('General','ScenFilePath'+a,''));
         i:=i+1;
    end;

//...........
Inifile.Free;
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
  //sets the file name and path for the scenario editor
     with OpenDialogEd do
          begin
               if Execute then
               begin
                 EditName:=OpenDialogEd.FileName;
                 edit1.Text:=EditName;
               end;
          end;
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
  //adds a database file to list window
     with OpenDialogDB do
          begin
               If Execute then
               begin
                 DBName:=OpenDialogDB.FileName;
                 ListBox1.Items.Append(DBName);
               end;
          end;
end;

procedure TMainForm.Button3Click(Sender: TObject);
begin
  //removes a selected databasefrom the list window
     if listbox1.SelCount>0 then
     //a database is selected
     begin
       n:=listbox1.Count;
       i:=n-1;  //move backwards thru list to prevent out of range error
       while i>=0 do
             begin
                  if listbox1.Selected[i] then
                  listbox1.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.Button4Click(Sender: TObject);
begin
  //save info to ini file, start SE and close launcher
    //......logic needs rework to stop procedure in event invalid scenario or battleset
    //................not just give warning
     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:=listbox1.Count;
     inifile.WriteInteger('General','DataBaseCount',n);
     i:=0;
     while i<=n-1 do
           begin
                a:=IntToStr(i);
                Inifile.WriteString('General','DataBasePath'+a,listbox1.Items.Strings[i]);
                i:=i+1;
           end;
     //...........
     n:=listbox3.Count;
     inifile.WriteInteger('General','ScenFileCount',n);
     i:=0;
     while i<=n-1 do
           begin
                a:=IntToStr(i);
                Inifile.WriteString('General','ScenFilePath'+a,listbox3.Items.Strings[i]);
                i:=i+1;
           end;

     //............
     Inifile.Free;
     //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 ListBox1.SelCount>0 then
        begin
          if fileExists(listbox1.GetSelectedText) then
          RunProcess.Parameters.Add('-d '+'"'+listbox1.GetSelectedText+'"')
          else messagedlg('selected database no longer valid',mtWarning,[mbOK],0);
        end; //else no DB selected = do nothing
        //........................
         //check if any scenario selected
        if listbox3.SelCount>0 then
        begin
          if fileexists(ListBox3.GetSelectedText) then
          RunProcess.Parameters.Add('-c '+ '"' + ListBox3.GetSelectedText + '"')
          else   messagedlg('selected scenario not present & wont load',mtWarning,[mbOK],0);
        end;
        //.....................
        //check if any battleset selected
        if listbox2.SelCount>0 then
        begin
          list1:=TStringList.Create;
          list1.Delimiter:='-';
          list1.DelimitedText:=listbox2.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;       ddt 041122  leave launcher running
     end
     else messagedlg('selected editor no longer valid, reset',mtError,[mbOK],0);

end;

procedure TMainForm.Button5Click(Sender: TObject);
begin
  //check if any database selected
     if ListBox1.SelCount>0 then
     Listbox1.ClearSelection;
end;

procedure TMainForm.Button6Click(Sender: TObject);
begin
  //check if battleset selected
     if Listbox2.SelCount>0 then
     Listbox2.ClearSelection;
end;

procedure TMainForm.Button7Click(Sender: TObject);
begin
     If FileExists(EditName) then
     OpenDialogSC.InitialDir:=ExtractFilePath(EditName);
  with OpenDialogSC do
       begin
            if Execute then
//               ListBox3.Items.Append(OpenDialogSC.FileName);  240614 try to insert and select
               ListBox3.Items.Insert(0, OpenDialogSC.FileName);
            //else no fileselected = do nothing
       end;
end;

procedure TMainForm.Button8Click(Sender: TObject);
begin
  //check if any scenario file selected
     if ListBox3.SelCount>0 then
     Listbox3.ClearSelection;
end;

procedure TMainForm.Button9Click(Sender: TObject);
begin
  //removes a selected scenario file from the list window
     if listbox3.SelCount>0 then
     //a scenario is selected
     begin
       n:=listbox3.Count;
       i:=n-1;  //move backwards thru list to prevent out of range error
       while i>=0 do
             begin
                  if listbox3.Selected[i] then
                  listbox3.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.

