import { useIntl } from "react-intl";
import { Dropdown, Tag, Tooltip, Typography, message } from "antd";
import { type ActionType, ProList } from "@ant-design/pro-components";
import { ExportOutlined } from "@ant-design/icons";

import { get } from "../../request";
import { useEffect, useRef, useState } from "react";
import {
  type ICourseDataResponse,
  type ICourseMemberListResponse,
  type ICourseResponse,
  type TCourseMemberAction,
  type TCourseMemberStatus,
  type TCourseRole,
  actionMap,
} from "../../api/course";

import User from "../auth/User";
import { getStatusColor, managerCanDo } from "./RolePower";

import CourseInvite from "./CourseInvite";
import type { IUser } from "../../api/Auth";
import type { IChannel } from "../../api/channel";
import type { ItemType } from "antd/es/menu/interface";

// 1. 引入 hook（删除旧的 ISetStatus 类型导入，改为从 hook 文件导入）
import { useSetStatus, type ISetStatus } from "./hooks/userActionUtils";

interface IParam {
  role?: string;
  status?: string[];
}

interface IRoleTag {
  title: string;
  color: string;
}

export interface ICourseMember {
  sn?: number;
  id?: string;
  userId: string;
  user?: IUser;
  name?: string;
  tag?: IRoleTag[];
  image: string;
  role?: TCourseRole;
  channel?: IChannel;
  startExp?: number;
  endExp?: number;
  currentExp?: number;
  expByDay?: number;
  status?: TCourseMemberStatus;
}

interface IWidget {
  courseId?: string;
  onSelect?: (record: ICourseMember) => void;
}

