/**
 * New Tour 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/tour_pages/tour_pagesSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';
import type { TourPage } from '../../types/entities';

const initialValues = {
  project: '' as TourPage['project'] | string,
  environment: 'dev',
  source_key: '',
  name: '',
  slug: '',
  sort_order: '',
  background_image_url: '',
  background_video_url: '',
  background_audio_url: '',
  background_loop: false,
  requires_auth: false,
  ui_schema_json: '',
};

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

  const handleSubmit = async (data: typeof initialValues) => {
    await dispatch(create(data as unknown as Partial<TourPage>));
    await router.push('/tour_pages/tour_pages-list');
  };

  return (
    <>
      <Head>
        <title>{getPageTitle('New Tour Page')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New Tour Page'
          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='Sort Order'>
                <Field
                  type='number'
                  name='sort_order'
                  placeholder='Sort Order'
                />
              </FormField>

              <FormField label='Background Image URL'>
                <Field
                  name='background_image_url'
                  placeholder='Background Image URL'
                />
              </FormField>

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

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

              <FormField label='Background Loop' labelFor='background_loop'>
                <Field
                  name='background_loop'
                  id='background_loop'
                  component={SwitchField}
                />
              </FormField>

              <FormField label='Requires Auth' labelFor='requires_auth'>
                <Field
                  name='requires_auth'
                  id='requires_auth'
                  component={SwitchField}
                />
              </FormField>

              <FormField label='UI Schema JSON' hasTextareaHeight>
                <Field
                  name='ui_schema_json'
                  as='textarea'
                  placeholder='UI Schema JSON'
                />
              </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('/tour_pages/tour_pages-list')}
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

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

export default Tour_pagesNew;
