import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head';
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';
import dayjs from 'dayjs';
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
import { useRouter } from 'next/router';
import { fetch } from '../../stores/page_elements/page_elementsSlice';
import { saveFile } from '../../helpers/fileSaver';
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from '../../components/ImageField';
import LayoutAuthenticated from '../../layouts/Authenticated';
import { getPageTitle } from '../../config';
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
import SectionMain from '../../components/SectionMain';
import CardBox from '../../components/CardBox';
import BaseButton from '../../components/BaseButton';
import BaseDivider from '../../components/BaseDivider';
import { mdiChartTimelineVariant } from '@mdi/js';
import { SwitchField } from '../../components/SwitchField';
import FormField from '../../components/FormField';
import type { PageElement } from '../../types/entities';

const Page_elementsView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const pageElementsState = useAppSelector((state) => state.page_elements);
  const page_elements_data = pageElementsState.page_elements as
    | PageElement
    | PageElement[]
    | undefined;
  const pageElementRaw = Array.isArray(page_elements_data)
    ? page_elements_data[0]
    : page_elements_data;
  const pageElement = pageElementRaw as unknown as
    | Record<string, unknown>
    | undefined;

  const { id } = router.query;
  const idStr = Array.isArray(id) ? id[0] : id;

  function removeLastCharacter(str: string): string {
    return str.slice(0, -1);
  }

  useEffect(() => {
    if (idStr) {
      dispatch(fetch({ id: idStr }));
    }
  }, [dispatch, idStr]);

  return (
    <>
      <Head>
        <title>{getPageTitle('View page_elements')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View page_elements')}
          main
        >
          <BaseButton
            color='info'
            label='Edit'
            href={`/page_elements/page_elements-edit/?id=${idStr}`}
          />
        </SectionTitleLineWithButton>
        <CardBox>
          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Page</p>

            <p>
              {(pageElement?.page as { name?: string } | undefined)?.name ??
                'No data'}
            </p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Elementtype</p>
            <p>{(pageElement?.element_type as string) ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Name</p>
            <p>{pageElement?.name as string}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Sortorder</p>
            <p>{(pageElement?.sort_order as string | number) || 'No data'}</p>
          </div>

          <FormField label='Isvisible'>
            <SwitchField
              field={{
                name: 'is_visible',
                value: pageElement?.is_visible as boolean,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>X(%)</p>
            <p>{(pageElement?.x_percent as string) || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Y(%)</p>
            <p>{(pageElement?.y_percent as string) || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Width(%)</p>
            <p>{(pageElement?.width_percent as string) || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Height(%)</p>
            <p>{(pageElement?.height_percent as string) || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Rotation(deg)</p>
            <p>{(pageElement?.rotation_deg as string) || 'No data'}</p>
          </div>

          <FormField label='Multi Text' hasTextareaHeight>
            <textarea
              className={'w-full'}
              disabled
              value={(pageElement?.style_json as string) ?? ''}
            />
          </FormField>

          <FormField label='Multi Text' hasTextareaHeight>
            <textarea
              className={'w-full'}
              disabled
              value={(pageElement?.content_json as string) ?? ''}
            />
          </FormField>

          <BaseDivider />

          <BaseButton
            color='info'
            label='Back'
            onClick={() => router.push('/page_elements/page_elements-list')}
          />
        </CardBox>
      </SectionMain>
    </>
  );
};

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

export default Page_elementsView;
