/*
 * @Date: 2021-01-05 14:24:23
 * @LastEditors: wangqiang@feewee.cn
 * @LastEditTime: 2023-02-11 16:54:22
 */
import React, { useEffect, useRef, useState } from "react";
import { message, Popover, Spin, Upload } from "antd";
import {
  LinkOutlined,
  EyeOutlined,
  DeleteOutlined,
  FileTwoTone,
  LoadingOutlined,
} from "@ant-design/icons";
import { UploadProps } from "antd/lib/upload";
import {
  RcFile,
  UploadChangeParam,
  UploadFile,
} from "antd/lib/upload/interface";
import getFidFile, { FeeweeFileAccept } from "@/utils/getFidFile";
import COS from "@/utils/COS";
import {
  getFileListDetailApi,
  confirmThirdFileApi,
  deleteFileApi,
} from "./api";
import "./index.less";
import { IMGURL } from "@/utils";

// @ts-ignore
interface Props extends UploadProps {
  /**
   * @description: 附件fid列表。
   * @description: 如果在表单中使用，则需要再Form.Item 中声明valuePropName="fidList"
   */
  fidList?: string[];
  onChange?: (fidList: string[]) => void;
  /** 使用腾讯云对象存储 */
  useCos?: boolean;
  /** 上传腾讯云-前缀文件夹名称 */
  catalog?: string;
  /** 不允许修改文件项列表 fid 数组[] */
  freezeItems?: string[];
  limitSize?: number; // 限制文件大小
  limitUnit?: "kb" | "mb" | "KB" | "MB"; // 限制大小单位, 默认MB
  children?: React.ReactNode;
}

/**
 * @description: 自定义封装的 FeeweeUploadAttachment 上传附件组件
 * @description: 使用方式：
 * 1. 仅需要 传入 fidList 和 onChange 参数就可以使用基本用法；
 * 2. 其他的参数同 Upload 组件的参数
 * 3. useCos catalog freezeItems 同 APP 组件作用一样；
 * 4. limitSize 和 limitUnit 是为了限制上传附件的大小
 * @param {Props} props
 */
