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

/**
 * @category composables/layout/Menus
 * @class CotisationsMenu
 * Classe pour la construction du Menu Cotisation (CMF)
 */
class CotisationsMenu extends BaseMenu implements Menu {
  private $ability: Ability;

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

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

    if (this.$ability.can('display', 'rate_cotisation_page')) {
      children.push(this.constructMenu('rate_cotisation', 'fa-euro-sign', '/cotisation/rate', true))
    }

    if (this.$ability.can('display', 'parameters_cotisation_page')) {
      children.push(this.constructMenu('parameters_cotisation', 'fa-euro-sign', '/cotisation/parameter', true))
    }

    if (this.$ability.can('display', 'send_cotisation_page')) {
      children.push(this.constructMenu('send_cotisation', 'fa-euro-sign', '/cotisation/send', true))
    }

    if (this.$ability.can('display', 'state_cotisation_page')) {
      children.push(this.constructMenu('state_cotisation', 'fa-euro-sign', '/cotisation/state', true))
    }

    if (this.$ability.can('display', 'pay_cotisation_page')) {
      children.push(this.constructMenu('pay_cotisation', 'fa-euro-sign', '/cotisation/pay', true))
    }

    if (this.$ability.can('display', 'check_cotisation_page')) {
      children.push(this.constructMenu('check_cotisation', 'fa-euro-sign', '/cotisation/check', true))
    }

    if (this.$ability.can('display', 'ledger_cotisation_page')) {
      children.push(this.constructMenu('ledger_cotisation', 'fa-euro-sign', '/cotisation/ledger', true))
    }

    if (this.$ability.can('display', 'magazine_cotisation_page')) {
      children.push(this.constructMenu('magazine_cotisation', 'fa-euro-sign', '/cotisation/magazine', true))
    }

    if (this.$ability.can('display', 'ventilated_cotisation_page')) {
      children.push(this.constructMenu('ventilated_cotisation', 'fa-euro-sign', '/cotisation/ventilated', true))
    }

    if (this.$ability.can('display', 'pay_erase_cotisation_page')) {
      children.push(this.constructMenu('pay_erase_cotisation', 'fa-euro-sign', '/cotisation/payerase', true))
    }

    if (this.$ability.can('display', 'resume_cotisation_page')) {
      children.push(this.constructMenu('resume_cotisation', 'fa-euro-sign', '/cotisation/resume', true))
    }

    if (this.$ability.can('display', 'history_cotisation_page')) {
      children.push(this.constructMenu('history_cotisation', 'fa-euro-sign', '/cotisation/history', true))
    }

    if (this.$ability.can('display', 'call_cotisation_page')) {
      children.push(this.constructMenu('call_cotisation', 'fa-euro-sign', '/cotisation/call', true))
    }

    if (this.$ability.can('display', 'history_struture_cotisation_page')) {
      children.push(this.constructMenu('history_struture_cotisation', 'fa-euro-sign', '/cotisation/historystructure', true))
    }

    if (this.$ability.can('display', 'insurance_cotisation_page')) {
      children.push(this.constructMenu('insurance_cotisation', 'fa-euro-sign', '/cotisation/insurance', true))
    }

    if (this.$ability.can('display', 'resume_all_cotisation_page')) {
      children.push(this.constructMenu('resume_all_cotisation', 'fa-euro-sign', '/cotisation/resumeall', true))
    }

    if (this.$ability.can('display', 'resume_pay_cotisation_page')) {
      children.push(this.constructMenu('resume_pay_cotisation', 'fa-euro-sign', '/cotisation/resumepay', true))
    }

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

export const getCotisationsMenu = ($config: NuxtConfig, $ability: Ability) => new CotisationsMenu($config, $ability).getMenu()
