{
  Author: Hert van Zwietering
  Version 2.31: 21-Oct-2024
  Version 2.3: 27-Aug-2024
  Version 2.2: 24-Jul-2024
  Version 2.1.1: 18-Jul-2024
  Version 2.1: 18-Jul-2022
  Version 2.0: 04-May-2022
  Changes:
    2.31 Duplicates are removed
    2.3 Made the result compatible so that filtering etc can be used in PSU
    2.2 Made the script suitable for large lists of file names
    2.1.1 Small performance improvement
    2.1 Now a message is displayed for files that are not found
    2.0 The file can now collect file with or without file path
}
const
  cGUID = '29312EB3605F4067A22612238E807010';

  procedure OpenNewTabForGroup(AGroup: TCatalogPropGroup);
  var
    AElem: TidElement;
  begin
    // delete results from a former run
    PublicCatalog.RemoveItemsFromTempList(AGroup.IncludeItems);

    // do search
    AElem := TidElement.Create(nil);
    try
      AElem.Prop['OpenNewTab'] := True;
      AElem.Data := AGroup;
      PublicBroadcast(nil, 'ShowGroupResults', AElem);
    finally
      AElem.Free;
    end;
  end;

  procedure FilesToItems(ASl: TTntStringList; AItems: TCatalogItems; AErrors: TTntStringList);
  var
    i: Integer;
    AItem: TCatalogItem;
    ASep, AFileName: WideString;
    AGUID: String;
    AFound: Boolean;
    APlatformSl, ANoPlatformSl: TTntStringList;
  begin
    ASep := iif(IsWindows, '\', '/');

    APlatformSl := TTntStringList.Create;
    ANoPlatformSl := TTntStringList.Create;
    try
      for i := 0 to ASl.Count - 1 do
      begin
        AFileName := ASl.Strings[i];

        // the file name seem to *not* match the platform?
        if WideTextPos(ASep, AFileName) > 0 then
          APlatformSl.Add(AFileName)
        else
          ANoPlatFormSl.Add(AFileName);
      end;

      if ANoPlatFormSl.Count > 0 then
      begin
        for i := 0 to ANoPlatformSl.Count - 1 do
        begin
          AItem := TCatalogItem.Create(nil);

          // the file name can be either a full file name (path+name) or a filename only
          AItem.FileName := ANoPlatformSl.Strings[i];
          if PublicCatalog.FindFileQuick(AItem.FileName, AItem, True) then
            AItem.Collection := AItems
          else
          begin
            AErrors.Add(AItem.FileName);
            AItem.Free;
          end;
        end;
      end;

      if APlatformSl.Count > 0 then
      begin
        AGUID := NewGUID;
        PublicCatalog.StoreFileListToTempFileList(AGUID, APlatformSl);
        PublicCatalog.EnumItemsFromTempFileList(AGUID, AItems);
        PublicCatalog.RemoveFromTempFileList(AGUID);
      end;
    finally
      ANoPlatformSl.Free;
      APlatformSl.Free;
    end;
  end;

  function IsAllowedOnBuild(ABuild: Integer): Boolean;
  var
    AAppBuild: Integer;
    ASl: TStringList;
  begin
    result := False;
    AAppBuild := 999999;
    ASl := TStringList.Create;
    try
      AnsiTokenize(xomProductVersion, '.', ASl);
      if ASl.Count > 3 then
        AAppBuild := StrToInt(ASl.Strings[3]);
    finally
      ASl.Free;
    end;

    result := (AAppBuild >= ABuild);
  end;

var
  AFileName: WideString;
  ASl, AErrors: TTntStringList;
  AGroup: TCatalogPropGroup;
begin
  if not IsAllowedOnBuild(6597) then
  begin
    Say('This script requires version 2024.2.2.6597 or higher.');
    exit;
  end;

  OpenDialog.Title := UTF8Encode('Select file with file names to search');
  OpenDialog.Filter := 'All Text Files|*.txt|All Files|*.*';
  OpenDialog.FileName := ReadFromRegistry('Scripts\SearchFromFile', 'FileName', '');
  if not OpenDialog.Execute then
    exit;

  AFileName := UTF8Decode(OpenDialog.FileName);

  if not WideFileExists(AFileName) then
  begin
    Say('File ' + AFileName + ' not found');
    exit;
  end;

  WriteToRegistry('Scripts\SearchFromFile', 'FileName', AFileName)

  AGroup := TCatalogPropGroup.Create(nil);
  try
    AGroup.GroupName := 'Collect Files from File';
    AGroup.IncludeItems.GUID := cGUID;  // fix the GUID

    AErrors := TTntStringList.Create;
    ASl := TTntStringList.Create;
    try
      ASl.Sorted := True;
      ASl.Duplicates := dupIgnore;
      ASl.LoadFromFile(AFileName);
      FilesToItems(ASl, AGroup.IncludeItems, AErrors);

      if (AErrors.Count > 0) then
        Say2('These files were not found', AErrors.Text);
    finally
      ASl.Free;
      AErrors.Free;
    end;

    if AGroup.IncludeItems.Count > 0 then
    begin
      //PublicCatalog.EnumGroupResultQuick(AGroup, AGroup.IncludeItems, -1);

      OpenNewTabForGroup(AGroup);
    end;
  finally
    AGroup.Free;
  end;
end;