export default function FeeweeUploadAttachment({
  useCos,
  catalog = "",
  limitSize = undefined,
  limitUnit = "MB",
  children,
  freezeItems = [],
  fidList,
  onChange,
  ...props
}: Props) {
  const [loading, setLoading] = useState(false);
  const [fileList, setFileList] = useState<UploadFile[]>([]);
  const removingFileFidList = useRef<string[]>([]);

  useEffect(() => {
    if (fidList?.length) {
      setLoading(true);
      // const hide = message.loading("", 0);
      getFileListDetailApi(fidList)
        .then((res) => {
          setFileList(
            (res.data || []).map((item) => ({
              name: item.aliasName || item.fileName || "",
              uid: item.fid || "",
              url: item.fileUri,
              status: "done",
              size: item.length,
              type: item.contentType,
              response: { data: item.fileUri, fid: item.fid },
            }))
          );
        })
        .catch((error: any) => { })
        .finally(() => {
          // hide();
          setLoading(false);
        });
    }
  }, [fidList]);

  if (!props.listType) {
    props.listType = "text";
  }
  if (useCos) {
    props.customRequest = (data) => COS.cosUpload({ ...data, uploadPath: "fw_fds_file/" + catalog });
  } else {
    props.action = "api/file/upload";
  }

  const onPreview = (file: UploadFile) => {
    if (props.onPreview) {
      props.onPreview(file);
      return;
    }
    getFidFile(file?.response.data);
  };

  const beforeUpload = (file: RcFile, filelist: RcFile[]) => {
    if (limitSize !== undefined && typeof limitSize === "number") {
      const limit = isExceededSize(file, limitSize, limitUnit);
      if (limit.success) {
        return true;
      } else {
        message.error(limit.msg);
        // @ts-ignore
        file.status = "size_limit";
        // @ts-ignore
        file.errMsg = limit.msg;
        return false;
      }
    }
    return props.beforeUpload ? props.beforeUpload(file, filelist) : true;
  };

  const allFileDone = (fileList: UploadFile<any>[]) => (fileList || []).every((file) => file?.status !== "uploading");

  const onUploadChange = async ({
    file,
    fileList,
  }: UploadChangeParam<UploadFile>) => {
    setFileList(
      fileList.map((_file) => ({
        ..._file,
        // @ts-ignore
        status: _file?.status === "size_limit" ? "error" : _file?.status,
      }))
    );
    if (file?.status === "done") {
      if (useCos) {
        try {
          const ext_array = file?.name.split(".");
          const { data } = await confirmThirdFileApi({
            fileName: file?.name,
            ext: "." + ext_array[ext_array.length - 1],
            contentType: file?.type,
            fileUri: file?.response.data,
            length: file?.size,
          });
          file.response.fid = data;
          if (allFileDone(fileList)) {
            onChange &&
              onChange(
                fileList
                  .filter(
                    (file) => file?.status === "done" && file?.response.fid
                  )
                  .map((file) => file?.response.fid)
              );
          }
        } catch (error: any) {
          message.error(error.message || "确认文件失败", 2);
        }
      } else {
        file.response.fid = file?.response.data;
        if (allFileDone(fileList)) {
          onChange &&
            onChange(
              fileList
                .filter((file) => file?.status === "done" && file?.response.fid)
                .map((file) => file?.response.fid)
            );
        }
      }
    }
  };

  const onFileRemove = async (file: UploadFile) => {
    if (removingFileFidList.current.includes(file?.response?.fid)) return false;

    if (file.status === "error" || file?.status === "uploading") {
      setFileList(fileList.filter((_file) => _file.uid !== file.uid));
      return true;
    }

    const hide = message.loading("删除中...", 0);
    try {
      removingFileFidList.current.push(file?.response.fid);
      setLoading(true);
      // await deleteFileApi(file?.response.fid);
      // if (useCos) {
      //   await deleteCOSFile(file?.response.data);
      // }
      onChange &&
        onChange((fidList || []).filter((fid) => fid !== file?.response.fid));
      return Promise.resolve();
    } catch (error: any) {
      message.error(error.message || "删除文件失败", 2);
      return Promise.reject();
    } finally {
      removingFileFidList.current = removingFileFidList.current.filter(
        (fid) => fid !== file?.response.fid
      );
      setLoading(false);
      hide();
    }
  };

  return (
    <Spin spinning={loading}>
      <Upload
        accept={FeeweeFileAccept}
        beforeUpload={beforeUpload}
        {...props}
        onPreview={onPreview}
        fileList={fileList}
        onChange={onUploadChange}
        onRemove={onFileRemove}
        itemRender={(originalNode, file, fileList, { remove }) => {
          const DOM =
            props.listType === "picture-card"
              ? ListTypeOfPictureCard
              : props.listType === "picture"
                ? ListTypeOfPicture
                : ListTypeOfText;
          return (
            <DOM
              file={file}
              freezeItems={freezeItems}
              remove={remove}
              disabled={props.disabled}
            />
          );
          // return (
          //   <div
          //     id="FeeweeUploadAttachment"
          //     className={
          //       freezeItems.includes(file?.response?.fid)
          //         ? "FeeweeUploadAttachment_hidden_delete_icon"
          //         : ""
          //     }
          //   >
          //     {/* @ts-ignore */}
          //     {originalNode}
          //   </div>
          // );
        }}
      >
        {props.disabled ? (
          fileList.length ? null : (
            <span style={{ color: "#999" }}>暂无附件</span>
          )
        ) : (
          children
        )}
      </Upload>
    </Spin>
  );
}

interface ItemRenderProps {
  file: UploadFile;
  freezeItems?: string[];
  remove: () => void;
  disabled?: boolean;
}

