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/pwa_caches/pwa_cachesSlice';
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 { PwaCache, Project } from '../../types/entities';

const Pwa_cachesView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const pwaCachesState = useAppSelector((state) => state.pwa_caches);
  const pwa_caches = pwaCachesState.pwa_caches as
    | PwaCache
    | PwaCache[]
    | undefined;
  const pwaCache = Array.isArray(pwa_caches) ? pwa_caches[0] : pwa_caches;

  const { id } = router.query as { id?: string };

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

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

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

            <p>
              {typeof pwaCache?.project === 'object' &&
              pwaCache?.project !== null
                ? (pwaCache.project as Project).name
                : 'No data'}
            </p>
          </div>

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

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

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

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

          <FormField label='Generatedat'>
            {(pwaCache as unknown as { generated_at?: string })
              ?.generated_at ? (
              <DatePicker
                dateFormat='yyyy-MM-dd hh:mm'
                showTimeSelect
                selected={
                  (pwaCache as unknown as { generated_at?: string })
                    ?.generated_at
                    ? new Date(
                        dayjs(
                          (pwaCache as unknown as { generated_at?: string })
                            ?.generated_at,
                        ).format('YYYY-MM-DD hh:mm'),
                      )
                    : null
                }
                disabled
              />
            ) : (
              <p>No Generatedat</p>
            )}
          </FormField>

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

          <BaseDivider />

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

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

export default Pwa_cachesView;
