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/publish_events/publish_eventsSlice';
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 FormField from '../../components/FormField';
import type { PublishEvent } from '../../types/entities';

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

  const publish_eventsState = useAppSelector((state) => state.publish_events);
  const publish_events = publish_eventsState.publish_events as
    | PublishEvent
    | PublishEvent[]
    | undefined;
  const item = Array.isArray(publish_events)
    ? publish_events[0]
    : publish_events;

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

            <p>
              {typeof item?.project === 'object' && item?.project
                ? item.project.name
                : 'No data'}
            </p>
          </div>

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

            <p>
              {typeof item?.user === 'object' && item?.user
                ? item.user.firstName
                : 'No data'}
            </p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Fromenvironment</p>
            <p>{item?.from_environment ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Toenvironment</p>
            <p>{item?.to_environment ?? 'No data'}</p>
          </div>

          <FormField label='Startedat'>
            {item?.started_at ? (
              <DatePicker
                dateFormat='yyyy-MM-dd hh:mm'
                showTimeSelect
                selected={
                  item.started_at
                    ? new Date(
                        dayjs(item.started_at).format('YYYY-MM-DD hh:mm'),
                      )
                    : null
                }
                disabled
              />
            ) : (
              <p>No Startedat</p>
            )}
          </FormField>

          <FormField label='Finishedat'>
            {item?.finished_at ? (
              <DatePicker
                dateFormat='yyyy-MM-dd hh:mm'
                showTimeSelect
                selected={
                  item.finished_at
                    ? new Date(
                        dayjs(item.finished_at).format('YYYY-MM-DD hh:mm'),
                      )
                    : null
                }
                disabled
              />
            ) : (
              <p>No Finishedat</p>
            )}
          </FormField>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Status</p>
            <p>{item?.status ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Title</p>
            <p>{item?.title ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Description</p>
            <p>{item?.description ?? 'No data'}</p>
          </div>

          <FormField label='Multi Text' hasTextareaHeight>
            <textarea
              className={'w-full'}
              disabled
              value={item?.error_message || ''}
            />
          </FormField>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Pagescopied</p>
            <p>{item?.pages_copied || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Transitionscopied</p>
            <p>{item?.transitions_copied || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Audioscopied</p>
            <p>{item?.audios_copied || 'No data'}</p>
          </div>

          <BaseDivider />

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

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

export default Publish_eventsView;
