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

const css = styled.div;

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

storiesOf('Components/Forms/Radio', module)
   .add('default', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}}>
                     <Radio
                        name="default"
                        options={[
                           { id: 'M', label: 'Male' },
                           { id: 'F', label: 'Female' },
                           { id: 'D', label: 'Disabled', disabled: true },
                        ]}
                     />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('with color', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ radio: 'M' }}>
                     <Radio
                        color="red"
                        name="radio"
                        options={[
                           { id: 'M', label: 'Male' },
                           { id: 'F', label: 'Female' },
                           { id: 'D', label: 'Disabled', disabled: true },
                        ]}
                     />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('with object value', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ radio: { id: 'F' } }}>
                     <Radio
                        color="red"
                        name="radio"
                        options={[
                           { id: { id: 'M' }, label: 'Male' },
                           { id: { id: 'F' }, label: 'Female' },
                           { id: { id: 'D' }, label: 'Disabled', disabled: true },
                        ]}
                     />
                  </Form>
                  <Form onSubmit={() => {}} initialData={{ radio: { id: 'T' } }}>
                     <Radio
                        color="red"
                        name="radio"
                        options={[
                           { id: { id: 'M' }, label: 'Male' },
                           { id: { id: 'F' }, label: 'Female' },
                           { id: { id: 'D' }, label: 'Disabled', disabled: true },
                        ]}
                     />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('with object value async', () => {
      const formRef = useRef<FormHandles>();

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

      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ radio: { id: 2 } }} ref={formRef}>
                     <Radio
                        name="radio"
                        options={[
                           { id: { id: 'M' }, label: 'Male' },
                           { id: { id: 'F' }, label: 'Female' },
                           { id: { id: 'D' }, label: 'Disabled', disabled: true },
                        ]}
                     />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('with error', () => {
      const formRef = useRef<FormHandles>();

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

      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}} initialData={{ radio: { id: 2 } }} ref={formRef}>
                     <Radio
                        name="radio"
                        options={[
                           { id: { id: 'M' }, label: 'Male' },
                           { id: { id: 'F' }, label: 'Female' },
                           { id: { id: 'D' }, label: 'Disabled', disabled: true },
                        ]}
                     />
                  </Form>
               </div>
            </div>
         </Style>
      );
   })
   .add('with disabled', () => {
      return (
         <Style>
            <div className="row">
               <div className="m-4 col-sm-3">
                  <Form onSubmit={() => {}}>
                     <Radio
                        name="radio"
                        options={[
                           { id: { id: 'M' }, label: 'Male' },
                           { id: { id: 'F' }, label: 'Female', disabled: true },
                           { id: { id: 'D' }, label: 'Disabled', disabled: true },
                        ]}
                     />
                  </Form>
               </div>
            </div>
         </Style>
      );
   });
