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 { ButtonsGroup } from '.';
import 'jest-styled-components';

const options = [
   { name: 'Eren', value: 'Eren' },
   { name: 'Levi', value: 'Levi' },
   { name: 'Rainer', value: 'Rainer' },
   { name: 'Falco', value: 'Falco' },
];

it('snapshot - default', () => {
   function Component(): any {
      return (
         <Form onSubmit={() => {}}>
            <ButtonsGroup name="default" options={options} />
         </Form>
      );
   }

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

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

it('snapshot - com titulo', () => {
   function Component(): any {
      return (
         <Form onSubmit={() => {}}>
            <ButtonsGroup name="default" title="ButtonGroup" options={options} />
         </Form>
      );
   }

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

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

it('snapshot - valor inicial', () => {
   function Component(): any {
      return (
         <Form onSubmit={() => {}} initialData={{ default: 'Rainer' }}>
            <ButtonsGroup name="default" options={options} title="Characters" />
         </Form>
      );
   }

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

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

it('snapshot - valor inicial com multiSelection', () => {
   function Component(): any {
      return (
         <Form onSubmit={() => {}} initialData={{ default: ['Rainer', 'Falco'] }}>
            <ButtonsGroup name="default" options={options} title="Characters" multiSelection />
         </Form>
      );
   }

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

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

it('onSubmit - padrão', () => {
   const onSubmit = jest.fn();

   function Component(): any {
      return (
         <Form onSubmit={(data) => onSubmit(data)}>
            <ButtonsGroup name="buttons" options={options} title="Characters" />
            <button type="submit" data-testid="submit">
               Submit
            </button>
         </Form>
      );
   }

   render(<Component />);

   // Definir data
   fireEvent.click(screen.getByTestId('button-Levi'));
   fireEvent.click(screen.getByTestId('button-Rainer'));
   fireEvent.click(screen.getByTestId('button-Rainer'));
   fireEvent.click(screen.getByTestId('submit'));

   expect(onSubmit).toHaveBeenCalled();
   expect(onSubmit).toHaveBeenCalledWith({
      buttons: 'Rainer',
   });
});

it('onSubmit - multiSelection', () => {
   const onSubmit = jest.fn();

   function Component(): any {
      return (
         <Form onSubmit={(data) => onSubmit(data)}>
            <ButtonsGroup name="buttons" options={options} title="Characters" multiSelection />
            <button type="submit" data-testid="submit">
               Submit
            </button>
         </Form>
      );
   }

   render(<Component />);

   // Definir data
   fireEvent.click(screen.getByTestId('button-Levi'));
   fireEvent.click(screen.getByTestId('button-Falco'));
   fireEvent.click(screen.getByTestId('button-Rainer'));
   fireEvent.click(screen.getByTestId('button-Falco'));
   fireEvent.click(screen.getByTestId('submit'));

   expect(onSubmit).toHaveBeenCalled();
   expect(onSubmit).toHaveBeenCalledWith({
      buttons: ['Levi', 'Rainer'],
   });
});

it('onSubmit - allowEmpty', () => {
   const onSubmit = jest.fn();

   function Component(): any {
      return (
         <Form onSubmit={(data) => onSubmit(data)}>
            <ButtonsGroup name="buttons" options={options} title="Characters" allowEmpty />
            <button type="submit" data-testid="submit">
               Submit
            </button>
         </Form>
      );
   }

   render(<Component />);

   // Definir data
   fireEvent.click(screen.getByTestId('button-Levi'));
   fireEvent.click(screen.getByTestId('button-Rainer'));
   fireEvent.click(screen.getByTestId('button-Rainer'));
   fireEvent.click(screen.getByTestId('submit'));

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

it('onSubmit - padrão com valor inicial', () => {
   const onSubmit = jest.fn();

   function Component(): any {
      return (
         <Form onSubmit={(data) => onSubmit(data)} initialData={{ buttons: 'Rainer' }}>
            <ButtonsGroup name="buttons" options={options} title="Characters" />
            <button type="submit" data-testid="submit">
               Submit
            </button>
         </Form>
      );
   }

   render(<Component />);

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

   expect(onSubmit).toHaveBeenCalled();
   expect(onSubmit).toHaveBeenCalledWith({
      buttons: 'Rainer',
   });
});

it('onSubmit - multiSelection com valor inicial', () => {
   const onSubmit = jest.fn();

   function Component(): any {
      return (
         <Form onSubmit={(data) => onSubmit(data)} initialData={{ buttons: ['Levi', 'Rainer'] }}>
            <ButtonsGroup name="buttons" options={options} title="Characters" multiSelection />
            <button type="submit" data-testid="submit">
               Submit
            </button>
         </Form>
      );
   }

   render(<Component />);

   // Definir data
   fireEvent.click(screen.getByTestId('submit'));

   expect(onSubmit).toHaveBeenCalled();
   expect(onSubmit).toHaveBeenCalledWith({
      buttons: ['Levi', 'Rainer'],
   });
});
