import { host } from './background.js'


const saveOptions = (e) => {
  saveHost('diaspora-host', 'diasporaHost')
  saveHost('friendica-host', 'friendicaHost')
  saveHost('gnusocial-host', 'gnusocialHost')
  saveHost('hubzilla-host', 'hubzillaHost')
  saveHost('lemmy-host', 'lemmyHost')
  saveHost('mastodon-host', 'mastodonHost')
  saveHost('pleroma-host', 'pleromaHost')
  saveHost('socialhome-host', 'socialhomeHost')
  saveHost('xmpp-host', 'xmppHost')
  
  saveCheckbox('diaspora-check', 'diasporaCheck')
  saveCheckbox('friendica-check', 'friendicaCheck')
  saveCheckbox('gnusocial-check', 'gnusocialCheck')
  saveCheckbox('hubzilla-check', 'hubzillaCheck')
  saveCheckbox('lemmy-check', 'lemmyCheck')
  saveCheckbox('mastodon-check', 'mastodonCheck')
  saveCheckbox('pleroma-check', 'pleromaCheck')
  saveCheckbox('socialhome-check', 'socialhomeCheck')
  saveCheckbox('xmpp-check', 'xmppCheck')
  
  e.preventDefault()
}


const saveHost = (hostId, hostKey) => {
  browser.storage.sync.set({
    [hostKey] : document.querySelector(`#${hostId}`).value
  })
}

const saveCheckbox = (checkId, checkKey) => {
  let checkbox = document.getElementById(`${checkId}`)
  if (checkbox.checked) {
    browser.storage.sync.set({
      [checkKey] : document.querySelector(`#${checkId}`).checked = true 
    })
  } else {
    browser.storage.sync.set({
      [checkKey] : document.querySelector(`#${checkId}`).checked = false 
    })
  }
}


const restoreOptions = () => {
  restoreHost('diasporaHost', 'diaspora-host', host.diasporaDefault)
  restoreHost('friendicaHost', 'friendica-host', host.friendicaDefault)
  restoreHost('gnusocialHost', 'gnusocial-host', host.gnusocialDefault)
  restoreHost('hubzillaHost', 'hubzilla-host', host.hubzillaDefault)
  restoreHost('lemmyHost', 'lemmy-host', host.lemmyDefault)
  restoreHost('mastodonHost', 'mastodon-host', host.mastodonDefault)
  restoreHost('pleromaHost', 'pleroma-host', host.pleromaDefault)
  restoreHost('socialhomeHost', 'socialhome-host', host.socialhomeDefault)
  restoreHost('xmppHost', 'xmpp-host', host.xmppDefault)

  restoreCheckbox('diasporaCheck', 'diaspora-check')
  restoreCheckbox('friendicaCheck', 'friendica-check')
  restoreCheckbox('gnusocialCheck', 'gnusocial-check')
  restoreCheckbox('hubzillaCheck', 'hubzilla-check')
  restoreCheckbox('lemmyCheck', 'lemmy-check')
  restoreCheckbox('mastodonCheck', 'mastodon-check')
  restoreCheckbox('pleromaCheck', 'pleroma-check')
  restoreCheckbox('socialhomeCheck', 'socialhome-check')
  restoreCheckbox('xmppCheck', 'xmpp-check')
}


const restoreHost = (hostKey, hostId, hostDefault) => {
  let gettingItem = browser.storage.sync.get(`${hostKey}`)
  gettingItem.then((res) => {
    document.querySelector(`#${hostId}`).value = res[hostKey] || hostDefault
  })
}

const restoreCheckbox = (checkKey, checkId) => {
  const checkbox = chrome.storage.sync.get(`${checkKey}`, value => {
    if (value[checkKey]) {
      document.getElementById(`${checkId}`).checked = true
    }
  })
}


document.addEventListener('DOMContentLoaded', restoreOptions, saveCheckbox)
document.querySelector("form").addEventListener("submit", saveOptions)
