unit untConfig;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, Registry, Forms, IniFiles;

type

  { TConfigReader }

  TConfigReader = class
  private
    class var FScale: Double;
  public
    class function GetMCToken(): string;
    class procedure SaveMCToken(AToken: string);
    class function GetMDPro3InstallPath(): string;
    class procedure SetScale(AScale: Double);
    class function GetScale(): Double;
    class function GetSearchHistory(): TStringArray;
    class procedure SetSearchHistory(Arr: TStringArray);
  end;

implementation

{ TConfigReader }

class function TConfigReader.GetMCToken(): string;
var
  ACfgPath: String;
  i: Integer;
  AToken: String = '';
begin
  ACfgPath:= GetMDPro3InstallPath() + DirectorySeparator + 'Data' + DirectorySeparator + 'config.conf';
  with TStringList.Create do begin
    LoadFromFile(ACfgPath);
    // 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 procedure TConfigReader.SaveMCToken(AToken: string);
var
  ACfgPath: String;
  AFound: Boolean = False;
  i: Integer;
begin
  ACfgPath:= GetMDPro3InstallPath() + DirectorySeparator + 'Data' + DirectorySeparator + 'config.conf';
  with TStringList.Create do begin
    LoadFromFile(ACfgPath);
    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;
    SaveToFile(ACfgPath);
    Free;
  end;

end;

class function TConfigReader.GetMDPro3InstallPath(): string;
var
  AOpened: Boolean;
  APath: string = '';
begin
  // GetMDPro3InstallPath
  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;
  Exit(APath);
end;

class procedure TConfigReader.SetScale(AScale: Double);
begin
  FScale:= AScale;
end;

class function TConfigReader.GetScale(): Double;
begin
  Exit(FScale);
end;

class function TConfigReader.GetSearchHistory(): TStringArray;
var
  ACfgPath: string;
  i: Integer;
  ini: TIniFile;
begin
  SetLength(Result, 10);
  ACfgPath:= ChangeFileExt(Application.ExeName, '.conf');
  ini := TIniFile.Create(ACfgPath);
  for i := 0 to 9 do begin
    Result[i] := ini.ReadString('HISTORY', Format('Key%d', [i]), '');
  end;
  ini.Free;
end;

class procedure TConfigReader.SetSearchHistory(Arr: TStringArray);
var
  ACfgPath: string;
  i: Integer;
  ini: TIniFile;
begin
  ACfgPath:= ChangeFileExt(Application.ExeName, '.conf');
  ini := TIniFile.Create(ACfgPath);
  for i := 0 to 9 do begin
    ini.WriteString('HISTORY', Format('Key%d', [i]), Arr[i]);
  end;
  ini.Free;
end;

end.

