/**
 * New Asset 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/assets/assetsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';

import type { Asset } from '../../types/entities';

const initialValues = {
  project: '',
  name: '',
  asset_type: 'image' as 'image' | 'video' | 'audio' | 'file',
  type: 'general' as Asset['type'],
  cdn_url: '',
  storage_key: '',
  mime_type: '',
  size_mb: '',
  width_px: '',
  height_px: '',
  duration_sec: '',
  checksum: '',
  is_public: false,
  is_deleted: false,
  deleted_at_time: '',
};

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('New Asset')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New Asset'
          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='Name'>
                <Field name='name' placeholder='Name' />
              </FormField>

              <FormField label='Asset Format' labelFor='asset_type'>
                <Field name='asset_type' id='asset_type' component='select'>
                  <option value='image'>image</option>
                  <option value='video'>video</option>
                  <option value='audio'>audio</option>
                  <option value='file'>file</option>
                </Field>
              </FormField>

              <FormField label='Type' labelFor='type'>
                <Field name='type' id='type' component='select'>
                  <option value='general'>general</option>
                  <option value='icon'>icon</option>
                  <option value='background_image'>background_image</option>
                  <option value='audio'>audio</option>
                  <option value='video'>video</option>
                  <option value='transition'>transition</option>
                  <option value='logo'>logo</option>
                  <option value='favicon'>favicon</option>
                  <option value='document'>document</option>
                </Field>
              </FormField>

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

              <FormField label='Storage Key'>
                <Field name='storage_key' placeholder='Storage Key' />
              </FormField>

              <FormField label='MIME Type'>
                <Field name='mime_type' placeholder='MIME Type' />
              </FormField>

              <FormField label='Size (MB)'>
                <Field type='number' name='size_mb' placeholder='Size (MB)' />
              </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='Duration (sec)'>
                <Field
                  type='number'
                  name='duration_sec'
                  placeholder='Duration (sec)'
                />
              </FormField>

              <FormField label='Checksum'>
                <Field name='checksum' placeholder='Checksum' />
              </FormField>

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

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

              <FormField label='Deleted At'>
                <Field
                  type='datetime-local'
                  name='deleted_at_time'
                  placeholder='Deleted At'
                />
              </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('/assets/assets-list')}
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

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

export default AssetsNew;
