unit uFactuGES_App;

interface

uses
  SysUtils, Classes, Forms, uBizUsuarios, uBizEmpresas, uControllerBase,
  uEmpresasController, uUsuariosController, uHostManager, uModuleController;

type
  TAppFactuGES_Event = procedure;

  IAppSplashForm = interface
    ['{9905DF57-4476-42E6-A7CD-B1479A84E220}']
    procedure Show;
    procedure Hide;
    procedure Update;
    function GetMensaje: String;
    procedure SetMensaje(const AMensaje : String);
    property Mensaje : String read GetMensaje write SetMensaje;
  end;

  IAppFactuGES = interface
    ['{FB6A0F3C-C1D1-462D-AFD4-2A368F85E920}']
    function GetMainForm: TCustomForm;
    function GetEmpresasController: IEmpresasController;
    function GetUsuariosController: IUsuariosController;
    function GetAppName: String;
    function GetAppVersion: String;
    function GetAppFullName: String;
    function GetUsuarioActivo: IBizUsuario;
    function GetEmpresaActiva: IBizEmpresa;
    function GetModuleManager : THostManager;
    function GetTerminated: Boolean;

    function GetLoadModulesEvent : TAppFactuGES_Event;
    procedure SetLoadModulesEvent (ALoadModulesEvent : TAppFactuGES_Event);

    function GetDoMainFormEvent : TAppFactuGES_Event;
    procedure SetDoMainFormEvent (AMainFormEvent : TAppFactuGES_Event);

    function GetDoRefreshMainFormEvent : TAppFactuGES_Event;
    procedure SetDoRefreshMainFormEvent (ARefreshMainFormEvent : TAppFactuGES_Event);

    function GetAppSplashForm : IAppSplashForm;
    procedure SetAppSplashForm (AAppSplashForm : IAppSplashForm);

    procedure SeleccionarEmpresa;    
    procedure CambiarEmpresa(const AIDEmpresa : Integer);

    procedure ShowSplashForm;
    procedure HideSplashForm;

    procedure Run;
    procedure Terminate;

    function GetModule(AModuleName: String): TModuleController;

    property AppVersion : String read GetAppVersion;
    property AppName : String read GetAppName;
    property AppFullName : String read GetAppFullName;
    property MainForm : TCustomForm read GetMainForm;
    property EmpresaActiva : IBizEmpresa read GetEmpresaActiva;
    property UsuarioActivo : IBizUsuario read GetUsuarioActivo;
    property EmpresasController : IEmpresasController read GetEmpresasController;
    property UsuariosController : IUsuariosController read GetUsuariosController;
    property AppSplashForm : IAppSplashForm read GetAppSplashForm write SetAppSplashForm;
    property ModuleManager : THostManager read GetModuleManager;
    property DoMainFormEvent : TAppFactuGES_Event read GetDoMainFormEvent write SetDoMainFormEvent;
    property DoLoadModulesEvent : TAppFactuGES_Event read GetLoadModulesEvent write SetLoadModulesEvent;
    property DoRefreshMainFormEvent : TAppFactuGES_Event read GetDoRefreshMainFormEvent write SetDoRefreshMainFormEvent;
    property Terminated : Boolean read GetTerminated;
  end;


  TAppFactuGES = class(TInterfacedObject, IAppFactuGES)
  private
    FEmpresasController : IEmpresasController;
    FUsuariosController: IUsuariosController;
    FEmpresaActiva: IBizEmpresa;
    FAppSplashForm : IAppSplashForm;
    FHostManager : THostManager;
    FTerminated : Boolean;

    FDoMainFormEvent: TAppFactuGES_Event;
    FDoLoadModulesEvent: TAppFactuGES_Event;
    FDoRefreshMainFormEvent: TAppFactuGES_Event;

    function GetMainForm: TCustomForm;
    function GetEmpresasController: IEmpresasController;
    function GetUsuariosController: IUsuariosController;
    function GetAppName: String;
    function GetAppVersion: String;
    function GetAppFullName: String;
    function GetUsuarioActivo: IBizUsuario;
    function GetEmpresaActiva: IBizEmpresa;
    function GetModuleManager : THostManager;
    function GetTerminated: Boolean;

    function GetLoadModulesEvent : TAppFactuGES_Event;
    procedure SetLoadModulesEvent (ALoadModulesEvent : TAppFactuGES_Event);

    function GetDoMainFormEvent : TAppFactuGES_Event;
    procedure SetDoMainFormEvent (AMainFormEvent : TAppFactuGES_Event);

    function GetDoRefreshMainFormEvent : TAppFactuGES_Event;
    procedure SetDoRefreshMainFormEvent (ARefreshMainFormEvent : TAppFactuGES_Event);

    function GetAppSplashForm : IAppSplashForm;
    procedure SetAppSplashForm (AAppSplashForm : IAppSplashForm);

    procedure AfterLoadModule(Sender: TObject; AModuleInfo: TModuleInfo);
    procedure RegisterModule(AModuleInfo : TModuleInfo);
  protected
    procedure DoMainForm;
    procedure DoRefreshMainForm;

    procedure CargarModulos;
    procedure ShowSplashForm;
    procedure HideSplashForm;
    procedure UpdateSplashForm;
  public
    constructor Create; virtual;
    destructor Destroy; override;

    procedure Run;
    procedure Terminate;

    procedure SeleccionarEmpresa;
    procedure CambiarEmpresa(const AIDEmpresa : Integer);

    function GetModule(AModuleName: String): TModuleController;

    property AppVersion : String read GetAppVersion;
    property AppName : String read GetAppName;
    property AppFullName : String read GetAppFullName;
    property MainForm : TCustomForm read GetMainForm;
    property EmpresaActiva : IBizEmpresa read GetEmpresaActiva;
    property AppSplashForm : IAppSplashForm read GetAppSplashForm write SetAppSplashForm;
    property UsuarioActivo : IBizUsuario read GetUsuarioActivo;
    property EmpresasController : IEmpresasController read GetEmpresasController;
    property UsuariosController : IUsuariosController read GetUsuariosController;
    property ModuleManager : THostManager read GetModuleManager;
    property DoMainFormEvent : TAppFactuGES_Event read GetDoMainFormEvent write SetDoMainFormEvent;
    property DoLoadModulesEvent : TAppFactuGES_Event read GetLoadModulesEvent write SetLoadModulesEvent;
    property DoRefreshMainFormEvent : TAppFactuGES_Event read GetDoRefreshMainFormEvent write SetDoRefreshMainFormEvent;
    property Terminated : Boolean read GetTerminated;
  end;

