//
// Copyright (C) 2024 - 2025 Sven Dmitrienko <svens@svens-tools.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//
////////////////////////////////////////////////////////////////////////////////

unit sdFileUtils;

interface

type
  TSDFileUtils = class
  public
    class var RecentFiles: array[1..6] of string;
    class procedure MakeBackup(aFileNameWithFullPath: String; aMaxBackup: Integer);
    class function ReadLastFilenameFromRegistry(const aKey: String): String;
    class procedure ReadRecentFilesFromRegistry(const aKey: String);
    class procedure RemoveRecentFile(aFileNameWithFullPath: String);
    class procedure SaveRecentFile(aFileNameWithFullPath: String);
    class procedure WriteLastFilenameIntoRegistry(const aFilename: String; const aKey: String);
    class procedure WriteRecentFilesIntoRegistry(const aKey: String);
  end;

implementation

uses
  Windows, SysUtils, System.Win.Registry, sdDateUtils, UnitMainForm;

class procedure TSDFileUtils.MakeBackup(aFileNameWithFullPath: String; aMaxBackup: Integer);
var
  a_BackupDate: TDateTime;
  a_FileName: String;
  a_OnlyFileName: String;
  a_OnlyFilePath: String;
begin
  a_BackupDate := Now - aMaxBackup;
  a_OnlyFilePath := ExtractFilePath(aFileNameWithFullPath);
  a_OnlyFileName := ExtractFileName(aFileNameWithFullPath);
  a_FileName := a_OnlyFilePath  + TSDDateUtils.ToYearMonthDayStr(a_BackupDate) + '--' + a_OnlyFileName;
  if (FileExists(a_FileName)) then
     DeleteFile(a_FileName);
  a_BackupDate := Now;
  if (FileExists(aFileNameWithFullPath)) then
  begin
    a_FileName := a_OnlyFilePath  + TSDDateUtils.ToYearMonthDayStr(a_BackupDate) + '--' + a_OnlyFileName;
    DeleteFile(a_FileName);
    RenameFile(aFileNameWithFullPath, a_FileName);
    Sleep(1000);
  end;
end;

class function TSDFileUtils.ReadLastFilenameFromRegistry(const aKey: String): String;
begin
  with TRegistry.Create do
  begin
    try
      Rootkey := HKEY_CURRENT_USER;
      if (OpenKey(aKey, True)) then
      begin
        Result := ReadString('LastFilename');
      end;
    finally
      Free;
    end;
  end;
end;

class procedure TSDFileUtils.ReadRecentFilesFromRegistry(const aKey: String);
begin
  with TRegistry.Create do
  begin
    try
      Rootkey := HKEY_CURRENT_USER;
      if (OpenKey(aKey, True)) then
      begin
          RecentFiles[1] := ReadString('RecentFile1');
          RecentFiles[2] := ReadString('RecentFile2');
          RecentFiles[3] := ReadString('RecentFile3');
          RecentFiles[4] := ReadString('RecentFile4');
          RecentFiles[5] := ReadString('RecentFile5');
          RecentFiles[6] := ReadString('RecentFile6');
      end;
    finally
      Free;
   end;
 end;
end;

class procedure TSDFileUtils.RemoveRecentFile(aFileNameWithFullPath: String);
var
  i, j: Integer;
  a_Found: Boolean;
begin
  a_Found := False;
  for i := 1 to 6 do
    if (RecentFiles[i] = aFileNameWithFullPath) then
    begin
      a_Found := True;
      Break;
    end;
  if (a_Found) then
  begin
    for j := i to 5 do
      RecentFiles[j] := RecentFiles[j+1];
    RecentFiles[6] := '';
  end;
end;

class procedure TSDFileUtils.SaveRecentFile(aFileNameWithFullPath: String);
var
  i, j: Integer;
  a_Found: Boolean;
begin
  a_Found := False;
  for i := 1 to 6 do
    if (RecentFiles[i] = aFileNameWithFullPath) then
    begin
      a_Found := True;
      Break;
    end;
  if (a_Found) then
  begin
    if (i > 1) then
    begin
      for j := i downto 2 do
        RecentFiles[j] := RecentFiles[j-1];
      RecentFiles[1] := aFileNameWithFullPath;
    end;
  end
  else
  begin
    for i := 6 downto 2 do
      RecentFiles[i] := RecentFiles[i-1];
    RecentFiles[1] := aFileNameWithFullPath;
  end;
end;

class procedure TSDFileUtils.WriteLastFilenameIntoRegistry(const aFilename: String; const aKey: String);
begin
  with TRegistry.Create do
  begin
    try
      Rootkey := HKEY_CURRENT_USER; //Hauptschlüssel
      if (OpenKey(aKey, True)) then //Unterschlüssel öffnen
      begin
        WriteString('LastFilename', aFilename);
      end;
    finally
      Free;
    end;
  end;
end;

class procedure TSDFileUtils.WriteRecentFilesIntoRegistry(const aKey: String);
begin
  with TRegistry.Create do
  begin
    try
      Rootkey := HKEY_CURRENT_USER; //Hauptschlüssel
      if (OpenKey(aKey, True)) then //Unterschlüssel öffnen
      begin
          WriteString('RecentFile1', RecentFiles[1]);
          WriteString('RecentFile2', RecentFiles[2]);
          WriteString('RecentFile3', RecentFiles[3]);
          WriteString('RecentFile4', RecentFiles[4]);
          WriteString('RecentFile5', RecentFiles[5]);
          WriteString('RecentFile6', RecentFiles[6]);
      end;
    finally
      Free;
    end;
  end;
end;

end.
