import React, { useEffect, useReducer, useRef } from 'react';
import { storiesOf } from '@storybook/react';
import { FormHandles } from '@unform/core';
import { Form } from '@unform/web';
import styled from 'styled-components';
import { Checkbox } from '.';

const css = styled.div;

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

storiesOf('Components/Forms/Checkbox', module)
   .add('default', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}}>
                     <Checkbox name="default" title="Checkbox" />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('with color', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ checkbox: true }}>
                     <Checkbox name="checkbox" title="Checkbox" color="red" />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('with object value', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ checkbox: { id: 1 } }}>
                     <Checkbox name="checkbox" title="Checkbox" color="red" value={{ id: 1 }} />
                  </Form>
                  <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }}>
                     <Checkbox name="checkbox" title="Checkbox" color="red" value={{ id: 1 }} />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('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" value={{ id: 1 }} />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('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" value={{ id: 1 }} />
                  </Form>
               </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" value={{ id: 1 }} disabled />
                  </Form>
               </div>
            </div>
         </Style>
      );
   });
