unit uScaleConfig;

interface

uses
  System.SysUtils, System.IniFiles, Winapi.Windows;

type
  TScaleConfig = record
    PortName: string;
    BaudRate: Integer;
    DataBits: Byte;
    Parity: Byte;     // 0: None, 1: Odd, 2: Even
    StopBits: Byte;   // 0: 1 stop bit, 1: 1.5, 2: 2 stop bits
    Simulated: Boolean;
    StableSamples: Integer;
    ToleranceGrams: Double;
    PollIntervalMs: Integer;
    DefaultEmpresaId: Integer;
    DefaultObradorId: Integer;
    ServerHost: string;
    ServerPort: Integer;
    ServerDatabase: string;
    ServerUser: string;
    ServerPass: string;

    class function DefaultConfig: TScaleConfig; static;
    class function LoadFromIni(const AFileName: string = ''): TScaleConfig; static;
    procedure SaveToIni(const AFileName: string = '');
  end;

function GetDefaultIniPath: string;

implementation

uses
  System.IOUtils;

function GetDefaultIniPath: string;
begin
  Result := TPath.Combine(ExtractFilePath(ParamStr(0)), 'fabricacion.ini');
end;

{ TScaleConfig }

class function TScaleConfig.DefaultConfig: TScaleConfig;
begin
  Result.PortName := 'COM1';
  Result.BaudRate := 9600;
  Result.DataBits := 8;
  Result.Parity := 0;      // NOPARITY
  Result.StopBits := 0;    // ONESTOPBIT
  Result.Simulated := True; // Por defecto arrancamos con simulador para permitir testeo sin cable
  Result.StableSamples := 3;
  Result.ToleranceGrams := 2.0;
  Result.PollIntervalMs := 150;
  Result.DefaultEmpresaId := 1;
  Result.DefaultObradorId := 1;
  Result.ServerHost := '127.0.0.1';
  Result.ServerPort := 3306;
  Result.ServerDatabase := 'pastelerias';
  Result.ServerUser := 'root';
  Result.ServerPass := '';
end;

class function TScaleConfig.LoadFromIni(const AFileName: string): TScaleConfig;
var
  LIni: TIniFile;
  LPath: string;
begin
  Result := DefaultConfig;
  LPath := AFileName;
  if LPath = '' then
    LPath := GetDefaultIniPath;

  if not FileExists(LPath) then
    Exit;

  LIni := TIniFile.Create(LPath);
  try
    Result.PortName := LIni.ReadString('BASCULA', 'Puerto', Result.PortName);
    Result.BaudRate := LIni.ReadInteger('BASCULA', 'BaudRate', Result.BaudRate);
    Result.DataBits := LIni.ReadInteger('BASCULA', 'DataBits', Result.DataBits);
    Result.Parity := LIni.ReadInteger('BASCULA', 'Paridad', Result.Parity);
    Result.StopBits := LIni.ReadInteger('BASCULA', 'StopBits', Result.StopBits);
    Result.Simulated := LIni.ReadBool('BASCULA', 'Simulado', Result.Simulated);
    Result.StableSamples := LIni.ReadInteger('BASCULA', 'MuestrasEstables', Result.StableSamples);
    Result.ToleranceGrams := LIni.ReadFloat('BASCULA', 'ToleranciaGramos', Result.ToleranceGrams);
    Result.PollIntervalMs := LIni.ReadInteger('BASCULA', 'IntervaloMs', Result.PollIntervalMs);

    Result.DefaultEmpresaId := LIni.ReadInteger('PLANTA', 'EmpresaId', Result.DefaultEmpresaId);
    Result.DefaultObradorId := LIni.ReadInteger('PLANTA', 'ObradorId', Result.DefaultObradorId);
    Result.ServerHost := LIni.ReadString('SERVIDOR', 'Host', Result.ServerHost);
    Result.ServerPort := LIni.ReadInteger('SERVIDOR', 'Puerto', Result.ServerPort);
    Result.ServerDatabase := LIni.ReadString('SERVIDOR', 'BaseDatos', Result.ServerDatabase);
    Result.ServerUser := LIni.ReadString('SERVIDOR', 'Usuario', Result.ServerUser);
    Result.ServerPass := LIni.ReadString('SERVIDOR', 'Password', Result.ServerPass);
  finally
    LIni.Free;
  end;
end;

procedure TScaleConfig.SaveToIni(const AFileName: string);
var
  LIni: TIniFile;
  LPath: string;
begin
  LPath := AFileName;
  if LPath = '' then
    LPath := GetDefaultIniPath;

  LIni := TIniFile.Create(LPath);
  try
    LIni.WriteString('BASCULA', 'Puerto', PortName);
    LIni.WriteInteger('BASCULA', 'BaudRate', BaudRate);
    LIni.WriteInteger('BASCULA', 'DataBits', DataBits);
    LIni.WriteInteger('BASCULA', 'Paridad', Parity);
    LIni.WriteInteger('BASCULA', 'StopBits', StopBits);
    LIni.WriteBool('BASCULA', 'Simulado', Simulated);
    LIni.WriteInteger('BASCULA', 'MuestrasEstables', StableSamples);
    LIni.WriteFloat('BASCULA', 'ToleranciaGramos', ToleranceGrams);
    LIni.WriteInteger('BASCULA', 'IntervaloMs', PollIntervalMs);

    LIni.WriteInteger('PLANTA', 'EmpresaId', DefaultEmpresaId);
    LIni.WriteInteger('PLANTA', 'ObradorId', DefaultObradorId);
    LIni.WriteString('SERVIDOR', 'Host', ServerHost);
    LIni.WriteInteger('SERVIDOR', 'Puerto', ServerPort);
    LIni.WriteString('SERVIDOR', 'BaseDatos', ServerDatabase);
    LIni.WriteString('SERVIDOR', 'Usuario', ServerUser);
    LIni.WriteString('SERVIDOR', 'Password', ServerPass);
  finally
    LIni.Free;
  end;
end;

end.
