import type {
  PlasmoCSConfig,
  PlasmoCSUIProps,
  PlasmoGetOverlayAnchor,
  PlasmoGetShadowHostId,
  PlasmoGetStyle
} from "plasmo"
import React, { useEffect, useRef, type FC } from "react"
import { v4 as uuidv4 } from "uuid"

import { useMessage } from "@plasmohq/messaging/hook"
import { useStorage } from "@plasmohq/storage/hook"

import TagBtnStyle from "~component/tagBtn/style"
import Tags from "~component/ui/tags"
import ToolBox from "~component/ui/toolBox"
import { addCss, i18n, saveHtml, saveMarkdown, saveWord } from "~tools"
import useCssCodeHook from "~utils/cssCodeHook"
import { useEditMarkdown } from "~utils/editMarkdownHook"
import { useParseMarkdown } from "~utils/parseMarkdownHook"
import { Print } from "~utils/print"
import Turndown from "~utils/turndown"

export const config: PlasmoCSConfig = {
  matches: ["https://*.jianshu.com/*"]
}

const turndownService = Turndown()
const articleTitle = document
  .querySelector<HTMLElement>("head title")
  .innerText.trim()

const HOST_ID = "codebox-jianshu"
export const getShadowHostId: PlasmoGetShadowHostId = () => HOST_ID

export const getOverlayAnchor: PlasmoGetOverlayAnchor = async () =>
  document.querySelector("section.ouvJEz")

export const getStyle: PlasmoGetStyle = () => TagBtnStyle()

const PlasmoOverlay: FC<PlasmoCSUIProps> = ({ anchor }) => {
  const [parseContent, setParseContent] = useParseMarkdown()
  const [allShowTag, setAllShowTag] = useStorage("config-allShowTag", true)
  const [showTag, setShowTag] = useStorage<boolean>("jianshu-showTag", true)
  const [cssCode, runCss] = useCssCodeHook("jianshu")
  const [closeLoginModal] = useStorage<boolean>("jianshu-closeLoginModal")
  const [copyCode] = useStorage<boolean>("jianshu-copyCode")
  const [autoOpenCode] = useStorage<boolean>("jianshu-autoOpenCode")
  const [history, setHistory] = useStorage<any[]>("codebox-history")
  const [closeLog] = useStorage("config-closeLog", true)
  const [content, setContent] = useEditMarkdown()

  useEffect(() => {
    closeLog ||
      console.log("jianshu status", { copyCode, closeLoginModal, autoOpenCode })
    copyCode && copyCodeFunc()
    closeLoginModal && closeLoginModalFunc()
    autoOpenCode && autoOpenCodeFunc()
  }, [copyCode, closeLoginModal, autoOpenCode])

  useMessage(async (req, res) => {
    if (req.name == "jianshu-isShow") {
      res.send({ isShow: true })
    }
    if (req.name == "jianshu-editMarkdown") {
      editMarkdown()
    }
    if (req.name == "jianshu-downloadMarkdown") {
      downloadMarkdown()
    }
    if (req.name == "jianshu-downloadHtml") {
      downloadHtml()
    }
    if (req.name == "jianshu-downloadPdf") {
      downloadPdf()
    }
  })

  // 一键复制
  function copyCodeFunc() {
    const codes = document.querySelectorAll<HTMLElement>(".hljs")

    codes.forEach((code) => {
      const button = document.createElement("button")
      button.innerText = i18n("copy")
      button.style.position = "absolute"
      button.style.top = "0"
      button.style.right = "0"
      button.style.background = "#fff"
      button.title = i18n("copyCode")
      button.classList.add("Button")
      button.classList.add("VoteButton")

      code.appendChild(button)
      code.style.position = "relative"

      button.addEventListener("click", (e) => {
        const target = e.target as HTMLElement
        const parentPreBlock = target.closest(".hljs")
        const codeBlock = parentPreBlock.querySelector<HTMLElement>("code")

        navigator.clipboard.writeText(codeBlock.innerText)
        setHistory((prevData) =>
          prevData
            ? [
                {
                  id: uuidv4(),
                  value: codeBlock.innerText,
                  createdAt: new Date(),
                  from: "简书",
                  link: location.href,
                  tags: [],
                  remark: ""
                },
                ...prevData
              ]
            : [
                {
                  id: uuidv4(),
                  value: codeBlock.innerText,
                  createdAt: new Date(),
                  from: "简书",
                  link: location.href,
                  tags: [],
                  remark: ""
                }
              ]
        )

        target.innerText = i18n("copied")
        setTimeout(() => {
          target.innerText = i18n("copy")
        }, 1000)
        e.stopPropagation()
        e.preventDefault()
      })
    })
  }

  // 隐藏登录弹窗
  function closeLoginModalFunc() {
    addCss(
      `.hide,
      ._23ISFX-wrap,
      ._23ISFX-mask{ 
        display:none !important; 
      }
      body {
        overflow: unset !important;
      }`
    )
    let openAppModal = document.querySelector(".open-app-modal")
    if (openAppModal) {
      const dialog = openAppModal.closest(
        "div[class^='dialog-']"
      ) as HTMLElement
      const className = dialog.className
      addCss(
        `.download-app-guidance,.${className} { display:none !important; }`
      )
    }
  }

  // 自动展开全文
  function autoOpenCodeFunc() {
    const tips = document.querySelector(".collapse-tips")
    if (tips) {
      const css = `
        .collapse-free-content{
          height: auto !important;
        }
        .collapse-tips {
          display: none !important;
        }`
      addCss(css)
    }
  }

  function getDescription() {
    const summary = document.querySelector<HTMLMetaElement>(
      'meta[name="description"]'
    ).content
    summary && prompt(i18n("getDescription"), summary)
  }

  function downloadPdf() {
    const article = document.querySelector<HTMLElement>("section.ouvJEz")
    if (article) {
      Print.print(article, { title: articleTitle })
        .then(() => console.log("Printing complete"))
        .catch((error) => console.error("Printing failed:", error))
    }
  }

  function downloadWord() {
    const dom = document.querySelector("section.ouvJEz")
    saveWord(dom, articleTitle)
  }

  function editMarkdown() {
    const dom = document.querySelector("section.ouvJEz")
    setContent(dom, articleTitle)
  }

  function downloadMarkdown() {
    const html = document.querySelector("section.ouvJEz")
    const markdown = turndownService.turndown(html)
    saveMarkdown(markdown, articleTitle)
  }

  function downloadHtml() {
    const dom = document.querySelector("section.ouvJEz")
    saveHtml(dom, articleTitle)
  }

  function parseMarkdown() {
    const dom = document.querySelector("section.ouvJEz")
    setParseContent(dom)
  }

  return showTag && allShowTag ? (
    <ToolBox
      onGetDescription={getDescription}
      onEditMarkdown={editMarkdown}
      onDownloadMarkdown={downloadMarkdown}
      onPrint={downloadPdf}
      onParseMarkdown={parseMarkdown}
      onDownloadWord={downloadWord}
    />
  ) : (
    <></>
  )
}

export default PlasmoOverlay
