import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head';
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
import { useRouter } from 'next/router';
import { fetch } from '../../stores/project_audio_tracks/project_audio_tracksSlice';
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 { ProjectAudioTrack } from '../../types/entities';

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

  const project_audio_tracksState = useAppSelector(
    (state) => state.project_audio_tracks,
  );
  const project_audio_tracks =
    project_audio_tracksState.project_audio_tracks as
      | ProjectAudioTrack
      | ProjectAudioTrack[]
      | undefined;
  const item = Array.isArray(project_audio_tracks)
    ? project_audio_tracks[0]
    : project_audio_tracks;

  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 project_audio_tracks')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View project_audio_tracks')}
          main
        >
          <BaseButton
            color='info'
            label='Edit'
            href={`/project_audio_tracks/project_audio_tracks-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'}>Environment</p>
            <p>{item?.environment ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Sourcekey</p>
            <p>{item?.source_key}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Name</p>
            <p>{item?.name}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Slug</p>
            <p>{item?.slug}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>URL</p>
            <p>{item?.url}</p>
          </div>

          <FormField label='Loop'>
            <SwitchField
              field={{ name: 'loop', value: item?.loop }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

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

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

          <FormField label='Isenabled'>
            <SwitchField
              field={{
                name: 'is_enabled',
                value: item?.is_enabled,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

          <BaseDivider />

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

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

export default Project_audio_tracksView;
