unit uMainMenuController;

interface

uses
  SysUtils, Menus, Controls, Classes, uHostManager,
  PngImageList, uPantallaPrincipal;

type
  TMainMenuController = class(TObject)
  private
    FMainMenu: TMainMenu;
    FMainForm: IMainForm;
    FLargeImages: TImageList;
    FSmallImages: TImageList;
    procedure SetMainMenu(const Value: TMainMenu);
    procedure CambiarEmpresaClick(Sender: TObject);    
  public
    procedure RegisterModule(AModuleInfo : TModuleInfo);
    procedure SortMenuByTag;
    procedure InitMainMenu;

    property MainMenu : TMainMenu read FMainMenu write SetMainMenu;
    property SmallImages: TImageList read FSmallImages write FSmallImages;
    property LargeImages: TImageList read FLargeImages write FLargeImages;
    property MainForm : IMainForm read FMainForm write FMainForm;
  end;

procedure CopyImages(const SrcList: TPngImageList; var DstList:
        TPngImageList; out Index, Count: Integer);

var
  MainMenuController : TMainMenuController;

implementation

uses
  Dialogs, PNGImage, uModuleController, uFactuGES_App,
  uBizEmpresas, uMenuUtils;


{ TMainMenuController }

procedure CopyImages(const SrcList: TPngImageList; var DstList:
        TPngImageList; out Index, Count: Integer);
begin
  Index := -1;
  Count := 0;

  if not Assigned(SrcList) or not Assigned(DstList) then
    Exit;

  DstList.BeginUpdate;
  try
    Index := DstList.Count;
    DstList.AddImages(SrcList);
    Count := SrcList.Count;
  finally
    DstList.EndUpdate;
  end;
end;


procedure TMainMenuController.CambiarEmpresaClick(Sender: TObject);
begin
  AppFactuGES.CambiarEmpresa((Sender as TMenuItem).Tag);
end;

procedure TMainMenuController.InitMainMenu;
var
  AEmpresas : IBizEmpresa;
  AEmpresaMenu : TMenuItem;
  AListaEmpresasItem : TMenuItem;
  AItem: TMenuItem;
begin
    AEmpresaMenu := BuscarMenu(FMainMenu, 'Empresa');
    AListaEmpresasItem := AEmpresaMenu.Find('Lista de empresas');
    
    if Assigned(AListaEmpresasItem) then
    begin
      AEmpresas := AppFactuGES.EmpresasController.BuscarTodos;
      try
        AEmpresas.DataTable.Active := True;
        while not AEmpresas.DataTable.EOF do
        begin
          AItem := TMenuItem.Create(nil);
          with AItem do
          begin
            Caption := AEmpresas.NOMBRE;
            Tag := AEmpresas.ID;
            AutoCheck := True;
            ShortCut := 0;
            OnClick := CambiarEmpresaClick; 
            HelpContext := 0;
            RadioItem := True;
            Enabled := True;
          end;
          AListaEmpresasItem.Add(AItem);
          AEmpresas.Next;
        end;
      finally
        AEmpresas := NIL;
      end;
    end;
end;

procedure TMainMenuController.RegisterModule(AModuleInfo: TModuleInfo);
var
  AModule: TModuleController;
  AIndex: Integer;
  ACount: Integer;
begin
  AModule := AModuleInfo.Module;

  if Assigned(AModule.ModuleMenu) then
  begin
    CopyImages(TPngImageList(AModule.SmallImages), TPngImageList(FSmallImages), AIndex, ACount);
    CopyImages(TPngImageList(AModule.LargeImages), TPngImageList(FLargeImages), AIndex, ACount);

    MergeMenus(AModule.ModuleMenu, FMainMenu, AIndex);
  end;
end;

procedure TMainMenuController.SetMainMenu(const Value: TMainMenu);
begin
  FMainMenu := Value;
end;

procedure TMainMenuController.SortMenuByTag;
begin
  uMenuUtils.SortMenuByTag(FMainMenu);
end;


end.