const CourseMemberListWidget = ({ courseId, onSelect }: IWidget) => {
  const intl = useIntl(); //i18n
  const [canManage, setCanManage] = useState(false);
  const [course, setCourse] = useState<ICourseDataResponse>();
  const ref = useRef<ActionType | null>(null);
  const { Text } = Typography;

  // 2. ✅ 在组件顶层调用 hook
  const { setStatus } = useSetStatus();

  useEffect(() => {
    if (courseId) {
      const url = `/api/v2/course/${courseId}`;
      console.debug("course url", url);
      get<ICourseResponse>(url)
        .then((json) => {
          console.debug("course data", json.data);
          if (json.ok) {
            setCourse(json.data);
          }
        })
        .catch((e) => console.error(e));
    }
  }, [courseId]);

  return (
    <>
      <ProList<ICourseMember, IParam>
        actionRef={ref}
        search={{
          filterType: "light",
        }}
        onItem={(record: ICourseMember) => {
          return {
            onClick: () => {
              // 点击行
              if (typeof onSelect !== "undefined") {
                onSelect(record);
              }
            },
          };
        }}
        columns={[
          {
            listSlot: "title",
            dataIndex: "name",
            search: false,
          },
          {
            listSlot: "avatar",
            search: false,
            render(_dom, entity) {
              return <User {...entity.user} showName={false} />;
            },
            editable: false,
          },
          {
            listSlot: "description",
            dataIndex: "desc",
            search: false,
            render(_dom, entity) {
              return (
                <div>
                  {entity.role === "student" ? (
                    <>
                      {"channel:"}
                      {entity.channel?.name ?? (
                        <Text type="danger">
                          {intl.formatMessage({
                            id: `course.channel.unbound`,
                          })}
                        </Text>
                      )}
                    </>
                  ) : (
                    <></>
                  )}
                </div>
              );
            },
          },
          {
            listSlot: "subTitle",
            search: false,
            render: (_: React.ReactNode, entity: ICourseMember) => {
              return (
                <Tag>
                  {intl.formatMessage({
                    id: `auth.role.${entity.role}`,
                  })}
                </Tag>
              );
            },
          },
          {
            listSlot: "actions",
            search: false,
            render: (_text, row, index) => {
              const statusColor = getStatusColor(row.status);
              const actions: TCourseMemberAction[] = [
                "invite",
                "revoke",
                "accept",
                "reject",
                "block",
              ];

              const items: ItemType[] = actions.map((item) => {
                return {
                  key: item,
                  label: intl.formatMessage({
                    id: `course.member.status.${item}.button`,
                  }),
                  disabled: !managerCanDo(
                    item,
                    course?.start_at,
                    course?.end_at,
                    course?.join,
                    row.status,
                    course?.sign_up_start_at,
                    course?.sign_up_end_at
                  ),
                };
              });

              return [
                <span style={{ color: statusColor }}>
                  {intl.formatMessage({
                    id: `course.member.status.${row.status}.label`,
                  })}
                </span>,
                canManage ? (
                  <Dropdown.Button
                    key={index}
                    type="link"
                    menu={{
                      items,
                      onClick: (e) => {
                        console.debug("click", e);
                        const currAction = e.key as TCourseMemberAction;
                        if (actions.includes(currAction)) {
                          const newStatus = actionMap(currAction);
                          if (newStatus) {
                            const actionParam: ISetStatus = {
                              courseMemberId: row.id,
                              message: intl.formatMessage(
                                {
                                  id: `course.member.status.${currAction}.message`,
                                },
                                { user: row.user?.nickName }
                              ),
                              status: newStatus,
                              onSuccess: () => {
                                message.success(
                                  intl.formatMessage({ id: "flashes.success" })
                                );
                                ref.current?.reload();
                              },
                            };
                            setStatus(actionParam);
                          }
                        }
                      },
                    }}
                  >
                    <></>
                  </Dropdown.Button>
                ) : (
                  <></>
                ),
              ];
            },
          },
          {
            // 自己扩展的字段，主要用于筛选，不在列表中显示
            listSlot: "status",
            title: "状态",
            valueType: "checkbox",
            valueEnum: {
              joined: {
                text: intl.formatMessage({
                  id: "course.member.status.joined.label",
                }),
                status: "Default",
              },
              applied: {
                text: intl.formatMessage({
                  id: "course.member.status.applied.label",
                }),
                status: "Success",
              },
              invited: {
                text: intl.formatMessage({
                  id: "course.member.status.invited.label",
                }),
                status: "Success",
              },
              canceled: {
                text: intl.formatMessage({
                  id: "course.member.status.canceled.label",
                }),
                status: "Success",
              },
              revoked: {
                text: intl.formatMessage({
                  id: "course.member.status.revoked.label",
                }),
                status: "Success",
              },
              agreed: {
                text: intl.formatMessage({
                  id: "course.member.status.agreed.label",
                }),
                status: "Success",
              },
              accepted: {
                text: intl.formatMessage({
                  id: "course.member.status.accepted.label",
                }),
                status: "Success",
              },
              disagreed: {
                text: intl.formatMessage({
                  id: "course.member.status.disagreed.label",
                }),
                status: "Success",
              },
              rejected: {
                text: intl.formatMessage({
                  id: "course.member.status.rejected.label",
                }),
                status: "Success",
              },
              left: {
                text: intl.formatMessage({
                  id: "course.member.status.left.label",
                }),
                status: "Success",
              },
              blocked: {
                text: intl.formatMessage({
                  id: "course.member.status.blocked.label",
                }),
                status: "Success",
              },
            },
          },
          {
            listSlot: "role",
            // 自己扩展的字段，主要用于筛选，不在列表中显示
            title: "角色",
            valueType: "select",
            valueEnum: {
              student: {
                text: intl.formatMessage({
                  id: "auth.role.student",
                }),
                status: "Default",
              },
              manager: {
                text: intl.formatMessage({
                  id: "auth.role.manager",
                }),
                status: "Success",
              },
              assistant: {
                text: intl.formatMessage({
                  id: "auth.role.assistant",
                }),
                status: "Success",
              },
            },
          },
        ]}
        request={async (params = {}, sorter, filter) => {
          console.log(params, sorter, filter);

          let url = `/api/v2/course-member?view=course&id=${courseId}`;
          const offset =
            ((params.current ? params.current : 1) - 1) *
            (params.pageSize ? params.pageSize : 20);
          url += `&limit=${params.pageSize}&offset=${offset}`;
          if (
            typeof params.keyword !== "undefined" &&
            params.keyword.trim() !== ""
          ) {
            url += "&search=" + params.keyword;
          }
          if (params.role) {
            url += `&role=${params.role}`;
          }
          if (params.status) {
            url += `&status=${params.status}`;
          }
          console.info("api request", url);
          const res = await get<ICourseMemberListResponse>(url);
          if (res.ok) {
            console.debug("api response", res.data);
            if (res.data.role === "owner" || res.data.role === "manager") {
              setCanManage(true);
            }
            const items: ICourseMember[] = res.data.rows.map((item, id) => {
              const member: ICourseMember = {
                sn: id + 1,
                id: item.id,
                userId: item.user_id,
                user: item.user,
                name: item.user?.nickName,
                role: item.role,
                status: item.status,
                channel: item.channel,
                tag: [],
                image: "",
              };

              return member;
            });
            console.log(items);
            return {
              total: res.data.count,
              succcess: true,
              data: items,
            };
          } else {
            console.error(res.message);
            return {
              total: 0,
              succcess: false,
              data: [],
            };
          }
        }}
        rowKey="id"
        bordered
        pagination={{
          showQuickJumper: true,
          showSizeChanger: true,
        }}
        options={{
          search: true,
        }}
        toolBarRender={() => [
          <CourseInvite
            courseId={courseId}
            onCreated={() => {
              ref.current?.reload();
            }}
          />,
          <Tooltip title="导出成员列表">
            <a
              href={`${import.meta.env.BASE_URL}/api/v2/course-member-export?course_id=${courseId}`}
              target="_blank"
              key="export"
              rel="noreferrer"
            >
              <ExportOutlined />
            </a>
          </Tooltip>,
        ]}
      />
    </>
  );
};

export default CourseMemberListWidget;
