import { CloseCircle, DocumentUpload, Gallery } from 'iconsax-react'
import { type FC, useCallback, useEffect, useState } from 'react'
import { useDropzone } from 'react-dropzone';
import { useTranslation } from 'react-i18next';
import { clx } from '../helpers/utils';

type Props = {
    label: string;
    onChange: (file: File[]) => void;
    isMultiple?: boolean;
    isFile?: boolean,
    preview?: string[],
    onChangePreview?: (preview: string[]) => void,
    getCover?: (url: string) => void,
    coverUrl?: string,
    imageSize?: 'small' | 'medium' | 'large'
}

const UploadBoxDraggble: FC<Props> = (props: Props) => {

    const { t } = useTranslation('global')
    const [files, setFiles] = useState<File[]>([])
    const [perviews, setPerviews] = useState<string[]>(props.preview ? props.preview : [])
    const [isFill, setIsFill] = useState<boolean>(false)
    const [cover, setCover] = useState<string>(props.coverUrl ? props.coverUrl : '')

    const onDrop = useCallback((acceptedFiles: File[]) => {
        if (props.isMultiple) {
            const array = [...files]
            array.push(acceptedFiles[0])
            setFiles(array)
            props.onChange(array)
        } else {
            setFiles([acceptedFiles[0]])
            props.onChange([acceptedFiles[0]])
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [files])
    const { getRootProps, getInputProps } = useDropzone({ onDrop })

    const handleDelete = (index: number) => {
        const array = [...files]
        array.splice(index, 1)
        setFiles(array)
        props.onChange(array)
    }

    const handleDeletePreview = (index: number) => {
        const array = [...perviews];
        array.splice(index, 1);
        setPerviews(array);
        if (props.onChangePreview) {
            props.onChangePreview(array);
        }
    }

    useEffect(() => {

        if (props.preview && props.preview.length > 0 && !isFill) {
            setPerviews(props.preview)
            console.log('preview', props.preview);

            setIsFill(true)
        }

    }, [props.preview, isFill])

    useEffect(() => {
        setCover(props.coverUrl ? props.coverUrl : '')
    }, [props.coverUrl])


    return (
        <div>
            <div {...getRootProps()} className='w-full py-8 border border-dashed border-description flex flex-col justify-center items-center gap-4 rounded-2xl'>
                <input {...getInputProps()} />
                <Gallery
                    className='size-8'
                    color='#8C90A3'
                />
                <div className='text-description text-xs'>
                    {props.label}
                </div>

                <div className='flex items-center gap-2'>
                    <DocumentUpload
                        className='size-4'
                        color='black'
                    />
                    <div className='text-xs'>{t('upload')}</div>
                </div>
            </div>

            {
                files.length > 0 || perviews ? (
                    <div className={clx(
                        'mt-4',
                        props.imageSize === 'large' ? 'grid grid-cols-2 gap-4' : 'flex gap-4 items-center flex-wrap'
                    )}>
                        {
                            perviews && perviews.map((item, index) => {
                                const imageSizeClass = props.imageSize === 'large'
                                    ? 'w-full h-32 rounded-lg'
                                    : props.imageSize === 'medium'
                                        ? 'size-20 rounded-lg'
                                        : 'size-10 rounded-full'
                                return (
                                    <div key={index} className='flex items-center gap-2'>
                                        <div key={index} className='flex relative items-center gap-2'>
                                            <img onClick={() => {
                                                if (props.getCover) {
                                                    setCover(item)
                                                    props.getCover(item)
                                                }
                                            }} src={item}
                                                className={clx(
                                                    imageSizeClass,
                                                    'object-cover cursor-pointer',
                                                    cover === item ? 'border-2 border-red-400' : ''
                                                )} />
                                            <div onClick={() => handleDeletePreview(index)} className='absolute -left-2 -top-2 shadow-md bg-white size-5 rounded-full flex justify-center items-center cursor-pointer z-10'>
                                                <CloseCircle className='size-4 ' color='red' />
                                            </div>
                                        </div>
                                    </div>
                                )
                            })
                        }
                        {
                            files.map((file, index) => {
                                if (props.isFile) {
                                    return (
                                        <div key={index} className='flex border p-2 rounded-lg items-center gap-2'>
                                            <div className='flex relative items-center gap-2'>
                                                <div className='text-xs'>{file.name}</div>
                                                <div className='absolute -left-4 -top-4 shadow-md bg-white size-5 rounded-full flex justify-center items-center cursor-pointer'>
                                                    <CloseCircle onClick={() => handleDelete(index)} className='size-4 ' color='red' />
                                                </div>
                                            </div>
                                        </div>
                                    )
                                }
                                else {
                                    const imageSizeClass = props.imageSize === 'large'
                                        ? 'w-full h-32 rounded-lg'
                                        : props.imageSize === 'medium'
                                            ? 'size-20 rounded-lg'
                                            : 'size-10 rounded-full'
                                    return (
                                        <div key={index} className='flex items-center gap-2'>
                                            <div key={index} className='flex relative items-center gap-2'>
                                                <img src={URL.createObjectURL(file)} alt={file.name} className={clx(imageSizeClass, 'object-cover')} />
                                                <div className='absolute -left-2 -top-2 shadow-md bg-white size-5 rounded-full flex justify-center items-center cursor-pointer z-10'>
                                                    <CloseCircle onClick={() => handleDelete(index)} className='size-4 ' color='red' />
                                                </div>
                                            </div>
                                        </div>
                                    )
                                }
                            })
                        }
                    </div>
                ) : null
            }

        </div>
    )
}

export default UploadBoxDraggble