import $ from 'jquery'
import Cookies from 'js-cookie'

const disclaimer = () => {
  init()
}
const init = () => {
  // console.log($('body').hasClass('needDisclaimer'))
  if ($('body').hasClass('needDisclaimer')) {
    // console.log(lpscripts.nonce, wpmlLang)
    var data = {
      action: 'lpdisclaimer',
      nonce: lpscripts.nonce,
      lang: wpmlLang, // defined in header
    }
    $.post(lpscripts.ajax_url, data, function (response) {
      // console.log('response', response)
      $('body').append(response)
      disclaimerInit()
    })
      .done(function () {
        console.log('done')
      })
      .fail(function (e) {
        document.body.classList.remove('need-disclaimer')
        console.log('Failed to load XNR', e)
        disclaimerValidation(false)
      })
      .always(function () {})
  } else {
  }
}
// DISCLAIMER
const disclaimerInit = () => {
  // console.log('disclaimer init')
  var dataLang = wpmlLang // wpmlLang is defined as a JS war in the header
  var legalAge = 18
  jQuery('#disclaimer form').each(function () {
    $(this)
      .find(':input')
      .on('focus', function (e) {
        if ($(this)[0].tagName.toUpperCase() == 'SELECT') {
          $(this).parent().removeClass('error')
        } else {
          $(this).removeClass('error')
        }
      })
  })
  $('#disclaimer #countries').on('change', (e) => {
    legalAge = parseInt(
      e.currentTarget[e.currentTarget.selectedIndex].dataset.age
    )
  })

  $('#disclaimer form').on('submit', function (e) {
    e.preventDefault()
    // reset
    $(this)
      .find(':input')
      .each(function () {
        if ($(this)[0].tagName.toUpperCase() == 'SELECT') {
          $(this).parent().removeClass('error')
        } else {
          $(this).removeClass('error')
        }
      })

    var form_data = $(this).serializeArray()
    let b = true
    let a = []
    const d = new Date()

    form_data.forEach((item) => {
      if (item.name == 'countries') {
        if (item.value == '-') {
          b = false
          a.push(item.name)
        }
      }
      if (item.name == 'year') {
        if (
          d.getFullYear() < parseInt(item.value) + legalAge ||
          item.value == ''
        ) {
          b = false
          a.push(item.name)
        }
      }
    })
    // console.log('result', b)
    if (b) {
      disclaimerValidation(true)
    } else {
      for (let index = 0; index < a.length; index++) {
        setTimeout(() => {
          const element = document.getElementsByName(a[index])[0]
          if (element.tagName.toUpperCase() == 'SELECT') {
            element.parentElement.classList.add('error')
          } else {
            element.classList.add('error')
          }
        }, 50)
      }
    }
  })
}

const disclaimerValidation = (b) => {
  if (b) {
    Cookies.set('disclaimer', true, { path: '/', expires: 365 })
  }
  window.scrollTo(0, 0)
  $('#disclaimer').fadeOut(600, function () {
    $(this).remove()
  })
  $('body').removeClass('needDisclaimer')
  window.scrollTo(0, 0)
  $(window).trigger('resize')
  $(window).trigger('scroll')
  window.dispatchEvent(new Event('resize'))
}

export { disclaimer }