const ListTypeOfText = ({
  file,
  freezeItems = [],
  remove,
  disabled,
}: ItemRenderProps) => (
  <div
    id="FeeweeUploadAttachment"
    className={
      freezeItems.includes(file?.response?.fid)
        ? "FeeweeUploadAttachment_hidden_delete_icon"
        : ""
    }
  >
    <div
      className={`ant-upload-list-item ant-upload-list-item-done ${file?.status === "error" ? "ant-upload-list-item-error" : ""
        } ant-upload-list-item-list-type-text`}
    >
      <div className="ant-upload-list-item-info">
        {file?.status === "uploading" ? (
          <span className="ant-upload-span">
            <div className="ant-upload-text-icon">
              <LoadingOutlined />
            </div>
            <span
              className="ant-upload-list-item-name"
              title={`${file?.name}(${file?.size ? getFileSize(file?.size) : ""
                })`}
            >
              {file?.name}({file?.size ? getFileSize(file?.size) : ""})
            </span>
            <span className="ant-upload-list-item-card-actions">
              <button
                title="删除文件"
                type="button"
                onClick={() => remove()}
                className="ant-btn ant-btn-text ant-btn-sm ant-btn-icon-only ant-upload-list-item-card-actions-btn"
              >
                <DeleteOutlined />
              </button>
            </span>
          </span>
        ) : (
          <span className="ant-upload-span">
            <div className="ant-upload-text-icon">
              <LinkOutlined />
            </div>
            <a
              target="_blank"
              rel="noopener noreferrer"
              className="ant-upload-list-item-name"
              title={file?.name}
              href={
                file?.status === "error"
                  ? undefined
                  : IMGURL.showImage(file?.response?.data)
              }
            >
              {file?.name}({file?.size ? getFileSize(file?.size) : ""})
            </a>
            {/* @ts-ignore */}
            {file?.status === "error" && file?.errMsg ? (
              // @ts-ignore
              <span>{file?.errMsg}</span>
            ) : null}
            <span className="ant-upload-list-item-card-actions">
              {!disabled ? (
                <button
                  title="删除文件"
                  type="button"
                  onClick={() => remove()}
                  className="ant-btn ant-btn-text ant-btn-sm ant-btn-icon-only ant-upload-list-item-card-actions-btn"
                >
                  <DeleteOutlined />
                </button>
              ) : null}
            </span>
          </span>
        )}
      </div>
      {file?.status === "uploading" ? (
        <div className="ant-upload-list-item-progress">
          <div className="ant-progress ant-progress-line ant-progress-status-normal ant-progress-default">
            <div className="ant-progress-outer">
              <div className="ant-progress-inner">
                <div
                  className="ant-progress-bg"
                  style={{ width: `${file?.percent || 0}%`, height: 2 }}
                />
              </div>
            </div>
          </div>
        </div>
      ) : null}
    </div>
  </div>
);

