import { getOllamaURL, isOllamaRunning } from "../services/ollama"
import { browser } from "wxt/browser"
import { clearBadge, streamDownload } from "@/utils/pull-ollama"

export default defineBackground({
  main() {
    let isCopilotRunning: boolean = false
    browser.runtime.onMessage.addListener(async (message,sender,sendResponse) => {
      switch(message.type){
        case "sidepanel":
          await browser.sidebarAction.open()
          break;
        case "pull_model":
          const ollamaURL = await getOllamaURL()

          const isRunning = await isOllamaRunning()
  
          if (!isRunning) {
            setBadgeText({ text: "E" })
            setBadgeBackgroundColor({ color: "#FF0000" })
            setTitle({ title: "Ollama is not running" })
            setTimeout(() => {
              clearBadge()
            }, 5000)
          }
          await streamDownload(ollamaURL, message.modelName)
          break;
          case "retrieveDeepScript":
          return retrieveDeepScript(message);
        default:
          break;
      }
    })

    browser.runtime.onConnect.addListener((port) => {
      if (port.name === "pgCopilot") {
        isCopilotRunning = true
        port.onDisconnect.addListener(() => {
          isCopilotRunning = false
        })
      }
    })

    chrome.action.onClicked.addListener((tab) => {
      chrome.tabs.create({ url: chrome.runtime.getURL("/options.html") })
    })

    const contextMenuTitle = {
      webUi: browser.i18n.getMessage("openOptionToChat"),
      sidePanel: browser.i18n.getMessage("openSidePanelToChat")
    }

    const contextMenuId = {
      webUi: "open-web-ui-pa",
      sidePanel: "open-side-panel-pa"
    }

    browser.contextMenus.create({
      id: contextMenuId["sidePanel"],
      title: contextMenuTitle["sidePanel"],
      contexts: ["page", "selection"]
    })

    browser.contextMenus.create({
      id: "summarize-pa",
      title: browser.i18n.getMessage("contextSummarize"),
      contexts: ["selection"]
    })

    browser.contextMenus.create({
      id: "explain-pa",
      title: browser.i18n.getMessage("contextExplain"),
      contexts: ["selection"]
    })

    browser.contextMenus.create({
      id: "rephrase-pa",
      title: browser.i18n.getMessage("contextRephrase"),
      contexts: ["selection"]
    })

    browser.contextMenus.create({
      id: "translate-pg",
      title: browser.i18n.getMessage("contextTranslate"),
      contexts: ["selection"]
    })

    browser.contextMenus.create({
      id: "custom-pg",
      title: browser.i18n.getMessage("contextCustom"),
      contexts: ["selection"]
    })

    browser.contextMenus.onClicked.addListener(async (info, tab) => {
      if (info.menuItemId === "open-side-panel-pa") {
        chrome.sidePanel.open({
          tabId: tab.id!
        })
      } else if (info.menuItemId === "open-web-ui-pa") {
        browser.tabs.create({
          url: browser.runtime.getURL("/options.html")
        })
      } else if (info.menuItemId === "summarize-pa") {
        chrome.sidePanel.open({
          tabId: tab.id!
        })
        // this is a bad method hope somone can fix it :)
        setTimeout(async () => {
          await browser.runtime.sendMessage({
            from: "background",
            type: "summary",
            text: info.selectionText
          })
        }, isCopilotRunning ? 0 : 5000)

      } else if (info.menuItemId === "rephrase-pa") {
        chrome.sidePanel.open({
          tabId: tab.id!
        })
        setTimeout(async () => {

          await browser.runtime.sendMessage({
            type: "rephrase",
            from: "background",
            text: info.selectionText
          })
        }, isCopilotRunning ? 0 : 5000)

      } else if (info.menuItemId === "translate-pg") {
        chrome.sidePanel.open({
          tabId: tab.id!
        })

        setTimeout(async () => {
          await browser.runtime.sendMessage({
            type: "translate",
            from: "background",
            text: info.selectionText
          })
        }, isCopilotRunning ? 0 : 5000)
      } else if (info.menuItemId === "explain-pa") {
        chrome.sidePanel.open({
          tabId: tab.id!
        })

        setTimeout(async () => {
          await browser.runtime.sendMessage({
            type: "explain",
            from: "background",
            text: info.selectionText
          })
        }, isCopilotRunning ? 0 : 5000)
      } else if (info.menuItemId === "custom-pg") {
        chrome.sidePanel.open({
          tabId: tab.id!
        })

        setTimeout(async () => {
          await browser.runtime.sendMessage({
            type: "custom",
            from: "background",
            text: info.selectionText
          })
        }, isCopilotRunning ? 0 : 5000)
      }
    })

    browser.commands.onCommand.addListener((command) => {
      switch (command) {
        case "execute_side_panel":
          chrome.tabs.query(
            { active: true, currentWindow: true },
            async (tabs) => {
              const tab = tabs[0]
              chrome.sidePanel.open({
                tabId: tab.id!
              })
            }
          )
          break
        default:
          break
      }
    })

  },
  persistent: true
})

const iodConfig = {
  "gatewayUrl": "tcp://127.0.0.1:21051",
  "registry":"bdware/Registry",
  "localRepository":"bdtest.local/myrepo1",
  "doBrowser":"http://127.0.0.1:21030/SCIDE/SCManager"
}



const makeDOIPParams = (doId:string, op:string, attributes:Object, requestBody: string) => ({
action: "executeContract",
contractID: "BDBrowser",
operation: "sendRequestDirectly",
arg: {
  id: doId,
  doipUrl: iodConfig.gatewayUrl,
  op: op,
  attributes: attributes,
  body: requestBody
}
}) 
const retrieveDeepScript = async function(message) {
console.log(message);
const doId = message.doId;
console.log("retriveDoc:"+doId)
const params = makeDOIPParams(doId,"Retrieve",{
  bodyBase64Encoded: false
}, "");
const abortController = new AbortController()
setTimeout(() => abortController.abort(), 10000)
return await fetch(iodConfig.doBrowser, {
    method: "POST",
    body: JSON.stringify(params),
    signal: abortController.signal
}).then((response) => {
  console.log("responseIn retrieveDoc:");
  console.log(response);
  return response.json()})
.then((res) => {
     console.log("res:");
     console.log(res.result.body);
     //TODO
    return {
      metadata:{traceId:res.result.header.attributes?.traceId},
      pageContent:res.result.body
    }
})
}