import { MediaType } from "expo-image-picker";
import { ReactNode } from "react";
import { StyleProp, ViewStyle } from "react-native";

export interface FileStatus {
    file: File;
    status: 'pending' | 'uploading' | 'success' | 'error';
    progress: number;
    error?: string;
}
export interface MaterialFile {
    id: string;
    file_name: string;
    url: string;
}

export interface OutputVideoFile {
    id: string;
    file_name: string;
    url: string;
}

export interface ManualTask {
    task_id: string;
    user_id: string;
    status: 'Created' | 'Processing' | 'Completed' | 'Failed';
    created_at: string;
    started_at: string;
    completed_at: string;
    failure_reason: string | null;
    template_id: number;
    source_files: MaterialFile[];
    output_video_file?: OutputVideoFile;
}

export interface Size {
    value: number;
    unit: 'B' | 'KB' | 'MB' | 'GB' | 'TB';
}

export interface ContentType {
    value: string;
}

export interface FilePath {
    value: string;
}

export interface Metadata {
    [key: string]: any;
}

export type UploadStatus = 'Pending' | 'Uploading' | 'Completed' | 'Failed';
export type DeletionStatus = 'Active' | 'PendingDeletion' | 'Deleted';

export interface ConfirmUpload {
    file_id: string;
    upload_url: string
    name: string;
    size: Size;
    content_type: ContentType;
    upload_time: string; // ISO date string
    storage_medium: string;
    file_path: FilePath;
    uploader_id: number;
    upload_status: UploadStatus;
    deletion_status: DeletionStatus;
    metadata: Metadata;
}

// 定义 EXIF 数据类型
export type ExifData = {
    GPSLatitude?: number | undefined;
    GPSLongitude?: number | undefined;
    GPSAltitude?: number | undefined;
    DateTimeOriginal?: string | undefined;
    Make?: string | undefined;
    Model?: string | undefined;
    ExposureTime?: number | undefined;
    FNumber?: number | undefined;
    ISOSpeedRatings?: number | undefined;
    FocalLength?: number | undefined;
    [key: string]: any;
};

// 默认的 EXIF 数据结构
export const defaultExifData: ExifData = {
    GPSLatitude: undefined,
    GPSLongitude: undefined,
    GPSAltitude: undefined,
    DateTimeOriginal: undefined,
    Make: undefined,
    Model: undefined,
    ExposureTime: undefined,
    FNumber: undefined,
    ISOSpeedRatings: undefined,
    FocalLength: undefined,
};

// 压缩图片可配置参数
export interface ImagesuploaderProps {
    children?: ReactNode;
    style?: StyleProp<ViewStyle>;
    onPickImage?: (file: File, exifData: ExifData) => void;
    /** 压缩质量，0-1 之间的数字，默认为 0.8 */
    compressQuality?: number;
    /** 最大宽度，图片会被等比例缩放 */
    maxWidth?: number;
    /** 最大高度，图片会被等比例缩放 */
    maxHeight?: number;
    /** 是否保留 EXIF 数据，默认为 true */
    preserveExif?: boolean;
    /** 是否上传原图，默认为 false */
    uploadOriginal?: boolean;
    /** 上传完成回调 */
    onUploadComplete?: UploadCompleteCallback;
    /** 进度 */
    onProgress?: (progress: FileStatus) => void;
    /** 多选单选 默认单选*/
    multipleChoice?: boolean;
    /** 文件类型 默认图片*/
    fileType?: MediaType[];
    /** 是否展示预览 默认展示*/
    showPreview?: boolean;
}

// 定义上传结果类型
export interface UploadResult {
    originalUrl?: string;
    compressedUrl: string;
    file: File;
    exifData: ExifData;
    originalFile: ConfirmUpload;
    compressedFile: ConfirmUpload;
    thumbnail: string;
    thumbnailFile: File;
}

// 定义上传完成回调类型
export type UploadCompleteCallback = (result: FileUploadItem[]) => void;

// 单张图片上传完成回调类型
export type UploadSingleCompleteCallback = (result: FileUploadItem) => void;

// 定义上传 URL 响应类型
export interface UploadUrlResponse {
    expires_in: number;
    file_id: string;
    file_path: string;
    upload_url: string;
}
interface FileSize {
    value: number;
    unit: string;
}

interface FileMetadata {
    originalName: string;
    type: string;
    isCompressed: string;
    fileType: string;
}
interface FileInfo {
    file_id: number;
    name: string;
    size: FileSize;
    content_type: ContentType;
    upload_time: string;
    storage_medium: string;
    file_path: FilePath;
    uploader_id: number;
    upload_status: string;
    deletion_status: string;
    metadata: FileMetadata;
}
export interface FileUploadItem {
    id: string;
    uri: string;
    name: string;
    progress: number;
    status: 'pending' | 'uploading' | 'success' | 'error';
    error?: string;
    previewUrl: string;
    file?: File;
    type: 'image' | 'video';
    thumbnail?: string;  // 缩略图URL
    thumbnailFile?: File; // 缩略图文件对象
    originalFile?: FileInfo
}

// 压缩图片可配置参数
export interface ImagesPickerProps {
    children?: ReactNode;
    style?: StyleProp<ViewStyle>;
    onPickImage?: (file: File, exifData: ExifData) => void;
    /** 压缩质量，0-1 之间的数字，默认为 0.8 */
    compressQuality?: number;
    /** 最大宽度，图片会被等比例缩放 */
    maxWidth?: number;
    /** 最大高度，图片会被等比例缩放 */
    maxHeight?: number;
    /** 是否保留 EXIF 数据，默认为 true */
    preserveExif?: boolean;
    /** 是否上传原图，默认为 false */
    uploadOriginal?: boolean;
    /** 上传完成回调 */
    onUploadComplete?: UploadSingleCompleteCallback;
    /** 进度 */
    onProgress?: (progress: FileStatus) => void;
}