const ListTypeOfPictureCard = ({
  file,
  freezeItems = [],
  remove,
  disabled,
}: ItemRenderProps) => (
  <div
    id="FeeweeUploadAttachment"
    className={
      freezeItems.includes(file?.response?.fid)
        ? "FeeweeUploadAttachment_hidden_delete_icon"
        : ""
    }
  >
    <Popover
      placement="topLeft"
      content={
        <div>
          <p>
            文件信息：{file?.name}({file?.size ? getFileSize(file?.size) : ""})
          </p>
          {/* @ts-ignore */}
          {file?.errMsg ? (
            <p>
              {/* @ts-ignore */}
              错误原因：<span style={{ color: "#ff4d4f" }}>{file?.errMsg}</span>
            </p>
          ) : null}
        </div>
      }
    >
      <div
        className={`ant-upload-list-item ant-upload-list-item-done ${file?.status === "error" ? "ant-upload-list-item-error" : ""
          } ant-upload-list-item-list-type-picture-card`}
      >
        <div className="ant-upload-list-item-info">
          {file?.status === "uploading" ? (
            <span className="ant-upload-span">
              <div className="ant-upload-list-item-thumbnail">文件上传中</div>
              <span className="ant-upload-list-item-name" title={file?.name}>
                {file?.name}
              </span>
            </span>
          ) : (
            <span className="ant-upload-span">
              <a
                className="ant-upload-list-item-thumbnail ant-upload-list-item-file"
                href={
                  file?.status === "error"
                    ? undefined
                    : IMGURL.showImage(file?.response?.data)
                }
                target="_blank"
                rel="noopener noreferrer"
              >
                {file?.type?.includes("image") ? (
                  <img
                    src={IMGURL.showImage(
                      file?.thumbUrl || file?.response?.data
                    )}
                    alt={`${file?.name}(${file?.size ? getFileSize(file?.size) : ""
                      })`}
                    className="ant-upload-list-item-image"
                  />
                ) : (
                  <FileTwoTone style={{ fontSize: 26 }} />
                )}
              </a>
              <span
                className="ant-upload-list-item-name"
                style={{
                  display: file?.type?.includes("image") ? "none" : undefined,
                }}
                title={`${file?.name}(${file?.size ? getFileSize(file?.size) : ""
                  })`}
              >
                {file?.name}({file?.size ? getFileSize(file?.size) : ""})
              </span>
            </span>
          )}
        </div>
        {file?.status === "uploading" ? (
          <div className="ant-upload-list-item-progress">
            <div className="ant-progress ant-progress-line ant-progress-status-normal ant-progress-default">
              <div className="ant-progress-outer">
                <div className="ant-progress-inner">
                  <div
                    className="ant-progress-bg"
                    style={{ width: `${file?.percent || 0}%`, height: 2 }}
                  />
                </div>
              </div>
            </div>
          </div>
        ) : (
          <span className="ant-upload-list-item-actions">
            <a
              target="_blank"
              rel="noopener noreferrer"
              title="预览文件"
              href={
                file?.status === "error"
                  ? undefined
                  : IMGURL.showImage(file?.response?.data)
              }
              style={{ display: file?.status === "error" ? "none" : undefined }}
            >
              <EyeOutlined />
            </a>
            {!disabled ? (
              <button
                title="删除文件"
                type="button"
                onClick={() => remove()}
                className="ant-btn ant-btn-text ant-btn-sm ant-btn-icon-only ant-upload-list-item-card-actions-btn"
              >
                <DeleteOutlined />
              </button>
            ) : null}
          </span>
        )}
      </div>
    </Popover>
  </div>
);