var
  AppFactuGES : IAppFactuGES = nil;

implementation
{$INCLUDE ..\FactuGES.inc}

uses
  Windows, Dialogs, JSDialog, JSDialogs, uDialogUtils, cxControls, uUsuariosViewRegister,
  uEmpresasViewRegister, uAppInfoUtils, uDMBase;

procedure TAppFactuGES.CambiarEmpresa(const AIDEmpresa: Integer);
var
  Aux : IBizEmpresa;
begin
  Aux := EmpresasController.Buscar(AIDEmpresa);
  if Assigned(Aux) then
  begin
    Aux.DataTable.Active := True;
    if not Aux.IsEmpty then
      FEmpresaActiva := Aux;
  end;

  if Assigned(FEmpresaActiva) then
     dmBase.dxPrintStyleManager1Style1.PrinterPage.PageFooter.LeftTitle.Add(FEmpresaActiva.NOMBRE);
end;

procedure TAppFactuGES.CargarModulos;
begin
  UpdateSplashForm;
  if not Assigned(FDoLoadModulesEvent) then
    raise Exception.Create('Evento para la carga de los módulos de la aplicación no asignado (CargarModulos)');

  FDoLoadModulesEvent();
  UpdateSplashForm;
end;

constructor TAppFactuGES.Create;
begin
  FTerminated := False;
  FDoMainFormEvent := NIL;
  FDoLoadModulesEvent := NIL;
  FAppSplashForm := NIL;
  FEmpresaActiva := NIL;
  FUsuariosController := NIL; // Se crea la primera vez que se usa
 
  FEmpresasController := NIL; // Se crea la primera vez que se usa

  FHostManager := THostManager.Create(NIL);
  with FHostManager do
  begin
    OnAfterLoad := AfterLoadModule;
    // Ruta por defecto de los módulos
    BPLPath := ExtractFilePath(Application.ExeName) + PathDelim;
  end;
