unit DebugUsers;

interface

uses
  Classes;

type
  TUserInfo = class
  private
    FUser: string;
    FPassword: string;
  public
    constructor Create(const AUser, APassword: string);
  end;

  TDebugUsers = class
  private
    FUsers: TList;
    procedure LoadPasswords(const AFileName: string);
  public
    constructor Create;
    destructor Destroy; override;

    function GetPassword(const AUser: string): string;
  end;

  function GetDebugUsers: TDebugUsers;

implementation

uses
  UtilMethods, SysUtils, Math;

var
  _DebugUsers: TDebugUsers;

function GetDebugUsers: TDebugUsers;
begin
  if not Assigned(_DebugUsers) then
    _DebugUsers := TDebugUsers.Create;
  Result := _DebugUsers;
end;

{ TDebugUsers }

constructor TDebugUsers.Create;
var
  sFileName: string;
begin
  FUsers := TList.Create;
  sFileName := GetAppFilePath + 'UserPassword.txt';
  LoadPasswords(sFileName);
end;

destructor TDebugUsers.Destroy;
begin
  FUsers.Free;
  inherited;
end;

function TDebugUsers.GetPassword(const AUser: string): string;
var
  i: Integer;
  vUser: TUserInfo;
begin
  Result := '';
  for i := 0 to FUsers.Count - 1 do
  begin
    vUser := TUserInfo(FUsers.Items[i]);
    if (vUser.FUser = AUser) then
    begin
      Result := vUser.FPassword;
      Break;
    end;
  end;
end;

procedure TDebugUsers.LoadPasswords(const AFileName: string);
var
  sgs: TStrings;
  i, iPos: Integer;
  sValue, sUser, sPassword: string;
  vUser: TUserInfo;
begin
  if FileExists(AFileName) then
  begin
    sgs := TStringList.Create;
    try
      sgs.LoadFromFile(AFileName);
      for i := 0 to sgs.Count - 1 do
      begin
        sValue := sgs.Strings[i];
        iPos := Pos(';', sValue);
        if iPos > 0 then
        begin
          sUser := Copy(sValue, 1, iPos - 1);
          sPassword := Copy(sValue, iPos + 1, Length(sValue)-iPos);
          vUser := TUserInfo.Create(sUser, sPassword);
          FUsers.Add(vUser);
        end;
      end;
    finally
      sgs.Free;
    end;
  end;
end;

{ TPassword }

constructor TUserInfo.Create(const AUser, APassword: string);
begin
  FUser := AUser;
  FPassword := APassword;
end;

initialization

finalization
  _DebugUsers.Free;
end.
