import React, { useEffect, useRef, useState } from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import { FormHandles } from '@unform/core';
import { Form } from '@unform/web';
import { Radio } from './Radio';
import 'jest-styled-components';

describe('checkbox', () => {
   it('snapshot - default', () => {
      function Component(): any {
         return (
            <Form onSubmit={() => {}}>
               <Radio
                  name="default"
                  options={[
                     { value: 'M', name: 'Male' },
                     { value: 'F', name: 'Female' },
                     { value: 'D', name: 'Disabled', disabled: true },
                  ]}
               />
            </Form>
         );
      }

      const { container } = render(<Component />);

      expect(container.firstChild).toMatchSnapshot();
   });

   it('snapshot - with color', () => {
      function Component(): any {
         return (
            <Form onSubmit={() => {}} initialData={{ checkbox: true }}>
               <Radio
                  color="orange"
                  name="radio"
                  options={[
                     { value: 'M', name: 'Male' },
                     { value: 'F', name: 'Female' },
                     { value: 'D', name: 'Disabled', disabled: true },
                  ]}
               />
            </Form>
         );
      }

      const { container } = render(<Component />);

      expect(container.firstChild).toMatchSnapshot();
   });

   it('snapshot - with rightRadio', () => {
      function Component(): any {
         return (
            <Form onSubmit={() => {}} initialData={{ checkbox: true }}>
               <Radio
                  name="radio"
                  rightRadio
                  options={[
                     { value: 'M', name: 'Male' },
                     { value: 'F', name: 'Female' },
                     { value: 'D', name: 'Disabled', disabled: true },
                  ]}
               />
            </Form>
         );
      }

      const { container } = render(<Component />);

      expect(container.firstChild).toMatchSnapshot();
   });

   it('snapshot - with object value', () => {
      function Component(): any {
         return (
            <>
               <Form onSubmit={() => {}} initialData={{ radio: { id: 'F' } }}>
                  <Radio
                     color="red"
                     name="radio"
                     options={[
                        { value: { id: 'M' }, name: 'Male' },
                        { value: { id: 'F' }, name: 'Female' },
                        { value: { id: 'D' }, name: 'Disabled', disabled: true },
                     ]}
                  />
               </Form>
               <Form onSubmit={() => {}} initialData={{ radio: { id: 'T' } }}>
                  <Radio
                     color="red"
                     name="radio"
                     options={[
                        { value: { id: 'M' }, name: 'Male' },
                        { value: { id: 'F' }, name: 'Female' },
                        { value: { id: 'D' }, name: 'Disabled', disabled: true },
                     ]}
                  />
               </Form>
            </>
         );
      }

      const { container } = render(<Component />);

      expect(container.firstChild).toMatchSnapshot();
   });

   it('snapshot - with object value async', () => {
      function Component(): any {
         const formRef = useRef<FormHandles>();

         useEffect(() => {
            formRef.current.setFieldValue('radio', { id: 'F' });
         }, []);

         return (
            <Form onSubmit={() => {}} initialData={{ radio: { id: 2 } }} ref={formRef}>
               <Radio
                  name="radio"
                  options={[
                     { value: { id: 'M' }, name: 'Male' },
                     { value: { id: 'F' }, name: 'Female' },
                     { value: { id: 'D' }, name: 'Disabled', disabled: true },
                  ]}
               />
            </Form>
         );
      }

      const { container } = render(<Component />);

      expect(container.firstChild).toMatchSnapshot();
   });

   it('snapshot - with error', () => {
      function Component(): any {
         const formRef = useRef<FormHandles>();

         useEffect(() => {
            formRef.current.setFieldError('radio', 'Field is required');
         }, []);

         return (
            <Form onSubmit={() => {}} initialData={{ radio: { id: 2 } }} ref={formRef}>
               <Radio
                  name="radio"
                  options={[
                     { value: { id: 'M' }, name: 'Male' },
                     { value: { id: 'F' }, name: 'Female' },
                     { value: { id: 'D' }, name: 'Disabled', disabled: true },
                  ]}
               />
            </Form>
         );
      }

      const { container } = render(<Component />);

      expect(screen.getByTestId('input-error')).not.toBeUndefined();

      expect(container.firstChild).toMatchSnapshot();
   });

   it('snapshot - with disabled', () => {
      function Component(): any {
         const formRef = useRef<FormHandles>();

         useEffect(() => {
            formRef.current.setFieldError('checkbox', 'Field is required');
         }, []);

         return (
            <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }} ref={formRef}>
               <Radio
                  name="radio"
                  options={[
                     { value: { id: 'M' }, name: 'Male' },
                     { value: { id: 'F' }, name: 'Female', disabled: true },
                     { value: { id: 'D' }, name: 'Disabled', disabled: true },
                  ]}
               />
            </Form>
         );
      }

      const { container } = render(<Component />);

      expect(screen.getAllByTestId('input-radio-circle-0')[0].className).not.toContain('disabled');
      expect(screen.getAllByTestId('input-radio-circle-1')[0].className).toContain('disabled');
      expect(screen.getAllByTestId('input-radio-circle-2')[0].className).toContain('disabled');

      expect(container.firstChild).toMatchSnapshot();
   });

   it('check onChange', async () => {
      const onChange = jest.fn();

      function Component(): any {
         const [value, setValue] = useState();

         return (
            <>
               <Radio
                  value={value}
                  onChange={(v: any) => {
                     onChange(v);
                     setValue(v);
                  }}
                  options={[
                     { value: { id: 'M' }, name: 'Male' },
                     { value: { id: 'F' }, name: 'Female', disabled: true },
                     { value: { id: 'D' }, name: 'Disabled', disabled: true },
                  ]}
               />
            </>
         );
      }

      render(<Component />);

      await fireEvent.change(screen.getByTestId('radio-0'), { target: { checked: { id: 'M' } } });
      await fireEvent.click(screen.getByTestId('radio-0'));
      await fireEvent.change(screen.getByTestId('radio-1'), { target: { checked: { id: 'F' } } });
      await fireEvent.click(screen.getByTestId('radio-1'));

      expect(onChange).toHaveBeenCalled();
      expect(onChange).toHaveBeenCalledWith({ id: 'M' });
      expect(onChange).toHaveBeenCalledWith({ id: 'F' });
   });

   it('unform - get value on onSubmit - initial value object', () => {
      const onSubmit = jest.fn();

      function Component(): any {
         const formRef = useRef<FormHandles>();

         return (
            <>
               <Form onSubmit={(v) => onSubmit(v)} initialData={{ radio: { id: 'M' } }} ref={formRef}>
                  <Radio
                     color="red"
                     name="radio"
                     options={[
                        { value: { id: 'M' }, name: 'Male' },
                        { value: { id: 'F' }, name: 'Female' },
                        { value: { id: 'D' }, name: 'Disabled', disabled: true },
                     ]}
                  />
                  <button
                     data-testid="submit"
                     title="submit"
                     onClick={() => {
                        formRef.current.submitForm();
                     }}
                  />
               </Form>
            </>
         );
      }

      render(<Component />);

      fireEvent.click(screen.getByTestId('submit'));

      expect(onSubmit).toHaveBeenCalled();
      expect(onSubmit).toHaveBeenCalledWith({
         radio: { id: 'M' },
      });
   });

   it('unform - get value on onSubmit - initial value default', () => {
      const onSubmit = jest.fn();

      function Component(): any {
         const formRef = useRef<FormHandles>();

         return (
            <>
               <Form onSubmit={(v) => onSubmit(v)} initialData={{ radio: 'M' }} ref={formRef}>
                  <Radio
                     color="red"
                     name="radio"
                     options={[
                        { value: 'M', name: 'Male' },
                        { value: 'F', name: 'Female' },
                        { value: 'D', name: 'Disabled', disabled: true },
                     ]}
                  />
                  <button
                     data-testid="submit"
                     title="submit"
                     onClick={() => {
                        formRef.current.submitForm();
                     }}
                  />
               </Form>
            </>
         );
      }

      render(<Component />);

      fireEvent.click(screen.getByTestId('submit'));

      expect(onSubmit).toHaveBeenCalled();
      expect(onSubmit).toHaveBeenCalledWith({
         radio: 'M',
      });
   });

   it('unform - get value on onSubmit - clear form', () => {
      const onSubmit = jest.fn();

      function Component(): any {
         const formRef = useRef<FormHandles>();

         function clearForm(): void {
            formRef.current.reset();
         }

         return (
            <>
               <Form onSubmit={(v) => onSubmit(v)} initialData={{ radio: 'M' }} ref={formRef}>
                  <Radio
                     color="red"
                     name="radio"
                     options={[
                        { value: 'M', name: 'Male' },
                        { value: 'F', name: 'Female' },
                        { value: 'D', name: 'Disabled', disabled: true },
                     ]}
                  />
                  <button
                     data-testid="submit"
                     title="submit"
                     onClick={() => {
                        formRef.current.submitForm();
                     }}
                  />
                  <button data-testid="clear" title="clear" onClick={clearForm} />
               </Form>
            </>
         );
      }

      render(<Component />);

      fireEvent.click(screen.getByTestId('clear'));
      fireEvent.click(screen.getByTestId('submit'));

      expect(onSubmit).toHaveBeenCalled();
      expect(onSubmit).toHaveBeenCalledWith({
         radio: null,
      });
   });

   it('unform - get value on onSubmit - set data', () => {
      const onSubmit = jest.fn();

      function Component(): any {
         const formRef = useRef<FormHandles>();

         function setData(): void {
            formRef.current.setFieldValue('radio', 'F');
         }

         return (
            <>
               <Form onSubmit={(v) => onSubmit(v)} ref={formRef}>
                  <Radio
                     color="red"
                     name="radio"
                     options={[
                        { value: 'M', name: 'Male' },
                        { value: 'F', name: 'Female' },
                        { value: 'D', name: 'Disabled', disabled: true },
                     ]}
                  />
                  <button data-testid="setData" title="setData" onClick={setData} />
                  <button
                     data-testid="submit"
                     title="submit"
                     onClick={() => {
                        formRef.current.submitForm();
                     }}
                  />
               </Form>
            </>
         );
      }

      render(<Component />);

      fireEvent.click(screen.getByTestId('setData'));
      fireEvent.click(screen.getByTestId('submit'));

      expect(onSubmit).toHaveBeenCalled();
      expect(onSubmit).toHaveBeenCalledWith({
         radio: 'F',
      });
   });
});
