import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import rehypeRaw from "rehype-raw";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism";
import { useMemo, useState } from "react";
import "./index.less";

interface MarkdownViewerProps {
  content: string;
}

const CodeHeader: React.FC<{
  language: string;
  onCopy: () => void;
  copied: boolean;
}> = ({ language, onCopy, copied }) => (
  <div
    className="flex justify-between items-center text-white"
    style={{ background: "#afadad", padding: "3px 4px" }}
  >
    <div className="flex items-center">
      <span className="text-xs text-#fff">{language}</span>
    </div>

    <div>
      <span className="cursor-pointer text-xs" onClick={onCopy}>
        {copied ? "已复制!" : "复制代码"}
      </span>
    </div>
  </div>
);

const MarkdownViewer: React.FC<MarkdownViewerProps> = ({ content }) => {
  const [copiedIndex, setCopiedIndex] = useState<number | null>(null);
  const filterContent = useMemo(() => {
    return content
      .replaceAll("<think>", '<div class="think">')
      .replaceAll("</think>", "</div>");
  }, [content]);

  const handleCopy = (code: string, index: number) => {
    navigator.clipboard.writeText(code);
    setCopiedIndex(index);
    setTimeout(() => setCopiedIndex(null), 2000);
  };

  return (
    <div className="markdown-body">
      <ReactMarkdown
        remarkPlugins={[remarkGfm]}
        rehypePlugins={[rehypeRaw]}
        components={{
          code({ node, className, children, ...props }) {
            const match = /language-(\w+)/.exec(className || "");
            const code = String(children).replace(/\n$/, "");
            const language = match ? match[1] : "";
            if (match) {
              return (
                <div className="rounded-md overflow-hidden mb-4">
                  <CodeHeader
                    language={language}
                    onCopy={() =>
                      handleCopy(code, node?.position?.start.line ?? 0)
                    }
                    copied={copiedIndex === node?.position?.start.line}
                  />
                  <div className="max-w-full overflow-x-auto">
                    <SyntaxHighlighter
                      style={vscDarkPlus}
                      language={language}
                      PreTag="div"
                      {...props}
                      customStyle={{
                        margin: 0,
                        borderTopLeftRadius: 0,
                        borderTopRightRadius: 0,
                      }}
                    >
                      {code}
                    </SyntaxHighlighter>
                  </div>
                </div>
              );
            }
            return (
              <code className={className} {...props}>
                {children}
              </code>
            );
          },
        }}
      >
        {filterContent}
      </ReactMarkdown>
    </div>
  );
};

export default MarkdownViewer;
