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 MbasContentProps = {
    mbas?: Details[];
    update: (mba?: Details[]) => void;
    locale: string;
};

export const MbasContent = ({
    mbas,
    update,
    locale
}: MbasContentProps) => {
    const [jwt] = useLocalStorage(AUTH_TOKEN_KEY, '');

    const addMba = () => {
        const mba = {
            title: undefined,
            description: undefined,
            document: null
        };

        update(mbas?.length > 0
            ? [
                ...mbas,
                mba
            ]
            : [mba]);
    };

    const deleteMba = (index: number) => {
        update([...mbas].filter((mba, i) => i !== index));
    };

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

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

        return true;
    }

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

            return mb;
        }));
    };

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

                return mb;
            }));
        }
    };

    const onChangeName = (val: string, index: number) => {
        update(mbas?.map((mb, i) => {
            if (i === index) {
                return {
                    ...mb,
                    title: val,
                }
            }

            return mb;
        }));
    };

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

            return mb;
        }));
    };

    return (
        <div className="b-edu-content">
            <div className="b-edu-list">
                {mbas?.map(({ title, description, document: file}, index) => (
                    <div className="b-edu-list__item" key={`mba_${index}`}>
                        <div>
                            <CustomInput
                                value={title}
                                placeholder={i18nText('university', locale)}
                                onChange={(e) => onChangeName(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={() => deleteMba(index)}
                        />
                    </div>
                ))}
            </div>
            <OutlinedButton
                type="link"
                onClick={addMba}
            >
                {i18nText('addNew', locale)}
            </OutlinedButton>
        </div>
    );
};
