unit untConfig;

interface

uses
  {$IFDEF  MSWINDOWS} Windows, Registry, {$ENDIF}
  Classes, SysUtils, IniFiles;

type
  TConfig = class
  private
    class var FMDConfPath: string;
    class var FConfPath: string;
  public
    class procedure Init();
    class function GetMDPro3InstallPath(): string;
    class function GetMCToken(): string;
    class procedure SaveMCToken(AToken: string);
    class function GetSearchHistory(): TArray<String>;
    class procedure SetSearchHistory(Arr: TArray<string>);
  end;

implementation

{ TConfig }

class function TConfig.GetMCToken: string;
var
  i: Integer;
  AToken: String;
begin
  AToken := '';
  if (not FileExists(FMDConfPath)) then Exit(AToken);

  with TStringList.Create do begin
    LoadFromFile(FMDConfPath, TEncoding.UTF8);
    // MyCardToken->
    for i := 0 to Count - 1 do begin
      if Strings[i].StartsWith('MyCardToken->') then begin
        // read token
        AToken := Strings[i].Replace('MyCardToken->', '', [rfIgnoreCase, rfReplaceAll]);
      end;
    end;
    Free;
  end;
  Exit(AToken);
end;

class function TConfig.GetMDPro3InstallPath: string;
var
  AOpened: Boolean;
  APath: string;
begin
  APath := '';
  // GetMDPro3InstallPath
  {$IFDEF  MSWINDOWS}
  with TRegistry.Create do begin
    RootKey:= HKEY_CURRENT_USER;
    AOpened := OpenKey('Software\MDPro3\MDPro3Install', False);
    if AOpened then begin
      APath := ReadString('InstallPath');
      CloseKey;
    end;
    Free;
  end;
  {$ENDIF}

  // other platform will not have an callable MDPro3 installation.

  Exit(APath);
end;

class function TConfig.GetSearchHistory: TArray<String>;
var
  i: Integer;
  ini: TIniFile;
begin
  SetLength(Result, 10);
  ini := TIniFile.Create(FConfPath);
  for i := 0 to 9 do begin
    Result[i] := ini.ReadString('HISTORY', Format('Key%d', [i]), '');
  end;
  ini.Free;
end;

class procedure TConfig.Init;
begin
  {$IFDEF  MSWINDOWS}
  FMDConfPath := GetMDPro3InstallPath() + PathDelim + 'Data' + PathDelim + 'config.conf';
  FConfPath := ChangeFileExt(ParamStr(0), '.conf');
  {$ENDIF}

  {$IFDEF  MACOS}
  FMDConfPath := GetHomePath() + PathDelim + '.config' + PathDelim + 'PuzzleEditor2' + PathDelim + 'config.conf';
  FConfPath := GetHomePath() + PathDelim + '.config' + PathDelim + 'PuzzleEditor2' + PathDelim + 'pe.conf';
  {$ENDIF}
end;


class procedure TConfig.SaveMCToken(AToken: string);
var
  AFound: Boolean;
  i: Integer;
begin
  AFound:= False;
  with TStringList.Create do begin
    if (FileExists(FMDConfPath)) then begin
      LoadFromFile(FMDConfPath, TEncoding.UTF8);
    end;

    for i := 0 to Count - 1 do begin
      if Strings[i].StartsWith('MyCardToken->') then begin
        AFound := True;
        // override token
        Strings[i] := 'MyCardToken->' + AToken;
      end;
    end;
    if (not AFound) then begin
      Add('MyCardToken->' + AToken);
    end;
    WriteBOM := False;
    SaveToFile(FMDConfPath, TEncoding.UTF8);
    Free;
  end;
end;

class procedure TConfig.SetSearchHistory(Arr: TArray<string>);
var
  i: Integer;
  ini: TIniFile;
begin
  ini := TIniFile.Create(FConfPath);
  for i := 0 to 9 do begin
    ini.WriteString('HISTORY', Format('Key%d', [i]), Arr[i]);
  end;
  ini.Free;

end;

initialization
  TConfig.Init;

end.