const ListTypeOfPicture = ({
  file,
  freezeItems = [],
  remove,
  disabled,
}: ItemRenderProps) => (
  <div
    id="FeeweeUploadAttachment"
    className={
      freezeItems.includes(file?.response?.fid)
        ? "FeeweeUploadAttachment_hidden_delete_icon"
        : ""
    }
  >
    <div
      className={`ant-upload-list-item ant-upload-list-item-done ${file?.status === "error" ? "ant-upload-list-item-error" : ""
        } ${file?.status === "uploading" ? "ant-upload-list-item-uploading" : ""
        } ant-upload-list-item-list-type-picture`}
    >
      <div className="ant-upload-list-item-info">
        {file?.status === "uploading" ? (
          <span className="ant-upload-span">
            <div className="ant-upload-list-item-thumbnail">
              <LoadingOutlined />
            </div>
            <span
              className="ant-upload-list-item-name"
              title={`${file?.name}(${file?.size ? getFileSize(file?.size) : ""
                })`}
            >
              {file?.name}({file?.size ? getFileSize(file?.size) : ""})
            </span>
            <span className="ant-upload-list-item-card-actions picture">
              <button
                title="删除文件"
                type="button"
                onClick={() => remove()}
                className="ant-btn ant-btn-text ant-btn-sm ant-btn-icon-only ant-upload-list-item-card-actions-btn"
              >
                <DeleteOutlined />
              </button>
            </span>
          </span>
        ) : (
          <span className="ant-upload-span">
            <a
              className="ant-upload-list-item-thumbnail"
              href={
                file?.status === "error"
                  ? undefined
                  : IMGURL.showImage(file?.response?.data)
              }
              target="_blank"
              rel="noopener noreferrer"
            >
              {file?.type?.includes("image") ? (
                <img
                  src={IMGURL.showImage(file?.thumbUrl || file?.response?.data)}
                  alt={`${file?.name}(${file?.size ? getFileSize(file?.size) : ""
                    })`}
                  className="ant-upload-list-item-image"
                />
              ) : (
                <FileTwoTone style={{ fontSize: 26 }} />
              )}
            </a>
            <a
              target="_blank"
              rel="noopener noreferrer"
              className="ant-upload-list-item-name"
              title={`${file?.name}(${file?.size ? getFileSize(file?.size) : ""
                })`}
              href={
                file?.status === "error"
                  ? undefined
                  : IMGURL.showImage(file?.response?.data)
              }
            >
              {file?.name}({file?.size ? getFileSize(file?.size) : ""})
            </a>
            {/* @ts-ignore */}
            {file?.status === "error" && file?.errMsg ? (
              // @ts-ignore
              <span>{file?.errMsg}</span>
            ) : null}
            <span className="ant-upload-list-item-card-actions picture">
              {!disabled ? (
                <button
                  title="删除文件"
                  type="button"
                  onClick={() => remove()}
                  className="ant-btn ant-btn-text ant-btn-sm ant-btn-icon-only ant-upload-list-item-card-actions-btn"
                >
                  <DeleteOutlined />
                </button>
              ) : null}
            </span>
          </span>
        )}
      </div>

      {file?.status === "uploading" ? (
        <div className="ant-upload-list-item-progress">
          <div className="ant-progress ant-progress-line ant-progress-status-normal ant-progress-default">
            <div className="ant-progress-outer">
              <div className="ant-progress-inner">
                <div
                  className="ant-progress-bg"
                  style={{ width: `${file?.percent || 0}%`, height: 2 }}
                />
              </div>
            </div>
          </div>
        </div>
      ) : null}
    </div>
  </div>
);

const deleteCOSFile = (filePath: string) => {
  return new Promise((resolve, reject) => {
    COS.cosDelete({ filePath }, resolve, reject);
  });
};

/**
 * @description: 计算限制大小，统一返回限制大小转换为KB
 * @param {number} limitSize
 * @param {'kb' | 'mb' | 'KB' | 'MB'} limitUnit
 * @return {number}
 */
function getLimitSize(
  limitSize: number,
  limitUnit: "kb" | "mb" | "KB" | "MB"
): number {
  if (limitUnit === "kb" || limitUnit === "KB") {
    return limitSize;
  }
  if (limitUnit === "mb" || limitUnit === "MB") {
    return limitSize * 1024;
  }
  return 0;
}

/**
 * @description: 判断是否没超过限制大小
 * @param {RcFile} file
 * @param {number} limitSize
 * @param {*} limitUnit
 * @return {{ success: boolean, msg?: string }}
 */
function isExceededSize(
  file: RcFile,
  limitSize: number,
  limitUnit: "kb" | "mb" | "KB" | "MB"
): { success: boolean; msg?: string } {
  const _limitSize = getLimitSize(limitSize, limitUnit);

  const fileSizeToKB = file?.size / 1024;
  if (fileSizeToKB <= _limitSize) {
    return { success: true };
  } else {
    return {
      success: false,
      msg: `文件【${file?.name}】大小不能超过${limitSize}${limitUnit}`,
    };
  }
}

export function getFileSize(length: number): string {
  let fileSize: string;
  fileSize = (length / 1024).toFixed(2);
  if (+fileSize > 1024) {
    fileSize = (+fileSize / 1024).toFixed(2) + "MB";
  } else {
    fileSize += "KB";
  }

  return fileSize;
}
