import { Component, Injectable } from '@angular/core';
import { ConfigService } from '@shared/config/config.service';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';

export interface IServiceTabs {
  [tabName: string]: {
    component: Component,
    tabDisplayName: string,
  }
}

export interface IPluginServiceTabs {
  [panelName: string]: IServiceTabs
}

@Injectable({
  providedIn: 'root'
})
export class PluginsComponentsRegistryService {
  constructor(private config: ConfigService) {
  }

  private components: { [pluginName: string]: { [panelName: string]: { [componentName: string]: Component } } } = {};

  private serviceTabs: { [pluginName: string]: IPluginServiceTabs } = {};

  public getAdditionalEntityType(pluginName: string): string {
    switch (pluginName) {
      case 'domains':
        return 'domain'
      default:
        return null;
    }
  }

  public getAlternateServiceEditURL(pluginName: string, panelName: string, id: FleioId): {
    url: string,
    tooltip: string
  } {
    if (!id) {
      return null;
    }
    switch (pluginName) {
      case 'domains':
        if (panelName === 'staff') {
          return {url: this.config.getPanelUrl(`${pluginName}/${id}/edit`), tooltip: $localize`Edit domain`};
        } else {
          return null;
        }
      default:
        return null
    }
  }

  public registerComponent(pluginName: string, panelName: string, componentName: string, component) {
    if (!this.components[pluginName]) {
      this.components[pluginName] = {};
    }
    const plugin = this.components[pluginName];

    if (!plugin[panelName]) {
      plugin[panelName] = {};
    }
    const panel = plugin[panelName];

    if (!panel[componentName]) {
      panel[componentName] = component;
    } else {
      console.error(`Component ${componentName} already registered for plugin ${pluginName}, panel ${panelName}`);
    }
  }

  public registerServiceTab(pluginName: string, panelName: string, tabName: string, tabDisplayName: string, component) {
    if (!this.serviceTabs[pluginName]) {
      this.serviceTabs[pluginName] = {};
    }
    const pluginTabs = this.serviceTabs[pluginName];

    if (!pluginTabs[panelName]) {
      pluginTabs[panelName] = {};
    }
    const panelTabs = pluginTabs[panelName];

    if (!panelTabs[tabName]) {
      panelTabs[tabName] = {component, tabDisplayName};
    } else {
      console.error(`Tab ${tabName} already registered for plugin ${pluginName}, panel ${panelName}`);
    }
  }

  public getComponent(pluginName: string, panelName: string, componentName: string) {
    if (!this.components[pluginName]) {
      console.info(`Plugin ${pluginName} does not exist`);
      return null;
    }
    const plugin = this.components[pluginName];

    if (!plugin[panelName]) {
      console.error(`Panel ${panelName} does not exist`);
      return null;
    }
    const panel = plugin[panelName];

    if (!panel[componentName]) {
      console.info(`Component ${componentName} does not exist`);
      return null;
    } else {
      return panel[componentName];
    }
  }

  public getServiceTabs(pluginName: string, panelName: string): IServiceTabs {
    if (!this.serviceTabs[pluginName]) {
      console.info(`Plugin ${pluginName} does not exists`);
      return {};
    }
    const pluginTabs = this.serviceTabs[pluginName];

    if (!pluginTabs[panelName]) {
      console.info(`Panel ${panelName} does not exists`);
      return {};
    }
    return pluginTabs[panelName];
  }
}
