import React, { useMemo, useState } from "react"
import { DataNavigation } from "@/components/Common/DataNavigation.tsx"
import { Card, Drawer, Skeleton } from "antd"
import { IodRegistryEntry } from "@/types/iod.ts"

type ShowCardProps = {
  loading: boolean
  record: IodRegistryEntry
  truncate?: boolean
}

const ShowCard: React.FC<ShowCardProps> = ({
  loading,
  record,
  truncate = true
}) => (
  <Card className="[&_.ant-card-body]:!p-2 !bg-[gb(248, 248, 248)] border !border-[#e9e9e9]">
    {loading ? (
      <Skeleton title={false} active />
    ) : (
      <div className="flex flex-col gap-0.5">
        <h3
          className={`text-base font-medium  mb-1 text-[#222222] break-all ${truncate ? "line-clamp-2" : ""}`}
          title={record.name}>
          {record.name}
        </h3>
        <p
          className={`text-sm text-[#383838] break-all ${truncate ? "line-clamp-2" : ""}`}
          title={record.doId}>
          数字对象标识：{record.doId}
        </p>
        <p
          className={`text-[#828282] text-xs break-all ${truncate ? "truncate" : ""}`}
          title={record.description}>
          {record.description}
        </p>
      </div>
    )}
  </Card>
)

const defaultData:IodRegistryEntry[] = [
  {
    name: "上海芯飞睿科技有限公司",
    description:
      "上海芯飞睿科技有限公司专业从事集成化激光材料与微型化激光器件的研发",
    doId: "CSTR:15552.13.05.61.2022.783"
  },
  {
    name: "长三角先进材料研究院",
    description: "由江苏省人民政府联合中国科学院、中国钢研科技集团和中国",
    doId: "CSTR:15552.12.01.11.2021.528"
  },
  {
    name: "清华大学智能系统实验室",
    description: "清华大学",
    doId: "CSTR:15552.13.04.91.2021.614",
  },
]

export const PlaygroundTeam = () => {
  const { messages, iodLoading, currentMessageId, iodSearch } = useMessageOption()

  const data = useMemo<IodRegistryEntry[]>(() => {
    // 确保loading状态时数据大于3
    if (iodLoading) {
      return defaultData
    }

    if (messages.length && iodSearch) {
      const currentMessage = messages?.find(
        (message) => message.id === currentMessageId
      )
      return currentMessage?.iodSources.organization.data ?? []
    }

    return defaultData
  }, [currentMessageId, messages, iodLoading])

  const title = useMemo(() => {
    return messages.length > 0 ? "推荐团队" : "热点团队"
  }, [messages])

  const [open, setOpen] = useState(false)

  const showDrawer = () => {
    setOpen(true)
  }

  const onClose = () => {
    setOpen(false)
  }

  return (
    <Card
      className="h-full [&_.ant-card-body]:h-full [&_.ant-card-body]:!p-[20px] overflow-y-hidden"
      hoverable>
      <div className="h-full flex flex-col relative">
        {/* 数据导航 */}
        <DataNavigation
          Header={
            <div className="flex items-center text-[#BE0BAC] gap-1">
              <svg
                className="icon"
                viewBox="0 0 1024 1024"
                version="1.1"
                xmlns="http://www.w3.org/2000/svg"
                p-id="7272"
                width="18"
                height="18">
                <path
                  d="M824.2 699.9c-25.4-25.4-54.7-45.7-86.4-60.4C783.1 602.8 812 546.8 812 484c0-110.8-92.4-201.7-203.2-200-109.1 1.7-197 90.6-197 200 0 62.8 29 118.8 74.2 155.5-31.7 14.7-60.9 34.9-86.4 60.4C345 754.6 314 826.8 312 903.8c-0.1 4.5 3.5 8.2 8 8.2h56c4.3 0 7.9-3.4 8-7.7 1.9-58 25.4-112.3 66.7-153.5C493.8 707.7 551.1 684 612 684c60.9 0 118.2 23.7 161.3 66.8C814.5 792 838 846.3 840 904.3c0.1 4.3 3.7 7.7 8 7.7h56c4.5 0 8.1-3.7 8-8.2-2-77-33-149.2-87.8-203.9zM612 612c-34.2 0-66.4-13.3-90.5-37.5-24.5-24.5-37.9-57.1-37.5-91.8 0.3-32.8 13.4-64.5 36.3-88 24-24.6 56.1-38.3 90.4-38.7 33.9-0.3 66.8 12.9 91 36.6 24.8 24.3 38.4 56.8 38.4 91.4 0 34.2-13.3 66.3-37.5 90.5-24.2 24.2-56.4 37.5-90.6 37.5z"
                  p-id="7273"
                  fill="#BE0BAC"></path>
                <path
                  d="M361.5 510.4c-0.9-8.7-1.4-17.5-1.4-26.4 0-15.9 1.5-31.4 4.3-46.5 0.7-3.6-1.2-7.3-4.5-8.8-13.6-6.1-26.1-14.5-36.9-25.1-25.8-25.2-39.7-59.3-38.7-95.4 0.9-32.1 13.8-62.6 36.3-85.6 24.7-25.3 57.9-39.1 93.2-38.7 31.9 0.3 62.7 12.6 86 34.4 7.9 7.4 14.7 15.6 20.4 24.4 2 3.1 5.9 4.4 9.3 3.2 17.6-6.1 36.2-10.4 55.3-12.4 5.6-0.6 8.8-6.6 6.3-11.6-32.5-64.3-98.9-108.7-175.7-109.9-110.9-1.7-203.3 89.2-203.3 199.9 0 62.8 28.9 118.8 74.2 155.5-31.8 14.7-61.1 35-86.5 60.4-54.8 54.7-85.8 126.9-87.8 204-0.1 4.5 3.5 8.2 8 8.2h56.1c4.3 0 7.9-3.4 8-7.7 1.9-58 25.4-112.3 66.7-153.5 29.4-29.4 65.4-49.8 104.7-59.7 3.9-1 6.5-4.7 6-8.7z"
                  p-id="7274"
                  fill="#BE0BAC"></path>
              </svg>
              {title}
            </div>
          }
          onClick={showDrawer}
        />

        {/* 场景列表 */}
        <div className="grid grid-cols-3 gap-3 flex-1 overflow-y-auto">
          {data.slice(0, 3).map((item, index) => (
            <ShowCard key={item.doId} loading={iodLoading} record={item} />
          ))}
        </div>
      </div>

      {/* 抽屉 */}
      <Drawer
        title={title}
        closable={{ "aria-label": "Close Button" }}
        onClose={onClose}
        open={open}
        width="33.33%">
        <div className="grid grid-cols-1 gap-3 overflow-y-auto">
          {data.map((item, index) => (
            <ShowCard
              key={item.doId}
              loading={iodLoading}
              record={item}
              truncate={false}
            />
          ))}
        </div>
      </Drawer>
    </Card>
  )
}
