// Code injected into linkedin. Code injection defined by manifest.json
// Creates button to summon the extension and iframe to show extension

let isOpen = 0;
const p = document.querySelector("body")
const i = document.createElement('iframe')
const b = document.createElement('button')
const d = document.createElement('div')
d.className = 'fa-container fa-closed'

p.insertAdjacentElement("afterbegin", d)
const buttonImage = document.createElement('img')
buttonImage.className = 'fa-button-image';
b.insertAdjacentElement("afterbegin", buttonImage);

i.className = 'fa-content fa-closed'
b.className = 'fa-button'

chrome.storage.sync.get(["frameURL", "buttonImageURL"], (response) => {
  i.src = response.frameURL;
  buttonImage.src = response.buttonImageURL;
  d.insertAdjacentElement("afterbegin", i)
})

b.addEventListener('click', (evt) => {
  toggleFrame(evt);
})
p.appendChild(b)

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === "sendDetails") {
    console.log("Sending Details!")
    getDetails().then(sendResponse)
    return true;
  }
  else if (message.action === "toggleFrame") {
    toggleFrame();
  }
  else if (message.action === "openFrame" && !isOpen) {
    toggleFrame();
  }
  else if (message.action === "changeIFrameSrc") {
    setIFrameSrc(message.src);
  }
  else if (message.action === "changeWindowURL") {
    setWindowURL(message.url);
  }
});