const PAGE_SOURCE = "nt-workbench";
const HELPER_SOURCE = "nt-chrome-helper";

function sendToPage(type, payload = {}) {
  window.postMessage(
    {
      source: HELPER_SOURCE,
      type,
      ...payload,
    },
    window.location.origin,
  );
}

window.addEventListener("message", (event) => {
  if (
    event.source !== window ||
    event.data?.source !== PAGE_SOURCE
  ) {
    return;
  }

  if (event.data.type === "NT_HELPER_PING") {
    chrome.runtime.sendMessage(
      {
        type: "NT_RUNNER_HEALTH",
      },
      (response) => {
        sendToPage("NT_HELPER_READY", {
          version: chrome.runtime.getManifest().version,
          runnerOnline: Boolean(response?.ok),
          latestData: response?.latestData,
          latestTask: response?.latestTask,
        });
      },
    );
    return;
  }

  if (event.data.type === "NT_START_TASK") {
    chrome.runtime.sendMessage(
      {
        type: "NT_START_TASK",
        payload: event.data.payload,
      },
      (response) => {
        if (chrome.runtime.lastError) {
          sendToPage("NT_TASK_ERROR", {
            error: "Chrome 助手暂时没有响应，请重新加载扩展后再试。",
          });
          return;
        }
        if (response?.error) {
          sendToPage("NT_TASK_ERROR", { error: response.error });
        }
      },
    );
  }

  if (event.data.type === "NT_GET_LATEST_TASK") {
    chrome.runtime.sendMessage(
      {
        type: "NT_GET_LATEST_TASK",
      },
      (response) => {
        if (chrome.runtime.lastError) {
          return;
        }
        if (response?.latestTask) {
          sendToPage("NT_TASK_CREATED", {
            task: response.latestTask,
          });
        }
        if (response?.latestData?.available) {
          sendToPage("NT_HELPER_READY", {
            version: chrome.runtime.getManifest().version,
            runnerOnline: Boolean(response?.ok),
            latestData: response.latestData,
            latestTask: response.latestTask,
          });
        }
      },
    );
    return;
  }

  if (event.data.type === "NT_RECALCULATE_TASK") {
    chrome.runtime.sendMessage(
      {
        type: "NT_RECALCULATE_TASK",
        payload: event.data.payload,
      },
      (response) => {
        if (chrome.runtime.lastError) {
          sendToPage("NT_TASK_ERROR", {
            error: "Chrome 助手暂时没有响应，请重新加载扩展后再试。",
          });
          return;
        }
        if (response?.error) {
          sendToPage("NT_TASK_ERROR", { error: response.error });
        }
      },
    );
  }

  if (event.data.type === "NT_SEARCH_STREAMING_EVENTS") {
    chrome.runtime.sendMessage(
      {
        type: "NT_SEARCH_STREAMING_EVENTS",
        payload: event.data.payload,
      },
      (response) => {
        if (chrome.runtime.lastError) {
          sendToPage("NT_STREAMING_EVENTS_ERROR", {
            error: "Chrome 助手暂时没有响应，请重新加载扩展后再试。",
          });
          return;
        }
        if (response?.error) {
          sendToPage("NT_STREAMING_EVENTS_ERROR", {
            error: response.error,
          });
        }
      },
    );
  }
});

chrome.runtime.onMessage.addListener((message) => {
  if (message?.source !== HELPER_SOURCE) {
    return;
  }
  sendToPage(message.type, message.payload);
});

chrome.runtime.sendMessage(
  {
    type: "NT_RUNNER_HEALTH",
  },
  (response) => {
    sendToPage("NT_HELPER_READY", {
      version: chrome.runtime.getManifest().version,
      runnerOnline: Boolean(response?.ok),
      latestData: response?.latestData,
      latestTask: response?.latestTask,
    });
  },
);
