import { Ability } from '@casl/ability'
import { NuxtConfig } from '@nuxt/types/config'
import { AnyStore, ItemMenu, ItemsMenu, Menu } from '~/types/interfaces'
import BaseMenu from '~/composables/layout/Menus/baseMenu'

/**
 * @category composables/layout/Menus
 * @class ConfigurationMenu
 * Classe pour la construction du Menu Paramètres
 */
class ConfigurationMenu extends BaseMenu implements Menu {
  private $ability: Ability;
  private $store: AnyStore;

  /**
   * @constructor
   * Initialisation des services issues du context
   */
  constructor ($config: NuxtConfig, $ability: Ability, $store: AnyStore) {
    super($config)
    this.$ability = $ability
    this.$store = $store
  }

  /**
   * Construit le menu Header Configuration ou null si aucune page accessible
   * @return {ItemMenu | null}
   */
  getHeaderMenu (): ItemMenu | null {
    const children: ItemsMenu = []

    if (this.$ability.can('display', 'organization_page')) {
      children.push(this.constructMenu('organization_page', undefined, `/main/organizations/${this.$store.state.profile.organization.id}/dashboard`, true))
    }

    if (this.$ability.can('display', 'cmf_licence_page')) {
      children.push(this.constructMenu('cmf_licence_generate', undefined, '/licence_cmf/organization', true))
    }

    if (this.$ability.can('display', 'parameters_page')) {
      children.push(this.constructMenu('parameters', undefined,`/main/edit/parameters/${this.$store.state.profile.organization.id}`, true))
    }

    if (this.$ability.can('display', 'place_page')) {
      children.push(this.constructMenu('place', undefined, '/places/list/', true))
    }

    if (this.$ability.can('display', 'education_page')) {
      children.push(this.constructMenu('education', undefined, '/educations/list/', true))
    }

    if (this.$ability.can('display', 'tag_page')) {
      children.push(this.constructMenu('tag', undefined, '/taggs/list/', true))
    }

    if (this.$ability.can('display', 'activities_page')) {
      children.push(this.constructMenu('activities', undefined, '/activities/list/', true))
    }

    if (this.$ability.can('display', 'template_systems_page')) {
      children.push(this.constructMenu('template_systems', undefined,'/template_systems/list/', true))
    }

    if (this.$ability.can('display', 'billing_settings_page')) {
      children.push(this.constructMenu('billing_settings', undefined, '/main/edit/billing_settings/' + this.$store.state.profile.organization.id, true))
    }

    if (this.$ability.can('display', 'online_registration_settings_page')) {
      children.push(this.constructMenu('online_registration_settings', undefined, '/main/edit/online_registration_settings/' + this.$store.state.profile.organization.id, true))
    }

    if (this.$ability.can('display', 'transition_next_year_page')) {
      children.push(this.constructMenu('transition_next_year', undefined, '/transition_next_year', true))
    }

    if (this.$ability.can('display', 'course_duplication_page')) {
      children.push(this.constructMenu('course_duplication', undefined, '/duplicate_courses', true))
    }

    if (this.$ability.can('display', 'import_page')) {
      children.push(this.constructMenu('import', undefined, '/import/all', true))
    }

    if (children.length === 1) {
      return children[0]
    } else if (children.length > 0) {
      return this.constructMenu('configuration', 'fa-cogs', undefined, undefined, children)
    } else {
      return null
    }
  }
}

export const getConfigurationMenu = ($config: NuxtConfig, $ability: Ability, $store: AnyStore) => new ConfigurationMenu($config, $ability, $store).getHeaderMenu()
