unit dlg_PegarListaCodigos;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons,
  System.UITypes;

type
  TdlgPegarListaCodigos = class(TForm)
    pnlMain: TPanel;
    lblTitulo: TLabel;
    lblInstrucciones: TLabel;
    pnlMemoContainer: TPanel;
    mmCodigos: TMemo;
    lblLineCount: TLabel;
    pnlBotones: TPanel;
    btnProcesar: TBitBtn;
    btnCancelar: TBitBtn;

    procedure FormShow(Sender: TObject);
    procedure mmCodigosChange(Sender: TObject);
    procedure btnProcesarClick(Sender: TObject);
  private
    function GetProcessedLines: TArray<string>;
  public
    property ProcessedLines: TArray<string> read GetProcessedLines;
    class function Execute(AOwner: TComponent; out ACodes: TArray<string>): Boolean;
  end;

var
  dlgPegarListaCodigos: TdlgPegarListaCodigos;

implementation

uses
  uAppTheme;

{$R *.dfm}

procedure TdlgPegarListaCodigos.FormShow(Sender: TObject);
begin
  TAppTheme.ApplyToForm(Self);
  mmCodigos.Clear;
  mmCodigosChange(nil);
  mmCodigos.SetFocus;
end;

procedure TdlgPegarListaCodigos.mmCodigosChange(Sender: TObject);
var
  LCodes: TArray<string>;
  LCount: Integer;
begin
  LCodes := GetProcessedLines;
  LCount := Length(LCodes);
  if LCount = 1 then
    lblLineCount.Caption := '1 código detectado'
  else
    lblLineCount.Caption := Format('%d códigos detectados', [LCount]);

  btnProcesar.Enabled := LCount > 0;
end;

function TdlgPegarListaCodigos.GetProcessedLines: TArray<string>;
var
  LRawText, LToken: string;
  LList: TStringList;
  i: Integer;
  LDelims: TSysCharSet;
begin
  LRawText := mmCodigos.Text;
  LList := TStringList.Create;
  try
    LDelims := [#13, #10, ',', ';'];
    LToken := '';
    for i := 1 to Length(LRawText) do
    begin
      if CharInSet(LRawText[i], LDelims) then
      begin
        if Trim(LToken) <> '' then
          LList.Add(Trim(LToken));
        LToken := '';
      end
      else
        LToken := LToken + LRawText[i];
    end;
    if Trim(LToken) <> '' then
      LList.Add(Trim(LToken));

    SetLength(Result, LList.Count);
    for i := 0 to LList.Count - 1 do
      Result[i] := LList[i];
  finally
    LList.Free;
  end;
end;

procedure TdlgPegarListaCodigos.btnProcesarClick(Sender: TObject);
begin
  if Length(GetProcessedLines) = 0 then
  begin
    MessageDlg('Por favor introduzca o pegue al menos un código de barras.', mtWarning, [mbOK], 0);
    ModalResult := mrNone;
    mmCodigos.SetFocus;
    Exit;
  end;
  ModalResult := mrOk;
end;

class function TdlgPegarListaCodigos.Execute(AOwner: TComponent; out ACodes: TArray<string>): Boolean;
var
  LDlg: TdlgPegarListaCodigos;
begin
  Result := False;
  SetLength(ACodes, 0);
  LDlg := TdlgPegarListaCodigos.Create(AOwner);
  try
    if LDlg.ShowModal = mrOk then
    begin
      ACodes := LDlg.ProcessedLines;
      Result := Length(ACodes) > 0;
    end;
  finally
    LDlg.Free;
  end;
end;

end.
