import React from 'react';
import { Controller, useFormContext } from 'react-hook-form';

import FormHelperText from '@mui/material/FormHelperText';

import { Upload } from './upload';
import { UploadBox } from './upload-box';
import { IRHFUploadProps } from '../types';
import { UploadAvatar } from './upload-avatar';

/**
 * @description {RHFUploadAvatar} - Function for uploading avatar using React Hook Form.
 * @param {strung} name - The name of the avatar
 * @param {object} other - Other IRHFUploadProps
 * @return {JSX.Element} The JSX element for the uploaded avatar
 */
export const RHFUploadAvatar = ({ name, ...other }: IRHFUploadProps) => {
  const { control } = useFormContext();

  return (
    <Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <div>
          <UploadAvatar error={!!error} file={field.value} {...other} />

          {!!error && (
            <FormHelperText error sx={{ px: 2, textAlign: 'center' }}>
              {error.message}
            </FormHelperText>
          )}
        </div>
      )}
    />
  );
};

/**
 * @description {RHFUploadBox} - RHFUploadBox component for handling file uploads with react-hook-form.
 * @param {string} name - The name of the upload box
 * @param {object} other - Other props for the upload box
 * @return {ReactNode} The rendered upload box component
 */
export const RHFUploadBox = ({ name, ...other }: IRHFUploadProps) => {
  const { control } = useFormContext();

  return (
    <Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <UploadBox files={field.value} error={!!error} {...other} />
      )}
    />
  );
};

/**
 * @description {RHFUpload} - Function for RHFUpload component.
 * @param {string} name - name of the upload
 * @param {boolean} multiple - whether multiple files can be uploaded
 * @param {string} helperText - helper text for the upload
 * @param {...other} other - other props
 * @return {ReactNode} the rendered RHFUpload component
 */
export const RHFUpload = ({ name, multiple, helperText, ...other }: IRHFUploadProps) => {
  const { control } = useFormContext();

  return (
    <Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) =>
        multiple ? (
          <Upload
            multiple
            accept={{ 'image/*': [] }}
            files={field.value}
            error={!!error}
            helperText={
              (!!error || helperText) && (
                <FormHelperText error={!!error} sx={{ px: 2 }}>
                  {error ? error?.message : helperText}
                </FormHelperText>
              )
            }
            {...other}
          />
        ) : (
          <Upload
            accept={{ 'image/*': [] }}
            file={field.value}
            error={!!error}
            helperText={
              (!!error || helperText) && (
                <FormHelperText error={!!error} sx={{ px: 2 }}>
                  {error ? error?.message : helperText}
                </FormHelperText>
              )
            }
            {...other}
          />
        )
      }
    />
  );
};