end;

destructor TAppFactuGES.Destroy;
begin
  FAppSplashForm := NIL;

  FHostManager.UnloadModules;
  FreeAndNIL(FHostManager);

  FEmpresaActiva := NIL;
  FUsuariosController := NIL;
  FEmpresasController := NIL;

  inherited;
end;

procedure TAppFactuGES.DoMainForm;
begin
  if not Assigned(FDoMainFormEvent) then
    raise Exception.Create('Evento para la creación del formulario principal no asignado (DoMainForm)');

  FDoMainFormEvent();
end;

function TAppFactuGES.GetMainForm: TCustomForm;
begin
  Result := Application.MainForm;
end;

function TAppFactuGES.GetDoMainFormEvent: TAppFactuGES_Event;
begin
  Result := FDoMainFormEvent;
end;

function TAppFactuGES.GetDoRefreshMainFormEvent: TAppFactuGES_Event;
begin
  Result := FDoRefreshMainFormEvent;
end;

function TAppFactuGES.GetAppFullName: String;
begin
  Result := uAppInfoUtils.GetAppFullName;
end;

function TAppFactuGES.GetAppName: String;
begin
  Result := uAppInfoUtils.GetAppName;
end;

function TAppFactuGES.GetAppSplashForm: IAppSplashForm;
begin
  Result := FAppSplashForm;
end;

function TAppFactuGES.GetAppVersion: String;
begin
  Result := uAppInfoUtils.GetAppVersion;
end;

function TAppFactuGES.GetEmpresaActiva: IBizEmpresa;
begin
  Result := FEmpresaActiva;
end;

function TAppFactuGES.GetEmpresasController: IEmpresasController;
begin
  if not Assigned(FEmpresasController) then
    FEmpresasController := TEmpresasController.Create;

  Result := FEmpresasController;
end;

function TAppFactuGES.GetLoadModulesEvent: TAppFactuGES_Event;
begin
  Result := FDoLoadModulesEvent;
end;

function TAppFactuGES.GetModule(AModuleName: String): TModuleController;
var
  i : integer;
begin
  Result := NIL;
  for i := 0 to ModuleManager.ModulesCount - 1 do
    if ModuleManager.Modules[i].Module.ModuleName = AModuleName then
      Result := ModuleManager.Modules[i].Module
end;

function TAppFactuGES.GetModuleManager: THostManager;
begin
  Result := FHostManager;
end;

function TAppFactuGES.GetTerminated: Boolean;
begin
  Result := FTerminated;
end;

function TAppFactuGES.GetUsuarioActivo: IBizUsuario;
begin
  Result := UsuariosController.CurrentUser;
end;

function TAppFactuGES.GetUsuariosController: IUsuariosController;
begin
  if not Assigned(FUsuariosController) then
    FUsuariosController := TUsuariosController.Create;

  Result := FUsuariosController;
end;

procedure TAppFactuGES.HideSplashForm;
begin
  if not Assigned(FAppSplashForm) then
    raise Exception.Create('AppSplashForm no asignado (HideSplashForm)');

    FAppSplashForm.Hide;
end;

procedure TAppFactuGES.AfterLoadModule(Sender: TObject;
  AModuleInfo: TModuleInfo);
begin
  RegisterModule(AModuleInfo);
end;

procedure TAppFactuGES.DoRefreshMainForm;
begin
  if not Assigned(FDoRefreshMainFormEvent) then
    raise Exception.Create('Evento para el refresco del formulario principal no asignado (DoRefreshMainForm)');

  FDoRefreshMainFormEvent();
