unit XData.Wizard.Service.Form;

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.ComCtrls,
  Vcl.Grids, Vcl.ValEdit, Vcl.CheckLst,

  XData.Wizard.Service.Settings;

type
  TFServiceWizardForm = class(TForm)
    pnlHeading: TPanel;
    lblHeading: TLabel;
    imgHeading: TImage;
    pnlButtons: TPanel;
    Bevel1: TBevel;
    btFinish: TButton;
    btCancel: TButton;
    pnlContent: TPanel;
    lbConfigurationTitle: TLabel;
    ckSeparetedUnits: TCheckBox;
    Label1: TLabel;
    edServiceName: TEdit;
    lbInterfaceImplementationNames: TLabel;
    ckUseModel: TCheckBox;
    lbServiceModel: TLabel;
    edServiceModel: TEdit;
    ckAddSampleMethods: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btFinishClick(Sender: TObject);
    procedure edServiceNameChange(Sender: TObject);
    procedure ckSeparetedUnitsClick(Sender: TObject);
    procedure ckUseModelClick(Sender: TObject);
  private
    { Private declarations }
    FSettings: TXDataServiceSettings;
    FUpdating: boolean;
    procedure LoadSettings;
    procedure SaveSettings;
    procedure UpdateUI;
    function Validate: Boolean;
  public
    { Public declarations }
    property Settings: TXDataServiceSettings read FSettings;
  end;

var
  FServiceWizardForm: TFServiceWizardForm;

implementation

{$R *.dfm}

{ TFServiceWizardForm }

procedure TFServiceWizardForm.btFinishClick(Sender: TObject);
begin
  if not Validate then
  begin
    ModalResult := mrNone;
    Exit;
  end;
  SaveSettings;
end;

procedure TFServiceWizardForm.ckSeparetedUnitsClick(Sender: TObject);
begin
  UpdateUI;
end;

procedure TFServiceWizardForm.ckUseModelClick(Sender: TObject);
begin
  UpdateUI;
end;

procedure TFServiceWizardForm.edServiceNameChange(Sender: TObject);
begin
  UpdateUI;
end;

procedure TFServiceWizardForm.FormCreate(Sender: TObject);
begin
  FSettings := TXDataServiceSettings.Create;
  FUpdating := False;
  LoadSettings;
  UpdateUI;
end;

procedure TFServiceWizardForm.FormDestroy(Sender: TObject);
begin
  FSettings.Free;
end;

procedure TFServiceWizardForm.LoadSettings;
begin
  edServiceName.Text := FSettings.ServiceName;
  ckSeparetedUnits.Checked := FSettings.SeparetedUnits;
  ckAddSampleMethods.Checked := FSettings.AddSampleMethods;
  ckUseModel.Checked := FSettings.Model <> '';
  edServiceModel.Text := FSettings.Model;
end;

procedure TFServiceWizardForm.SaveSettings;
begin
  FSettings.ServiceName := edServiceName.Text;
  FSettings.Model := edServiceModel.Text;
  FSettings.SeparetedUnits := ckSeparetedUnits.Checked;
  FSettings.AddSampleMethods := ckAddSampleMethods.Checked;
end;

procedure TFServiceWizardForm.UpdateUI;
begin
  if FUpdating then
    Exit;

  FUpdating := True;
  try
    if edServiceName.Text <> '' then
      lbInterfaceImplementationNames.Caption := Format('%s / %s', [
        'I' + edServiceName.Text + 'Service',
        'T' + edServiceName.Text + 'Service'
      ])
    else
      lbInterfaceImplementationNames.Caption := '';
    lbServiceModel.Enabled := ckUseModel.Checked;
    edServiceModel.Enabled := ckUseModel.Checked;
    if not edServiceModel.Enabled then
      edServiceModel.Text := '';
  finally
    FUpdating := False;
  end;
end;

function TFServiceWizardForm.Validate: Boolean;
begin
  if edServiceName.Text = '' then
  begin
    ShowMessage('Enter the name of the service!');
    edServiceName.SetFocus;
    Exit(False);
  end;
  if ckUseModel.Checked and (edServiceModel.Text = '') then
  begin
    ShowMessage('Enter the name of the service model!');
    edServiceModel.SetFocus;
    Exit(False);
  end;
  Result := True;
end;

end.
