import React, { useEffect, useReducer, useRef, useState } from 'react';
import { faMinus } from '@fortawesome/free-solid-svg-icons/faMinus';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { storiesOf } from '@storybook/react';
import { FormHandles } from '@unform/core';
import { Form } from '@unform/web';
import styled, { ThemeProvider } from 'styled-components';
import { Checkbox } from './index';

const css = styled.div;

const Style = css`
   background: white !important;
`;

storiesOf('Components/Checkbox', module)
   .add('default', () => {
      const [checked, setChecked] = useState();

      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Checkbox title="Checkbox" checked={checked} onChange={setChecked} />
               </div>
            </div>
         </Style>
      );
   })
   .add('custom icon', () => {
      const [checked, setChecked] = useState(true);

      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Checkbox
                     title="Checkbox"
                     checked={checked}
                     onChange={setChecked}
                     icon={<FontAwesomeIcon icon={faMinus} />}
                  />
               </div>
            </div>
         </Style>
      );
   })
   .add('with color', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Checkbox title="Checkbox" color="pink" checked />
               </div>
            </div>
         </Style>
      );
   })
   .add('with rounded', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Checkbox title="Checkbox" rounded checked />
               </div>
            </div>
         </Style>
      );
   })
   .add('with size', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Checkbox title="Checkbox" size={80} checked />
               </div>
            </div>
         </Style>
      );
   })
   .add('with global theme', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <ThemeProvider
                     theme={{
                        checkbox: {
                           defaultColor: 'pink',
                           size: 120,
                           rounded: true,
                        },
                     }}
                  >
                     <Checkbox checked />
                  </ThemeProvider>
               </div>
            </div>
         </Style>
      );
   })
   .add('with variant', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Checkbox title="Checkbox" checked variant="primary" rounded />
                  <Checkbox title="Checkbox" checked variant="secondary" rounded />
                  <Checkbox title="Checkbox" checked variant="success" rounded />
                  <Checkbox title="Checkbox" checked variant="warning" rounded />
                  <Checkbox title="Checkbox" checked variant="danger" rounded />
                  <Checkbox title="Checkbox" checked variant="info" rounded />
                  <Checkbox title="Checkbox" checked variant="light" rounded />
                  <Checkbox title="Checkbox" checked variant="dark" rounded />
               </div>
            </div>
         </Style>
      );
   })
   .add('with initial value', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Checkbox title="Checkbox" color="red" checked />
               </div>
            </div>
         </Style>
      );
   })
   .add('unform - with initial value', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ checkbox: true }}>
                     <Checkbox name="checkbox" title="Checked" color="red" />
                  </Form>
                  <Form onSubmit={() => {}} initialData={{ checkbox: false }}>
                     <Checkbox name="checkbox" title="Not Checked" color="red" />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('unform - with initial value (custom)', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ checkbox: { id: 1 } }}>
                     <Checkbox name="checkbox" title="Checked" color="red" valueTrue={{ id: 1 }} />
                  </Form>
                  <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }}>
                     <Checkbox name="checkbox" title="Not Checked" color="red" valueTrue={{ id: 1 }} />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('unform - with object value async', () => {
      const formRef = useRef<FormHandles>();

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

      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }} ref={formRef}>
                     <Checkbox name="checkbox" title="Checkbox" color="red" valueTrue={{ id: 1 }} />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('unform - with error', () => {
      const formRef = useRef<FormHandles>();

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

      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }} ref={formRef}>
                     <Checkbox name="checkbox" title="Checkbox" color="red" valueTrue={{ id: 1 }} />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('with error', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Checkbox error="Field is required" title="Checkbox" color="red" valueTrue={{ id: 1 }} />
               </div>
            </div>
         </Style>
      );
   })
   .add('with disabled', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}}>
                     <Checkbox name="checkbox" title="Checkbox" color="red" valueTrue={{ id: 1 }} disabled />
                  </Form>
               </div>
            </div>
         </Style>
      );
   });
