
const promptPopupHTML = 
  `<span id="top-status" class="popup-status top">Remove this profile from queue?</span>
  <div class="prompt-button-wrapper">
    <button id="cancel" class="button prompt button_line">Cancel</button>
    <button id="delete" class="button prompt">Remove</button>
  </div>`
function getPopupHTML(text){
  return `<span id="top-status" class="popup-status top">${text}</span>
  <div class="loader-section"><div class="loader"></div></div>`;
}
function navigate(topUrl){
  const curUrl = window.location.toString();
  if(topUrl.includes("www.linkedin.com/sales/lead")){
    if(!curUrl.includes("current.html"))
      window.location.replace('current.html');
  }
  else if(topUrl.includes("www.linkedin.com/sales/company")){
    if(!curUrl.includes("company.html"))
      window.location.replace('company.html')
  }
  else if(!curUrl.includes("splash.html")){
    window.location.replace('splash.html')
  }
}

async function toggleFrame(){
  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  chrome.tabs.sendMessage(tab.id, { action: "toggleFrame" })
}
async function openFrame(){
  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  chrome.tabs.sendMessage(tab.id, { action: "openFrame" })

}
async function changeWindowURL(url){
  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  chrome.tabs.sendMessage(tab.id, { action: "changeWindowURL",url})
}
function initCloseButton(){
  let closeButton = document.getElementById("close-button");
  closeButton.addEventListener("click", toggleFrame);
}
function createLinkButton(parent,url, sameTab,text, buttonAction=()=>{}){
  const centeredDiv = document.createElement('div')
  centeredDiv.classList = "centeredSection";
  centeredDiv.id = "link-div"
  const button = document.createElement('button')
  button.classList = "button button_line";
  button.innerHTML = text

  centeredDiv.insertAdjacentElement('afterbegin', button)
  parent.insertAdjacentElement('beforeend', centeredDiv);
  if(sameTab){
    button.addEventListener("click", async () => {
      await changeWindowURL(url)
      buttonAction();
    });

  }
  else{
    button.addEventListener("click", () => {
      window.open(url, '_blank').focus();
      buttonAction();
    })

  }

  return centeredDiv;

}


function setOverlay(type, confirmFunc = null, cancelFunc = null){
  const overlay = document.getElementById("overlay")
  const popup = document.getElementById("popup")
  if(!overlay.classList.contains('closed')){
    return;
  }
  overlay.classList.remove('closed')
  popup.classList.remove('closed')
  if(type === "sending"){
    popup.insertAdjacentHTML('afterbegin',getPopupHTML('Sending to FreeAgent...'));
  }
  if(type === "loading"){
    popup.insertAdjacentHTML('afterbegin',getPopupHTML('Loading data...'))
  }
  if(type === "delete"){
    popup.insertAdjacentHTML('afterbegin', promptPopupHTML);
    const deleteButton = document.getElementById('delete')
    deleteButton.addEventListener('click',confirmFunc);
    const cancelButton = document.getElementById('cancel')
    cancelButton.addEventListener('click',cancelFunc);
  }
}
function removeOverlay(){
  const overlay = document.getElementById("overlay")
  const popup = document.getElementById("popup")
  popup.innerHTML = "";
  overlay.classList.add('closed')
  popup.classList.add('closed')
}