unit uNotUsed;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ImgList, GR32_Image, StdCtrls, ExtCtrls, Buttons, IniFiles;

type
  TFormNotUsed = class(TForm)
    FilesHolder: TListView;
    PanelPreviewImage: TPanel;
    ImagePreview: TImage32;
    LabelFeatureDescription: TLabel;
    ButtonDelete: TButton;
    ButtonClose: TButton;
    ButtonSelectAll: TButton;
    SpeedButton2: TButton;
    SystemSelector: TComboBoxEx;
    ImageTypeSelector: TComboBoxEx;
    ButtonClear: TButton;
    ButtonScan: TButton;
    TopImage: TImage32;
    procedure FilesHolderSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure ButtonSelectAllClick(Sender: TObject);
    procedure ButtonCloseClick(Sender: TObject);
    procedure ButtonDeleteClick(Sender: TObject);
    procedure SystemSelectorSelect(Sender: TObject);
    procedure ImageTypeSelectorSelect(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ButtonClearClick(Sender: TObject);
    procedure ButtonScanClick(Sender: TObject);
  private
    { Private declarations }
    procedure ScanFiles;
  public
    { Public declarations }
  end;

var
  FormNotUsed: TFormNotUsed;

implementation

uses uMain, uCommon, uStatus;

{$R *.dfm}

procedure TFormNotUsed.ScanFiles;
var
  GamesListDAT, FilesList: THashedStringList;
  Loop, FileIndex: Integer;
  Folder, tempString: String;

  procedure CheckGameFiles(GameName, ImageExt: String);

    function ScanImages(FormatIndex, StartIndex: ShortInt): Boolean;
    var
      Loop2: ShortInt;
    begin
      for Loop2:=StartIndex to 10 do
      begin
        tempString:= IncludeTrailingPathDelimiter(Folder)+
                     Format('%s%s', [FormMain.GetImageName(GameName, Loop2, FormatIndex), ImageExt]);
        FileIndex:= FilesList.IndexOf(tempString);
        if FileIndex <> -1 then
           FilesList.Delete(FileIndex);
      end;
    end;

  begin
    ScanImages(0, 1);
    ScanImages(1, 1);
    ScanImages(2, 2);
  end;

begin
  Application.ProcessMessages;
  Screen.Cursor:= crHourGlass;
  FormStatus.Show;
  FormStatus.LabelStatusType.Caption:= Format('%s [%s]',
        ['Scan Not Used Images',
         ImageTypeSelector.ItemsEx[ImageTypeSelector.ItemIndex].Caption]);
  FormStatus.LabelMessage.Caption:= 'Creating files list.';
  FormStatus.ProgressBar.Progress:= 0;
  FormStatus.Refresh;
  FilesList:= THashedStringList.Create;
  if FormMain.imgFolderZipFile[SystemSelector.Tag, ImageTypeSelector.Tag] <> '' then
     begin
        Folder:= FormMain.GetZipFolderFull(ImageTypeSelector.Tag, SystemSelector.Tag);
        GetFilesList(Folder, '.png', '*.*', FilesList, False, False, True);
        if not (ImageTypeSelector.Tag in [0, 1]) then
           begin
             GetFilesList(Folder, '.jpg', '*.*', FilesList, False, False, True);
             GetFilesList(Folder, '.gif', '*.*', FilesList, False, False, True);
           end;
     end;
  //Application.ProcessMessages;
  FormStatus.LabelMessage.Caption:= 'Parsing files.';
  FormStatus.Refresh;
  if FilesList.Count > 0 then
     begin
       Screen.Cursor:= crHourGlass;
       GamesListDAT:= THashedStringList.Create;
       GetGamesList(GamesListDAT, SystemSelector.Tag);
       GamesListDAT.BeginUpdate;
       for Loop:=GamesListDAT.Count-1 downto 0 do
       begin
         FormMain.GetROMFields(GamesListDAT, Loop);
         GamesListDAT[Loop]:= FormMain.mName;
       end;
       GamesListDAT.Sort;
       GamesListDAT.EndUpdate;

       FormStatus.ProgressBar.MaxValue:= GamesListDAT.Count-1;
       FormStatus.LabelMessage.Caption:= 'Scanning files.';
       FormStatus.Refresh;
       FilesList.BeginUpdate;
       for Loop:=0 to GamesListDAT.Count-1 do
       begin
         CheckGameFiles(GamesListDAT[Loop], '.png');
         if not (ImageTypeSelector.Tag in [0, 1]) then
            begin
              CheckGameFiles(GamesListDAT[Loop], '.jpg');
              CheckGameFiles(GamesListDAT[Loop], '.gif');
            end;
         FormStatus.ProgressBar.Progress:= Loop;
       end;
       FilesList.EndUpdate;
       if FilesList.Count > 0 then
          begin
            FilesHolder.Items.BeginUpdate;
            for Loop:=0 to FilesList.Count-1 do
            begin
              with FilesHolder.Items.Add do
              begin
                Caption:= FilesList[Loop];
                //SubItems.Add('');
                Checked:= True;
              end;
            end;
            FilesHolder.Items.EndUpdate;
          end;
     end;
  FormStatus.Close;
  Screen.Cursor:= crDefault;
  FreeAndNil(FilesList);
  FreeAndNil(GamesListDAT);
  if FilesHolder.Items.Count = 0 then
     GenerateMessage('Information', Format('No files were found for "%s" - [%s].',
         [ImageTypeSelector.ItemsEx[ImageTypeSelector.ItemIndex].Caption,
          SystemSelector.ItemsEx[SystemSelector.ItemIndex].Caption]), 2);
  //ButtonScan.Visible:= True;
  //imgFolderZipFile[1, 1]; // game snapshots [sys, image_type]
end;

procedure TFormNotUsed.FilesHolderSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  if Selected then
     try
       ImagePreview.Bitmap.LoadFromFile(Item.Caption);
     except
       ImagePreview.Bitmap:= nil;
     end;
end;

procedure TFormNotUsed.ButtonSelectAllClick(Sender: TObject);
var
  Loop: Integer;
begin
  if FilesHolder.Items.Count > 0 then
     begin
       FilesHolder.Items.BeginUpdate;
       for Loop:=0 to FilesHolder.Items.Count-1 do
           FilesHolder.Items[Loop].Checked:= Boolean(TButton(Sender).Tag);
       FilesHolder.Items.EndUpdate;
     end;
end;

procedure TFormNotUsed.ButtonCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TFormNotUsed.ButtonDeleteClick(Sender: TObject);
var
  Loop, CheckedCount: Integer;
begin
  if FilesHolder.Items.Count = 0 then
     begin
       GenerateMessage('Error', 'There are no files in the list. Cannot continue.', 2);
       Exit;
     end;
  CheckedCount:= 0;
  for Loop:=0 to FilesHolder.Items.Count-1 do
  begin
    if FilesHolder.Items[Loop].Checked then
       Inc(CheckedCount);
  end;
  if CheckedCount = 0 then
     begin
       GenerateMessage('Error', 'There are no checked files in the list. Cannot continue.', 2);
       Exit;
     end;
  if GenerateMessage('Warning', 'You are about to delete the selected files. '+
     'Recycle bin is not supported.'+#13#10+'Are you sure ?', 1) = mrYes then
     begin
       FilesHolder.Items.BeginUpdate;
       for Loop:=FilesHolder.Items.Count-1 downto 0 do
       begin
         if FilesHolder.Items[Loop].Checked then
            begin
              if DeleteFile(FilesHolder.Items[Loop].Caption) then
                 FilesHolder.Items[Loop].Delete;
            end;
       end;
       FilesHolder.Items.EndUpdate;
       ImagePreview.Bitmap.Clear;
       ImagePreview.Bitmap:= nil;
       GenerateMessage('Information', 'Process finished. If a checked file is still listed, '+
                       'it means that it was not succesfully deleted.', 2);
     end;
end;

procedure TFormNotUsed.SystemSelectorSelect(Sender: TObject);
begin
  SystemSelector.Tag:= SystemSelector.ItemsEx[SystemSelector.ItemIndex].ImageIndex;
end;

procedure TFormNotUsed.ImageTypeSelectorSelect(Sender: TObject);
begin
  ImageTypeSelector.Tag:= ImageTypeSelector.ItemsEx[ImageTypeSelector.ItemIndex].ImageIndex;
end;

procedure TFormNotUsed.FormShow(Sender: TObject);
begin
  FormMain.LoadTopBar(TopImage, 'images.png');
  FormMain.FillEmuDescriptionsSelector(SystemSelector);
  FormMain.UpdateImageTypeDescriptionComboBoxEx(ImageTypeSelector);
  SetComboBoxEx(SystemSelector, 0);
  SetComboBoxEx(ImageTypeSelector, 1);
end;

procedure TFormNotUsed.ButtonClearClick(Sender: TObject);
begin
  FilesHolder.Items.Clear;
end;

procedure TFormNotUsed.ButtonScanClick(Sender: TObject);
begin
  Application.ProcessMessages;
  ScanFiles;
end;


end.
