window.sweKvgStations = {};

(function (self) {
  'use strict'

  /**
   * The extension class.
   *
   * @type {string}
   */
  const _extClass = 'tx-swekvgstations'

  let _requiredModules = [
      'sweKvgStationsHelper',
    ],
    _suggestedModules = [],
    _checked = false,
    _filename = 'main',
    _selectors = {
      extClass: '.' + _extClass,
      select: {
        station: '#station',
        lineContainer: '[data-station]',
      },
    }

  /**
   * @private
   */
  function _check () {
    if (_checked) return
    if (!sweKvgStationsDebugger) throw '[' + _filename + '.js] Required namespace "sweKvgStationsDebugger" not found.'

    sweKvgStationsDebugger.setExtClass(_extClass)
    sweKvgStationsDebugger.checkModules(_requiredModules, 'required', true, _filename)
    sweKvgStationsDebugger.checkModules(_suggestedModules, 'suggested', false, _filename)
    _checked = true
  }

  /**
   * @private
   */
  function _initSelectBoxes() {
    let stationSelect = sweKvgStationsHelper.getElement(_selectors.select.station),
      lineContainers = sweKvgStationsHelper.getElements(_selectors.select.lineContainer)

    if (!stationSelect) return

    stationSelect.addEventListener('change', function () {
      let title = stationSelect.value

      for (let i = 0; i < lineContainers.length; i += 1) {
        let container = lineContainers[i],
          dataStation = container.getAttribute('data-station'),
          method = title === dataStation ? 'show' : 'hide'

        sweKvgStationsHelper[method](container)
      }
    })

    for (let i = 0; i < lineContainers.length; i += 1) {
      let select = sweKvgStationsHelper.getElement('select', lineContainers[i])
      if (!select) continue

      select.addEventListener('change', function () {
        let option = select.options[select.selectedIndex]

        if (!option.hasAttribute('data-pdf') || !option.getAttribute('data-pdf').length) return
        window.open(option.getAttribute('data-pdf'), 'Aushang', 'width=600,menubar=no,toolbar=no,scrollbar=yes,resiteable=yes').focus()
      })
    }
  }

  /**
   * Initiate the module.
   */
  self.init = function () {
    _check()
    sweKvgStationsIE.setup(_extClass)
    sweKvgStationsHelper.setCookieProtection(false)
    let isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor),
      isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1

    if (sweKvgStationsIE.isIE()) document.body.classList.add('ie')
    if (isChrome) document.body.classList.add('chrome')
    if (isFirefox) document.body.classList.add('firefox')

    if (!document.querySelector(_selectors.extClass)) return
    _initSelectBoxes()
  }

})(window.sweKvgStations)

// Initiate this js module when the content is loaded.
document.addEventListener('DOMContentLoaded', function () {
  try {
    window.sweKvgStations.init()
  } catch (e) {
    console.warn('unable to load sweKvgStations.')
    console.warn(e)
  }
})
