import { ApiFactory, HarmonixApi, PluginInfo, RegionManager, createRegionHost, createRegionManager } from "@uxland/harmonix";
import { primariaShellId } from "../constants";
import { createBroker } from "./broker/factory";
import { PrimariaBroker } from "./broker/primaria-broker";
import { EcapEventManager, createEcapEventManager } from "./ecap-event-manager/ecap-event-manager";
import { ExitGuardManager, ExitGuardManagerImpl } from "./exit-guard-manager/exit-guard-manager";
import { PrimariaGlobalStateManager, createGlobalStateManager } from "./global-state/global-state";
import { HttpClient, createHttpClient } from "./http-client/http-client";
import { PrimariaInteractionService } from "./interaction-service";
import { ParimariaInteractionServiceImpl } from "./interaction-service/interaction-service-impl";
import { createLocaleManager } from "./localization/localization";
import { PrimariaNotificationService } from "./notification-service/notification-service";
import { PrimariaNotificationServiceImpl } from "./notification-service/notification.service-impl";
import { PdfViewerManager, createPdfViewerManager } from "./pdf-viewer-manager/pdf-viewer-manager";
import { PluginBusyManager, PluginBusyManagerImpl } from "./plugin-busy-manager/plugin-busy-manager";
import { QuickActionBusyManager, QuickActionBusyManagerImpl } from "./quick-action-busy-manager/quick-action-busy-manager";
import { PrimariaRegionManager, createRegionManagerProxy } from "./region-manager/region-manager";
import { TokenManager, createTokenManager } from "./token-manager/token-manager";
import { UserManager, createUserManager } from "./user-manager/user-manager";
import { PrimariaImportDataManager, ImportDataManagerImpl } from "./import-data-manager";
import { PrimariaContextManager, createContextManager } from "./context-manager/context-manager";

const broker = createBroker();

export interface PrimariaApi extends HarmonixApi {
  httpClient: HttpClient;
  interactionService: PrimariaInteractionService;
  notificationService: PrimariaNotificationService;
  broker: PrimariaBroker;
  regionManager: PrimariaRegionManager;
  globalStateManager: PrimariaGlobalStateManager;
  contextManager: PrimariaContextManager;
  tokenManager: TokenManager;
  userManager: UserManager;
  ecapEventManager: EcapEventManager;
  pluginBusyManager: PluginBusyManager;
  exitGuardManager: ExitGuardManager;
  quickActionBusyManager: QuickActionBusyManager;
  pdfViewerManager: PdfViewerManager;
  importDataManager: PrimariaImportDataManager;
}

const regionManager: RegionManager = createRegionManager("primaria");
export const PrimariaRegionHost: any = createRegionHost(regionManager as any);
const tokenManager = createTokenManager();
const userManager = createUserManager(tokenManager);
const globalStateManager: PrimariaGlobalStateManager = createGlobalStateManager(broker);
const contextManager = createContextManager();
const pluginBusyManager = new PluginBusyManagerImpl();
const exitGuardManager = new ExitGuardManagerImpl();
const quickActionBusyManager = new QuickActionBusyManagerImpl(broker);
const interactionService = new ParimariaInteractionServiceImpl();
const notificationService = new PrimariaNotificationServiceImpl();
const ecapEventManager = createEcapEventManager(globalStateManager);
const pdfViewerManager = createPdfViewerManager(broker, notificationService);
const importDataManager = new ImportDataManagerImpl(interactionService);

/**
 * Factory function that creates a Primaria API instance.
 *
 * @param {PluginInfo} pluginInfo - Information about the plugin
 * @return {PrimariaApi} The created Primaria API instance
 */
export const primariaApiFactory: ApiFactory<PrimariaApi> = (pluginInfo: PluginInfo): PrimariaApi => {
  const regionManagerProxy = createRegionManagerProxy(pluginInfo, regionManager, broker);

  return {
    pluginInfo: pluginInfo,
    regionManager: regionManagerProxy,
    httpClient: createHttpClient(tokenManager, broker),
    broker,
    createLocaleManager: createLocaleManager(pluginInfo.pluginId) as any,
    globalStateManager,
    contextManager,
    tokenManager,
    userManager,
    ecapEventManager,
    pluginBusyManager,
    exitGuardManager,
    quickActionBusyManager,
    interactionService,
    notificationService,
    pdfViewerManager,
    importDataManager,
  };
};

export const shellApi = primariaApiFactory({ pluginId: primariaShellId }) as PrimariaApi;
