unit View.Login;

interface

uses
  System.SysUtils, System.Classes, WEBLib.Graphics, WEBLib.Controls, WEBLib.Forms, WEBLib.Dialogs,
  Vcl.Controls, Vcl.StdCtrls, WEBLib.StdCtrls, WEBLib.JSON,
  JS, XData.Web.Connection, WEBLib.ExtCtrls, Web,
  App.Types, ConnectionModule, XData.Web.Client;

type
  TFViewLogin = class(TWebForm)
    WebLabel1: TWebLabel;
    edtUsername: TWebEdit;
    edtPassword: TWebEdit;
    btnLogin: TWebButton;
    pnlMessage: TWebPanel;
    lblMessage: TWebLabel;
    btnCloseNotification: TWebButton;
    XDataWebClient: TXDataWebClient;
    edtTaskId: TWebEdit;
    edtDate: TWebEdit;
    procedure btnLoginClick(Sender: TObject);
    procedure btnCloseNotificationClick(Sender: TObject);
    procedure WebFormCreate(Sender: TObject);
  private
    FLoginProc: TSuccessProc;
    FMessage: string;
    FUserId: string;
    FTaskId: string;
    procedure ShowNotification(Notification: string);
    procedure HideNotification;
  public
    class procedure Display(LoginProc: TSuccessProc); overload;
    class procedure Display(LoginProc: TSuccessProc; AMsg: string); overload;
    class procedure Display(LoginProc: TSuccessProc; AUserId, ATaskId, AMsg: string); overload;
  end;

var
  FViewLogin: TFViewLogin;

implementation

uses
  Auth.Service,
  View.ErrorPage;

{$R *.dfm}

procedure TFViewLogin.btnLoginClick(Sender: TObject);
var
  timeEntriesParam: string;

  procedure LoginSuccess;
  begin
    if SameText(timeEntriesParam, 'true') then
      DMConnection.currentTaskId := ''
    else
      DMConnection.currentTaskId := edtTaskId.Text;

    FLoginProc;
  end;

  procedure LoginError(AMsg: string);
  begin
    ShowNotification('Login Error: ' + AMsg);
  end;

begin
  timeEntriesParam := Application.Parameters.Values['time_entries'];

  if SameText(timeEntriesParam, 'true') then
  begin
    AuthService.WebLogin(
      edtUsername.Text,
      'time_entries',
      edtDate.Text,
      edtPassword.Text,
      @LoginSuccess,
      @LoginError
    );
  end
  else
  begin
    AuthService.WebLogin(
      edtUsername.Text,
      'task_items',
      edtTaskId.Text,
      edtPassword.Text,
      @LoginSuccess,
      @LoginError
    );
  end;
end;

class procedure TFViewLogin.Display(LoginProc: TSuccessProc);
begin
  TFViewLogin.Display(LoginProc, '', '', '');
end;

class procedure TFViewLogin.Display(LoginProc: TSuccessProc; AMsg: string);
begin
  TFViewLogin.Display(LoginProc, '', '', AMsg);
end;

class procedure TFViewLogin.Display(LoginProc: TSuccessProc; AUserId, ATaskId, AMsg: string);

  procedure FormCreate(AForm: TObject);
  begin
    TFViewLogin(AForm).FUserId := AUserId;
    TFViewLogin(AForm).FTaskId := ATaskId;
    TFViewLogin(AForm).FMessage := AMsg;
  end;

begin
  console.log('TFViewLogin.Display start AUserId=' + AUserId + ' ATaskId=' + ATaskId + ' AMsg=' + AMsg);
  if Assigned(FViewLogin) then
    FViewLogin.Free;
  FViewLogin := TFViewLogin.CreateNew(@FormCreate);
  FViewLogin.FLoginProc := LoginProc;
  console.log('TFViewLogin.Display end');
end;

procedure TFViewLogin.HideNotification;
begin
  pnlMessage.ElementHandle.hidden := True;
end;

procedure TFViewLogin.ShowNotification(Notification: string);
begin
  if Notification <> '' then
  begin
    lblMessage.Caption := Notification;
    pnlMessage.ElementHandle.hidden := False;
  end;
end;

procedure TFViewLogin.WebFormCreate(Sender: TObject);
var
  timeEntriesParam: string;
begin
  console.log('TFViewLogin.WebFormCreate FUserId=' + FUserId + ' FTaskId=' + FTaskId + ' FMessage=' + FMessage);

  timeEntriesParam := Application.Parameters.Values['time_entries'];

  edtUsername.Text := FUserId;
  edtTaskId.Text := FTaskId;
  edtDate.Text := Application.Parameters.Values['date'];

  if SameText(timeEntriesParam, 'true') then
  begin
    WebLabel1.Caption := 'Time Entries Sign In';
    TJSHTMLElement(document.getElementById('view.login.taskid.group')).classList.add('d-none');
    TJSHTMLElement(document.getElementById('view.login.date.group')).classList.remove('d-none');
  end
  else
  begin
    WebLabel1.Caption := 'Task Items Sign In';
    TJSHTMLElement(document.getElementById('view.login.taskid.group')).classList.remove('d-none');
    TJSHTMLElement(document.getElementById('view.login.date.group')).classList.add('d-none');
  end;

  if FMessage <> '' then
    ShowNotification(FMessage)
  else
    HideNotification;
end;

procedure TFViewLogin.btnCloseNotificationClick(Sender: TObject);
begin
  HideNotification;
end;

end.
