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 AccountMenu
 * Classe pour la construction du Menu Mon compte
 */
class AccountMenu 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', 'my_schedule_page')) {
      children.push(this.constructMenu('my_schedule_page', undefined, '/my_calendar', true))
    }

    if (this.$ability.can('display', 'attendance_bookings_page')) {
      children.push(this.constructMenu('attendance_bookings_menu', undefined, '/own_attendance', true))
    }

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

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

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

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

    if (this.$ability.can('display', 'criteria_notations_page') || this.$ability.can('display', 'criteria_notations_page_from_account_menu')) {
      children.push(this.constructMenu('criteria_notations', undefined, '/criteria_notations/list/', true))
    }

    if (this.$ability.can('display', 'my_education_students_page')) {
      children.push(this.constructMenu('my_education_students', undefined, `/main/my_profile/${this.$store.state.profile.access.id}/dashboard/my_education_students/list/`, true))
    }

    if (this.$ability.can('display', 'send_an_email_page')) {
      children.push(this.constructMenu('send_an_email', undefined, `/list/create/emails`, true))
    }

    if (this.$ability.can('display', 'my_documents_page')) {
      children.push(this.constructMenu('my_documents', undefined, `/main/my_profile/${this.$store.state.profile.access.id}/dashboard/show/my_access_file`, true))
    }

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

    if (this.$ability.can('display', 'adherent_list_page')) {
      children.push(this.constructMenu('adherent_list', undefined, `/adherent_contacts/list/`, true))
    }

    if (this.$ability.can('display', 'subscription_page')) {
      children.push(this.constructMenu('my_subscription', undefined, `/subscription`))
    }

    if (this.$ability.can('display', 'my_bills_page')) {
      children.push(this.constructMenu('my_bills', undefined, `/main/my_profile/${this.$store.state.profile.access.id}/dashboard/show/my_bills`, true))
    }

    if (this.$ability.can('display', 'cmf_licence_person_page')) {
      children.push(this.constructMenu('print_my_licence', undefined, `/licence_cmf/user`, true))
    }

    const accountMenu = this.constructMenu('my_account', 'fa-user', undefined, undefined, children, false)

    const actions: ItemsMenu = [];
    actions.push(this.constructMenu('logout', undefined, `/logout`, true))

    accountMenu.actions = actions

    return accountMenu
  }
}

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