end;

procedure TAppFactuGES.RegisterModule(AModuleInfo: TModuleInfo);
begin
  AppSplashForm.Mensaje := 'Cargando ''' + AModuleInfo.Module.ModuleName + '''...';
end;

procedure TAppFactuGES.Run;
begin
  if UsuariosController.StartLogin then
  begin
    ShowHourglassCursor;
    ShowSplashForm;
    try
      CargarModulos;
      DoMainForm;
      SeleccionarEmpresa;
      DoRefreshMainForm;
    finally
      HideSplashForm;
      HideHourglassCursor;
    end;
  end;
  Application.Run;
  Terminate;
end;

procedure TAppFactuGES.SetDoMainFormEvent(
  AMainFormEvent: TAppFactuGES_Event);
begin
  FDoMainFormEvent := AMainFormEvent;
end;

procedure TAppFactuGES.SetDoRefreshMainFormEvent(
  ARefreshMainFormEvent: TAppFactuGES_Event);
begin
  FDoRefreshMainFormEvent := ARefreshMainFormEvent;
end;

procedure TAppFactuGES.SeleccionarEmpresa;
var
  JsDialog: TJSDialog;
  AEmpresas: IBizEmpresa;
  AEmpresasController: IEmpresasController;
begin
  JsDialog := TJSDialog.Create(nil);
  try
    JsDialog.Content.Add('Selección de empresa');
    JsDialog.Instruction.Text := 'Elija la empresa con la que desea trabajar.';
    JsDialog.DialogOptions := [doCommandLinks, doModal];
    JsDialog.ButtonBar.Buttons := [cbCancel];
    JsDialog.Width := 600;


    //Añadimos las diferentes empresas que existan
    AEmpresasController := TEmpresasController.Create;
    AEmpresas := AEmpresasController.BuscarTodos;
    AEmpresas.DataTable.Active := True;

    AEmpresas.Datatable.First;
    while not AEmpresas.Datatable.eof do
    begin
      with JsDialog.CustomButtons.Add do
      begin
         Caption := AEmpresas.NIF_CIF + ' - ' + AEmpresas.NOMBRE;
         Value := AEmpresas.ID;
      end;
      AEmpresas.DataTable.Next;
    end;
    AEmpresas.DataTable.First;

    {$IFDEF MULTIEMPRESA}
    if JsDialog.Execute <> IDCANCEL then
       if JsDialog.CustomButtonResult > 0 then
          CambiarEmpresa(JsDialog.CustomButtonResult);
    {$ELSE}
    CambiarEmpresa(AEmpresas.ID);
    {$ENDIF}

  finally
    FreeAndNIL(JsDialog);
    AEmpresasController:= NIL;
    AEmpresas := NIL;
  end;
end;

procedure TAppFactuGES.SetAppSplashForm(AAppSplashForm: IAppSplashForm);
begin
  FAppSplashForm := AAppSplashForm;
end;

procedure TAppFactuGES.SetLoadModulesEvent(
  ALoadModulesEvent: TAppFactuGES_Event);
begin
  FDoLoadModulesEvent := ALoadModulesEvent;
end;

procedure TAppFactuGES.ShowSplashForm;
begin
  if not Assigned(FAppSplashForm) then
    raise Exception.Create('AppSplashForm no asignado (ShowSplashForm)');

  FAppSplashForm.Show;
  FAppSplashForm.Update;
end;

procedure TAppFactuGES.Terminate;
begin
  ModuleManager.UnloadModules;
  FTerminated := True;
end;

procedure TAppFactuGES.UpdateSplashForm;
begin
  FAppSplashForm.Update;
end;

initialization
  // Pongo esto aquí por ahora
  uUsuariosViewRegister.RegisterViews;
  uEmpresasViewRegister.RegisterViews;

finalization
  // Pongo esto aquí por ahora
  AppFactuGES := NIL;
  
  uUsuariosViewRegister.UnregisterViews;
  uEmpresasViewRegister.UnregisterViews;

end.
