/**
 * New Transition Page
 * Cleaned up version
 */

import { mdiChartTimelineVariant } from '@mdi/js';
import Head from 'next/head';
import React, { ReactElement } from 'react';

import CardBox from '../../components/CardBox';
import LayoutAuthenticated from '../../layouts/Authenticated';
import SectionMain from '../../components/SectionMain';
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
import { getPageTitle } from '../../config';

import { Field, Form, Formik } from 'formik';
import FormField from '../../components/FormField';
import BaseDivider from '../../components/BaseDivider';
import BaseButtons from '../../components/BaseButtons';
import BaseButton from '../../components/BaseButton';
import { SwitchField } from '../../components/SwitchField';
import { SelectField } from '../../components/SelectField';

import { create } from '../../stores/transitions/transitionsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';

const initialValues = {
  project: '',
  environment: 'dev',
  source_key: '',
  name: '',
  slug: '',
  video_url: '',
  audio_url: '',
  supports_reverse: false,
  duration_sec: '',
};

const TransitionsNew = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();

  const handleSubmit = async (data: typeof initialValues) => {
    await dispatch(create(data));
    await router.push('/transitions/transitions-list');
  };

  return (
    <>
      <Head>
        <title>{getPageTitle('New Transition')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New Transition'
          main
        >
          {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>
              <FormField label='Project' labelFor='project'>
                <Field
                  name='project'
                  id='project'
                  component={SelectField}
                  options={[]}
                  itemRef='projects'
                />
              </FormField>

              <FormField label='Environment' labelFor='environment'>
                <Field name='environment' id='environment' component='select'>
                  <option value='dev'>dev</option>
                  <option value='stage'>stage</option>
                  <option value='production'>production</option>
                </Field>
              </FormField>

              <FormField label='Source Key'>
                <Field name='source_key' placeholder='Source Key' />
              </FormField>

              <FormField label='Name'>
                <Field name='name' placeholder='Name' />
              </FormField>

              <FormField label='Slug'>
                <Field name='slug' placeholder='Slug' />
              </FormField>

              <FormField label='Video URL'>
                <Field name='video_url' placeholder='Video URL' />
              </FormField>

              <FormField label='Audio URL'>
                <Field name='audio_url' placeholder='Audio URL' />
              </FormField>

              <FormField label='Supports Reverse' labelFor='supports_reverse'>
                <Field
                  name='supports_reverse'
                  id='supports_reverse'
                  component={SwitchField}
                />
              </FormField>

              <FormField label='Duration (sec)'>
                <Field
                  type='number'
                  name='duration_sec'
                  placeholder='Duration (sec)'
                />
              </FormField>

              <BaseDivider />
              <BaseButtons>
                <BaseButton type='submit' color='info' label='Submit' />
                <BaseButton type='reset' color='info' outline label='Reset' />
                <BaseButton
                  type='reset'
                  color='danger'
                  outline
                  label='Cancel'
                  onClick={() => router.push('/transitions/transitions-list')}
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

TransitionsNew.getLayout = function getLayout(page: ReactElement) {
  return (
    <LayoutAuthenticated permission='CREATE_TRANSITIONS'>
      {page}
    </LayoutAuthenticated>
  );
};

export default TransitionsNew;
