/**
 * New Page Elements 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/page_elements/page_elementsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';
import type { PageElement } from '../../types/entities';

interface FormValues {
  page: string;
  element_type: string;
  name: string;
  sort_order: string;
  is_visible: boolean;
  x_percent: string;
  y_percent: string;
  width_percent: string;
  height_percent: string;
  rotation_deg: string;
  style_json: string;
  content_json: string;
}

const initialValues: FormValues = {
  page: '',
  element_type: 'nav_button',
  name: '',
  sort_order: '',
  is_visible: false,
  x_percent: '',
  y_percent: '',
  width_percent: '',
  height_percent: '',
  rotation_deg: '',
  style_json: '',
  content_json: '',
};

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

  const handleSubmit = async (data: FormValues) => {
    await dispatch(create(data as unknown as Partial<PageElement>));
    await router.push('/page_elements/page_elements-list');
  };

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

              <FormField label='Element Type' labelFor='element_type'>
                <Field name='element_type' id='element_type' component='select'>
                  <option value='nav_button'>nav_button</option>
                  <option value='spot'>spot</option>
                  <option value='description'>description</option>
                  <option value='tooltip'>tooltip</option>
                  <option value='gallery'>gallery</option>
                  <option value='carousel'>carousel</option>
                  <option value='logo'>logo</option>
                  <option value='video_player'>video_player</option>
                  <option value='popup'>popup</option>
                </Field>
              </FormField>

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

              <FormField label='Sort Order'>
                <Field
                  type='number'
                  name='sort_order'
                  placeholder='Sort Order'
                />
              </FormField>

              <FormField label='Is Visible' labelFor='is_visible'>
                <Field
                  name='is_visible'
                  id='is_visible'
                  component={SwitchField}
                />
              </FormField>

              <FormField label='X (%)'>
                <Field type='number' name='x_percent' placeholder='X (%)' />
              </FormField>

              <FormField label='Y (%)'>
                <Field type='number' name='y_percent' placeholder='Y (%)' />
              </FormField>

              <FormField label='Width (%)'>
                <Field
                  type='number'
                  name='width_percent'
                  placeholder='Width (%)'
                />
              </FormField>

              <FormField label='Height (%)'>
                <Field
                  type='number'
                  name='height_percent'
                  placeholder='Height (%)'
                />
              </FormField>

              <FormField label='Rotation (deg)'>
                <Field
                  type='number'
                  name='rotation_deg'
                  placeholder='Rotation (deg)'
                />
              </FormField>

              <FormField label='Style JSON' hasTextareaHeight>
                <Field
                  name='style_json'
                  as='textarea'
                  placeholder='Style JSON'
                />
              </FormField>

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

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

export default Page_elementsNew;
