import videojs from 'video.js'
import { VideojsMenu, VideojsMenuButton, VideojsMenuButtonOptions, VideojsPlayer } from '../../types'
import { ResolutionMenuItem } from './resolution-menu-item'

const Menu = videojs.getComponent('Menu') as typeof VideojsMenu
const MenuButton = videojs.getComponent('MenuButton') as typeof VideojsMenuButton

class ResolutionMenuButton extends MenuButton {
  declare labelEl_: HTMLElement

  constructor (player: VideojsPlayer, options?: VideojsMenuButtonOptions) {
    super(player, options)

    this.controlText('Quality')

    player.peertubeResolutions().on('resolutions-added', () => this.update())
    player.peertubeResolutions().on('resolutions-removed', () => this.update())

    // For parent
    player.peertubeResolutions().on('resolutions-changed', () => {
      setTimeout(() => this.trigger('label-updated'))
    })
  }

  createEl () {
    const el = super.createEl()

    this.labelEl_ = videojs.dom.createEl('div', {
      className: 'vjs-resolution-value'
    }) as HTMLElement

    el.appendChild(this.labelEl_)

    return el
  }

  updateARIAAttributes () {
    this.el().setAttribute('aria-label', 'Quality')
  }

  createMenu () {
    const menu = new Menu(this.player_, { menuButton: this })
    const resolutions = this.player().peertubeResolutions().getResolutions()

    for (const r of resolutions) {
      const label = r.label === '0p'
        ? this.player().localize('Audio only')
        : r.label

      const component = new ResolutionMenuItem(
        this.player_,
        {
          id: r.id + '',
          resolutionId: r.id,
          label,
          selected: r.selected
        }
      )

      menu.addItem(component)
    }

    return menu
  }

  update () {
    super.update()

    this.trigger('resolution-menu-changed')
  }

  buildCSSClass () {
    return 'vjs-resolution-button ' + super.buildCSSClass()
  }

  buildWrapperCSSClass () {
    return 'vjs-resolution-control ' + super.buildWrapperCSSClass()
  }
}

videojs.registerComponent('ResolutionMenuButton', ResolutionMenuButton)
