import { DeleteOutlined } from '@ant-design/icons';
import { CustomInput } from '../../view/CustomInput';
import { LinkButton } from '../../view/LinkButton';
import { OutlinedButton } from '../../view/OutlinedButton';
import { Details } from '../../../types/education';
import { i18nText } from '../../../i18nKeys';
import { Upload, UploadFile } from 'antd';
import { validateDoc } from '../../../utils/account';
import { useLocalStorage } from '../../../hooks/useLocalStorage';
import { AUTH_TOKEN_KEY } from '../../../constants/common';

type EducationsContentProps = {
    educations?: Details[];
    update: (edu?: Details[]) => void;
    locale: string;
};

export const EducationsContent = ({
    educations,
    update,
    locale
}: EducationsContentProps) => {
    const [jwt] = useLocalStorage(AUTH_TOKEN_KEY, '');

    const addEdu = () => {
        const edu = {
            title: undefined,
            description: undefined,
            document: null
        };

        update(educations?.length > 0
            ? [
                ...educations,
                edu
            ]
            : [edu]);
    };

    const deleteEdu = (index: number) => {
        update([...educations].filter((ed, i) => i !== index));
    };

    const beforeUpload = (file: UploadFile) => {
        const isValid = validateDoc(file);

        if (!isValid) {
            return Upload.LIST_IGNORE;
        }

        return true;
    }

    const onRemoveFile = (index: number) => {
        update(educations?.map((edu, i) => {
            if (i === index) {
                return {
                    ...edu,
                    document: null,
                }
            }

            return edu;
        }));
    };

    const onChange = (file: any, index: number) => {
        if (file?.response) {
            update([...educations].map((edu, i) => {
                if (i === index) {
                    return {
                        ...edu,
                        document: file?.response || null,
                    }
                }

                return edu;
            }));
        }
    };

    const onChangeUniversity = (val: string, index: number) => {
        update(educations?.map((edu, i) => {
            if (i === index) {
                return {
                    ...edu,
                    title: val,
                }
            }

            return edu;
        }));
    };

    const onChangeDesc = (val: string, index: number) => {
        update(educations?.map((edu, i) => {
            if (i === index) {
                return {
                    ...edu,
                    description: val,
                }
            }

            return edu;
        }));
    };

    return (
        <div className="b-edu-content">
            <div className="b-edu-list">
                {educations?.map(({ title, description, document: file}, index) => (
                    <div className="b-edu-list__item" key={`edu_${index}`}>
                        <div>
                            <CustomInput
                                value={title}
                                placeholder={i18nText('university', locale)}
                                onChange={(e) => onChangeUniversity(e?.target?.value, index)}
                            />
                            <CustomInput
                                value={description}
                                placeholder={i18nText('description', locale)}
                                onChange={(e) => onChangeDesc(e?.target?.value, index)}
                            />
                            <Upload
                                fileList={file ? [
                                    {
                                        uid: file.original?.id,
                                        name: file.fileName,
                                        status: 'done',
                                        url: file.original?.url
                                    }
                                ] : undefined}
                                accept=".jpg,.jpeg,.png,.pdf"
                                beforeUpload={beforeUpload}
                                multiple={false}
                                onRemove={() => onRemoveFile(index)}
                                action="https://api.bbuddy.expert/api/home/uploadfile"
                                method="POST"
                                headers={{
                                    authorization: `Bearer ${jwt}`,
                                    'X-User-Language': locale,
                                    'X-Referrer-Channel': 'site',
                                }}
                                onChange={(obj) => onChange(obj.file, index)}
                            >
                                <LinkButton type="link">{i18nText('addDiploma', locale)}</LinkButton>
                            </Upload>
                        </div>
                        <LinkButton
                            type="link"
                            danger
                            icon={<DeleteOutlined />}
                            onClick={() => deleteEdu(index)}
                        />
                    </div>
                ))}
            </div>
            <OutlinedButton
                type="link"
                onClick={addEdu}
            >
                {i18nText('addNew', locale)}
            </OutlinedButton>
        </div>
    );
};
