import { ReactNode, useCallback, useState } from "react";
import {
	UploadOutlined,
	CheckCircleOutlined,
	DeleteOutlined,
} from "@ant-design/icons";
import { Upload, Progress, Button } from "antd";
import { useTusUpload } from "@web/src/hooks/useTusUpload";
import toast from "react-hot-toast";
export interface TusUploaderProps {
	value?: string[];
	onChange?: (value: string[]) => void;
	multiple?: boolean;
	allowTypes?: string[];
	style?:string
	icon?:ReactNode,
	description?:string
}

interface UploadingFile {
	name: string;
	progress: number;
	status: "uploading" | "done" | "error";
	fileId?: string;
	fileKey?: string;
}

export const TusUploader = ({
	value = [],
	onChange,
	multiple = true,
	allowTypes = undefined,
	style="",
	icon = <UploadOutlined />,
	description = "点击或拖拽文件到此区域进行上传",
}: TusUploaderProps) => {
	const { handleFileUpload, uploadProgress } = useTusUpload();
	const [uploadingFiles, setUploadingFiles] = useState<UploadingFile[]>([]);
	const [completedFiles, setCompletedFiles] = useState<UploadingFile[]>(
		() =>
			value?.map((fileId) => ({
				name: `文件 ${fileId}`,
				progress: 100,
				status: "done" as const,
				fileId,
			})) || []
	);
	const [uploadResults, setUploadResults] = useState<string[]>(value || []);
	const handleRemoveFile = useCallback(
		(fileId: string) => {
			setCompletedFiles((prev) =>
				prev.filter((f) => f.fileId !== fileId)
			);
			setUploadResults((prev) => {
				const newValue = prev.filter((id) => id !== fileId);
				onChange?.(newValue);
				return newValue;
			});
		},
		[onChange]
	);

	// 新增：处理删除上传中的失败文件
	const handleRemoveUploadingFile = useCallback((fileKey: string) => {
		setUploadingFiles((prev) => prev.filter((f) => f.fileKey !== fileKey));
	}, []);

	const handleBeforeUpload = useCallback(
		(file: File) => {
			if (allowTypes && !allowTypes.includes(file.type)) {
				toast.error(`文件类型 ${file.type} 不在允许范围内`);
				return Upload.LIST_IGNORE; // 使用 antd 的官方阻止方式
			}
			const fileKey = `${file.name}-${Date.now()}`;

			setUploadingFiles((prev) => [
				...prev,
				{
					name: file.name,
					progress: 0,
					status: "uploading",
					fileKey,
				},
			]);

			handleFileUpload(
				file,
				(result) => {
					console.log(result)
					setCompletedFiles((prev) => [
						...prev,
						{
							name: file.name,
							progress: 100,
							status: "done",
							fileId: result.fileId,
						},
					]);

					setUploadingFiles((prev) =>
						prev.filter((f) => f.fileKey !== fileKey)
					);

					setUploadResults((prev) => {
						// 如果是单文件模式,则替换现有文件
						const newValue = multiple
							? [...prev, result.fileId]
							: [result.fileId];
						onChange?.(newValue);
						return newValue;
					});

					// 单文件模式下,清除之前的完成文件
					if (!multiple) {
						setCompletedFiles([
							{
								name: file.name,
								progress: 100,
								status: "done",
								fileId: result.fileId,
							},
						]);
					}
				},
				(error) => {
					console.error("上传错误:", error);
					toast.error(
						`上传失败: ${error instanceof Error ? error.message : "未知错误"}`
					);
					setUploadingFiles((prev) =>
						prev.map((f) =>
							f.fileKey === fileKey
								? { ...f, status: "error" }
								: f
						)
					);
				},
				fileKey
			);

			return false;
		},
		[handleFileUpload, onChange]
	);

	return (
		<div className={`space-y-1 ${style}`}>
			<Upload.Dragger
				accept={allowTypes?.join(",")}
				name="files"
				multiple={multiple}
				showUploadList={false}
				beforeUpload={handleBeforeUpload}>
				<p className="ant-upload-drag-icon">
					{icon}
				</p>
				<p className="ant-upload-text">
					{description}
				</p>
				<p className="ant-upload-hint">
					{multiple ? "支持单个或批量上传文件" : "仅支持上传单个文件"}
					{/* {allowTypes && (
						<span className="block text-xs text-gray-500">
							允许类型: {allowTypes.join(", ")}
						</span>
					)} */}
				</p>

				<div className="px-2 py-0 rounded mt-1">
					{uploadingFiles.map((file) => (
						<div
							key={file.fileKey}
							className="flex flex-col gap-1 mb-2">
							<div className="flex items-center gap-2">
								<span className="text-sm">{file.name}</span>
							</div>
							<div className="flex items-center gap-2">
								<Progress
									percent={
										file.status === "done"
											? 100
											: Math.round(
													uploadProgress?.[
														file.fileKey!
													] || 0
												)
									}
									status={
										file.status === "error"
											? "exception"
											: "active"
									}
									className="flex-1"
								/>
								{file.status === "error" && (
									<Button
										type="text"
										danger
										icon={<DeleteOutlined />}
										onClick={(e) => {
											e.stopPropagation();
											if (file.fileKey)
												handleRemoveUploadingFile(
													file.fileKey
												);
										}}
									/>
								)}
							</div>
						</div>
					))}

					{completedFiles.map((file) => (
						<div
							key={file.fileId}
							className="flex items-center justify-between gap-2 mb-2">
							<div className="flex items-center gap-2">
								<CheckCircleOutlined className="text-green-500" />
								<span className="text-sm">{file.name}</span>
							</div>
							<Button
								type="text"
								danger
								icon={<DeleteOutlined />}
								onClick={(e) => {
									e.stopPropagation();
									if (file.fileId)
										handleRemoveFile(file.fileId);
								}}
							/>
						</div>
					))}
				</div>
			</Upload.Dragger>
		</div>
	);
};
