/**
 * New Asset Variants 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 { SelectField } from '../../components/SelectField';

import { create } from '../../stores/asset_variants/asset_variantsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';
import type { AssetVariant } from '../../types/entities';

const initialValues = {
  asset: null as AssetVariant['asset'] | null,
  variant_type: 'thumbnail',
  cdn_url: '',
  width_px: '',
  height_px: '',
  size_mb: '',
};

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

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

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

              <FormField label='Variant Type' labelFor='variant_type'>
                <Field name='variant_type' id='variant_type' component='select'>
                  <option value='thumbnail'>thumbnail</option>
                  <option value='preview'>preview</option>
                  <option value='webp'>webp</option>
                  <option value='mp4_low'>mp4_low</option>
                  <option value='mp4_high'>mp4_high</option>
                  <option value='original'>original</option>
                </Field>
              </FormField>

              <FormField label='CDN URL'>
                <Field name='cdn_url' placeholder='CDN URL' />
              </FormField>

              <FormField label='Width (px)'>
                <Field type='number' name='width_px' placeholder='Width (px)' />
              </FormField>

              <FormField label='Height (px)'>
                <Field
                  type='number'
                  name='height_px'
                  placeholder='Height (px)'
                />
              </FormField>

              <FormField label='Size (MB)'>
                <Field type='number' name='size_mb' placeholder='Size (MB)' />
              </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('/asset_variants/asset_variants-list')
                  }
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

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

export default Asset_variantsNew;
