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_links/page_linksSlice';
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 { PageLink } from '../../types/entities';

const Page_linksView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const pageLinksState = useAppSelector((state) => state.page_links);
  const page_links = pageLinksState.page_links as
    | PageLink
    | PageLink[]
    | undefined;
  const pageLink = Array.isArray(page_links) ? page_links[0] : page_links;

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

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

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

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

            <p>
              {typeof pageLink?.source_page === 'object' &&
              pageLink?.source_page
                ? (pageLink.source_page as { name?: string }).name
                : 'No data'}
            </p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Topage</p>

            <p>
              {typeof pageLink?.target_page === 'object' &&
              pageLink?.target_page
                ? (pageLink.target_page as { name?: string }).name
                : 'No data'}
            </p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Direction</p>
            <p>
              {(pageLink as unknown as { direction?: string })?.direction ??
                'No data'}
            </p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>ExternalURL</p>
            <p>
              {(pageLink as unknown as { external_url?: string })?.external_url}
            </p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Transition</p>

            <p>
              {typeof pageLink?.transition === 'object' && pageLink?.transition
                ? (pageLink.transition as { name?: string }).name
                : 'No data'}
            </p>
          </div>

          <FormField label='Isactive'>
            <SwitchField
              field={{
                name: 'is_active',
                value: (pageLink as unknown as { is_active?: boolean })
                  ?.is_active,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Triggerselector</p>
            <p>
              {
                (pageLink as unknown as { trigger_selector?: string })
                  ?.trigger_selector
              }
            </p>
          </div>

          <BaseDivider />

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

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

export default Page_linksView;
