unit View.Main;

interface

uses
  System.SysUtils, System.Classes, WEBLib.Graphics, WEBLib.Forms, WEBLib.Dialogs,
  Vcl.Controls, Vcl.Forms, WEBLib.Controls, WEBLib.ExtCtrls, Vcl.StdCtrls, WEBLib.StdCtrls,
  App.Types,
  ConnectionModule, WEBLib.Lists;

type
  TFViewMain = class(TWebForm)
    WebPanel1: TWebPanel;
    WebLinkLabel1: TWebLinkLabel;
    WebLinkLabel2: TWebLinkLabel;
    WebLinkLabel3: TWebLinkLabel;
    WebLinkLabel4: TWebLinkLabel;
    WebLinkLabel5: TWebLinkLabel;
    WebLinkLabel6: TWebLinkLabel;
    WebLinkLabel7: TWebLinkLabel;
    lbUsername: TWebLabel;
    procedure WebFormCreate(Sender: TObject);
    procedure WebLinkLabel1Click(Sender: TObject);
    procedure WebLinkLabel2Click(Sender: TObject);
    procedure WebLinkLabel3Click(Sender: TObject);
    procedure WebLinkLabel4Click(Sender: TObject);
    procedure WebLinkLabel5Click(Sender: TObject);
    procedure WebLinkLabel6Click(Sender: TObject);
    procedure WebLinkLabel7Click(Sender: TObject);
  private
    FChildForm: TWebForm;
    FLogoutProc: TLogoutProc;
    procedure ShowForm(AFormClass: TWebFormClass);
    procedure ShowCrudForm(AFormClass: TWebFormClass);
  public
    class procedure Display(LogoutProc: TLogoutProc);
  end;

var
  FViewMain: TFViewMain;

implementation

uses
  JS,
  Auth.Service,
  View.Login,
  View.UserProfile,
  View.Home,
  View.Artist,
  View.Genre,
  View.Album,
  View.Track;

{$R *.dfm}

procedure TFViewMain.WebFormCreate(Sender: TObject);
begin
  lbUsername.Caption := JS.toString(AuthService.TokenPayload.Properties['user']);
  FChildForm := nil;
  ShowForm(TFViewHome);
end;

procedure TFViewMain.WebLinkLabel1Click(Sender: TObject);
begin
  ShowForm(TFViewHome);
end;

procedure TFViewMain.WebLinkLabel2Click(Sender: TObject);
begin
  ShowCrudForm(TFViewArtist);
end;

procedure TFViewMain.WebLinkLabel3Click(Sender: TObject);
begin
  ShowCrudForm(TFViewGenre);
end;

procedure TFViewMain.WebLinkLabel4Click(Sender: TObject);
begin
  ShowCrudForm(TFViewAlbum);
end;

procedure TFViewMain.WebLinkLabel5Click(Sender: TObject);
begin
  ShowCrudForm(TFViewTrack);
end;

procedure TFViewMain.WebLinkLabel6Click(Sender: TObject);
begin
  // logout
  FLogoutProc;
end;

procedure TFViewMain.WebLinkLabel7Click(Sender: TObject);
begin
  ShowForm(TFViewUserProfile);
end;

class procedure TFViewMain.Display(LogoutProc: TLogoutProc);
begin
  if Assigned(FViewMain) then
    FViewMain.Free;
  FViewMain := TFViewMain.CreateNew;
  FViewMain.FLogoutProc := LogoutProc;
end;

procedure TFViewMain.ShowCrudForm(AFormClass: TWebFormClass);
begin
  ShowForm(AFormClass);
end;

procedure TFViewMain.ShowForm(AFormClass: TWebFormClass);
begin
  if Assigned(FChildForm) then
    FChildForm.Free;
  Application.CreateForm(AFormClass, WebPanel1.ElementID, FChildForm);
end;

end.
