/*
    Copyleft (ɔ) 2021  Mickie

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

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


window.onload = () => {
  
  /*
   * Assign and get URLs To Respective Element
   */
  
  const urlAssigner = (btn = {}) => {
    const { id, href } = btn
    const a = document.getElementById(id)
    a.href = href
    a.target = '_blank'
    a.addEventListener('click', e => {
      window.setTimeout(() => window.close(), 10)
    })
  }

  (function getCurrentTabUrl() {
    const queryInfo = {
      active: true,
      currentWindow: true,
    }

    chrome.tabs.query(queryInfo, (tabs = []) => {
      if (tabs.length === 0) {
	return
      }

      const tab = (tabs[0] || {});
      const tabUrL = encodeURIComponent(tab.url)
      const tabTitle = encodeURIComponent(tab.title)
      mkBtns(tabUrL, tabTitle)
    })
  })()

  
  /*
   * Make Social Button Objects
   */

  const  mkBtns = (tabUrl = '', tabTitle = '') => {

    // Set post items

    const diasporaPost = `/bookmarklet?url=${tabUrl}&title=${tabTitle}&jump-doclose`
    const friendicaPost = `/bookmarklet?url=${tabUrl}&title=${tabTitle}&jump-doclose`
    const gnusocialPost = `/?action=newnotice&status_textarea=${tabTitle} ${tabUrl}`
    const hubzillaPost = `/rpost?body=${tabTitle} &url=${tabUrl}`
    const lemmyPost = `/create_post?url=${tabUrl}&title=${tabTitle}`
    const mastodonPost = `/share?text=${tabTitle}&url=${tabUrl}`
    const pleromaPost = `/share?message=${tabTitle} ${tabUrl}`
    const socialhomePost = `/bookmarklet?url=${tabUrl}&title=${tabTitle}&jump-doclose`
    const xmppPost = `?message=${tabTitle} ${tabUrl}`

    // Set social Buttons

    const socialBtns = []

    const diaspora = {}
    const friendica = {}
    const gnusocial = {}
    const hubzilla = {}
    const lemmy = {}
    const mastodon = {}
    const pleroma = {}
    const socialhome = {}
    const xmpp = {}

    const itemBtn = (item, hostId, hostKey, hostDefault, hostPost) => {
      let itemHost = browser.storage.sync.get(`${hostKey}`)
      itemHost.then((res) => {
	document.querySelector(`#${hostId}`)
	  .href = [res[hostKey] || hostDefault] + hostPost
      })
      item.href = itemHost
      item.id = hostId
      socialBtns.push(item)
    }

    itemBtn(diaspora, 'url-diaspora', 'diasporaHost', host.diasporaDefault, diasporaPost)
    itemBtn(friendica, 'url-friendica', 'friendicaHost', host.friendicaDefault, friendicaPost)
    itemBtn(gnusocial, 'url-gnusocial', 'gnusocialHost', host.gnusocialDefault, gnusocialPost)
    itemBtn(hubzilla, 'url-hubzilla', 'hubzillaHost', host.hubzillaDefault, hubzillaPost)
    itemBtn(lemmy, 'url-lemmy', 'lemmyHost', host.lemmyDefault, lemmyPost)
    itemBtn(mastodon, 'url-mastodon', 'mastodonHost', host.mastodonDefault, mastodonPost)
    itemBtn(pleroma, 'url-pleroma', 'pleromaHost', host.pleromaDefault, pleromaPost)
    itemBtn(socialhome, 'url-socialhome', 'socialhomeHost', host.socialhomeDefault, socialhomePost)
    itemBtn(xmpp, 'url-xmpp', 'xmppHost', host.xmppDefault, xmppPost)
    
    socialBtns.forEach(urlAssigner)
  }

  
  /*
   * hide/show buttons
   */

  const enableItem = (hostKey, hostId) => {
    const item = chrome.storage.sync.get(`${hostKey}`, value => {
      if (value[hostKey]) {
	document.getElementById(`${hostId}`).removeAttribute('hidden')
      }
    })
  }

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

  
  /*
   * Open options page
   */
  
  function openOptions() {
    browser.runtime.openOptionsPage()
  }

  const el = document.getElementById('options')
  el.addEventListener('click', function() {openOptions()}, false)

  
}
