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/asset_variants/asset_variantsSlice';
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 { AssetVariant } from '../../types/entities';

interface AssetVariantData {
  asset?: { name?: string };
  variant_type?: string;
  cdn_url?: string;
  width_px?: number;
  height_px?: number;
  size_mb?: number;
}

const Asset_variantsView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const assetVariantsState = useAppSelector((state) => state.asset_variants);
  const asset_variants = assetVariantsState.asset_variants as
    | AssetVariantData
    | AssetVariantData[]
    | undefined;

  // Get the single entity (not array) for view page
  const entity = Array.isArray(asset_variants)
    ? asset_variants[0]
    : asset_variants;

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

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

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

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

            <p>{entity?.asset?.name ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Varianttype</p>
            <p>{entity?.variant_type ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>CDNURL</p>
            <p>{entity?.cdn_url}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Width(px)</p>
            <p>{entity?.width_px || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Height(px)</p>
            <p>{entity?.height_px || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Size(MB)</p>
            <p>{entity?.size_mb || 'No data'}</p>
          </div>

          <BaseDivider />

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

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

export default Asset_variantsView;
