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/analytics_events/analytics_eventsSlice';
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 { hasPermission } from '../../helpers/userPermissions';

const Analytics_eventsView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { analytics_events } = useAppSelector(
    (state) => state.analytics_events,
  );

  const { currentUser } = useAppSelector((state) => state.auth);

  const { id } = router.query;

  function removeLastCharacter(str) {
    console.log(str, `str`);
    return str.slice(0, -1);
  }

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

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

            <p>{analytics_events?.profile_page?.title ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>EventType</p>
            <p>{analytics_events?.event_type ?? 'No data'}</p>
          </div>

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

            <p>{analytics_events?.link?.label ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Referrer</p>
            <p>{analytics_events?.referrer}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>UserAgent</p>
            <p>{analytics_events?.user_agent}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>IPHash</p>
            <p>{analytics_events?.ip_hash}</p>
          </div>

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

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

            <p>{analytics_events?.organizations?.name ?? 'No data'}</p>
          </div>

          <BaseDivider />

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

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

export default Analytics_eventsView;
