{
  Copyright (C)
  2026 - Uladzimir Karpenka, EW8BAK

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
}

unit WisdomBuilder;

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

interface

uses
  Classes, SysUtils, Forms, Controls, ExtCtrls, StdCtrls,
  WDSP, AppTheme, FlatProgressBar;

type
  TWisdomBuildThread = class(TThread)
  private
    FDirectory: string;
    FError:     string;
  protected
    procedure Execute; override;
  public
    constructor Create(const ADirectory: string);
    property ErrorText: string read FError;
  end;

  TWisdomProgressDialog = class(TForm)
  private
    FInfoLabel: TLabel;
    FProgress:  TFlatProgressBar;
    FTimer:     TTimer;
    FThread:    TWisdomBuildThread;
    FPhase:     Integer;
    procedure TimerTick(Sender: TObject);
  public
    constructor Create(AOwner: TComponent; AThread: TWisdomBuildThread;
      const ATheme: TAppTheme); reintroduce;
  end;

implementation

constructor TWisdomBuildThread.Create(const ADirectory: string);
begin
  inherited Create(False);
  FreeOnTerminate := False;
  FDirectory      := ADirectory;
  FError          := '';
end;

procedure TWisdomBuildThread.Execute;
var
  DirA: AnsiString;
begin
  try
    if not Assigned(@WDSPwisdom) then Exit;
    DirA := AnsiString(FDirectory);
    WDSPwisdom(PAnsiChar(DirA));
  except
    on E: Exception do
      FError := E.ClassName + ': ' + E.Message;
  end;
end;

constructor TWisdomProgressDialog.Create(AOwner: TComponent;
  AThread: TWisdomBuildThread; const ATheme: TAppTheme);
begin
  inherited CreateNew(AOwner);
  FThread := AThread;
  FPhase  := 0;

  Caption     := 'WDSP Wisdom';
  Width       := 520;
  Height      := 140;
  Position    := poScreenCenter;
  BorderStyle := bsDialog;
  BorderIcons := [];
  Color       := ATheme.BG;
  Font.Name   := 'Courier New';
  Font.Size   := 9;
  Font.Color  := ATheme.Text;

  FInfoLabel := TLabel.Create(Self);
  FInfoLabel.Parent := Self;
  FInfoLabel.SetBounds(16, 16, 480, 40);
  FInfoLabel.AutoSize := False;
  FInfoLabel.WordWrap := True;
  FInfoLabel.Font.Color := ATheme.Text;
  FInfoLabel.Caption :=
    'Creating FFTW wisdom for WDSP. This is done once and may take a while on the first run.';

  FProgress := TFlatProgressBar.Create(Self);
  FProgress.Parent := Self;
  FProgress.SetBounds(16, 72, 480, 22);
  FProgress.Min      := 0;
  FProgress.Max      := 100;
  FProgress.Position := 0;
  FProgress.SetAppTheme(ATheme);

  FTimer := TTimer.Create(Self);
  FTimer.Interval := 250;
  FTimer.OnTimer  := TimerTick;
  FTimer.Enabled  := True;
end;

procedure TWisdomProgressDialog.TimerTick(Sender: TObject);
var
  P: PAnsiChar;
  S: string;
begin
  FPhase := (FPhase + 7) mod 101;
  FProgress.Position := FPhase;

  if Assigned(@wisdom_get_status) then
  begin
    P := wisdom_get_status;
    if P <> nil then
    begin
      S := Trim(string(AnsiString(P)));
      if S <> '' then
        FInfoLabel.Caption := S;
    end;
  end;

  if Assigned(FThread) and FThread.Finished then
  begin
    FTimer.Enabled := False;
    ModalResult := mrOk;
  end;
end;

end.
