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/titles/titlesSlice'
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";


const TitlesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { titles } = useAppSelector((state) => state.titles)
    

    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 titles')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View titles')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/titles/titles-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Type</p>
                    <p>{titles?.title_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OriginalName</p>
                    <p>{titles?.original_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Tagline</p>
                    <p>{titles?.tagline}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Overview</p>
                  {titles.overview
                    ? <p dangerouslySetInnerHTML={{__html: titles.overview}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ContentRating</p>
                    <p>{titles?.content_rating ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OriginalLanguage</p>
                    <p>{titles?.original_language}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RuntimeMinutes</p>
                  <p>{titles?.runtime_minutes || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>NumberofSeasons</p>
                  <p>{titles?.number_of_seasons || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>NumberofEpisodes</p>
                  <p>{titles?.number_of_episodes || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AverageRating</p>
                  <p>{titles?.average_rating || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RatingCount</p>
                  <p>{titles?.rating_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PopularityScore</p>
                  <p>{titles?.popularity_score || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>IMDBCode</p>
                    <p>{titles?.imdb_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PosterImages</p>
                    {titles?.poster_images?.length
                      ? (
                        <ImageField
                          name={'poster_images'}
                          image={titles?.poster_images}
                          className='w-20 h-20'
                        />
                      ) : <p>No PosterImages</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BackdropImages</p>
                    {titles?.backdrop_images?.length
                      ? (
                        <ImageField
                          name={'backdrop_images'}
                          image={titles?.backdrop_images}
                          className='w-20 h-20'
                        />
                      ) : <p>No BackdropImages</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Trending'>
                    <SwitchField
                      field={{name: 'is_trending', value: titles?.is_trending}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Title_genres Title</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.title_genres_title && Array.isArray(titles.title_genres_title) &&
                              titles.title_genres_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/title_genres/title_genres-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.title_genres_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Title_cast Title</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>CharacterName</th>
                                
                                
                                
                                <th>BillingOrder</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.title_cast_title && Array.isArray(titles.title_cast_title) &&
                              titles.title_cast_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/title_cast/title_cast-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="character_name">
                                        { item.character_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="billing_order">
                                        { item.billing_order }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.title_cast_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Title_crew Title</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Department</th>
                                
                                
                                
                                <th>Job</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.title_crew_title && Array.isArray(titles.title_crew_title) &&
                              titles.title_crew_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/title_crew/title_crew-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="department">
                                        { item.department }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="job">
                                        { item.job }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.title_crew_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Videos Title</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>VideoType</th>
                                
                                
                                
                                <th>Name</th>
                                
                                
                                
                                <th>VideoURL</th>
                                
                                
                                
                                <th>Site</th>
                                
                                
                                
                                <th>ExternalKey</th>
                                
                                
                                
                                <th>Size</th>
                                
                                
                                
                                <th>PublishedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.videos_title && Array.isArray(titles.videos_title) &&
                              titles.videos_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/videos/videos-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="video_type">
                                        { item.video_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="video_url">
                                        { item.video_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="site">
                                        { item.site }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="external_key">
                                        { item.external_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="size">
                                        { item.size }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="published_at">
                                        { dataFormatter.dateTimeFormatter(item.published_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.videos_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Watchlist_items Title</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>AddedAt</th>
                                
                                
                                
                                <th>WatchedAt</th>
                                
                                
                                
                                <th>SortOrder</th>
                                
                                
                                
                                <th>Note</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.watchlist_items_title && Array.isArray(titles.watchlist_items_title) &&
                              titles.watchlist_items_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/watchlist_items/watchlist_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="added_at">
                                        { dataFormatter.dateTimeFormatter(item.added_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="watched_at">
                                        { dataFormatter.dateTimeFormatter(item.watched_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sort_order">
                                        { item.sort_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="note">
                                        { item.note }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.watchlist_items_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>User_ratings Title</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Rating</th>
                                
                                
                                
                                
                                
                                <th>RatedAt</th>
                                
                                
                                
                                <th>ContainsSpoilers</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.user_ratings_title && Array.isArray(titles.user_ratings_title) &&
                              titles.user_ratings_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/user_ratings/user_ratings-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rating">
                                        { item.rating }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rated_at">
                                        { dataFormatter.dateTimeFormatter(item.rated_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="contains_spoilers">
                                        { dataFormatter.booleanFormatter(item.contains_spoilers) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.user_ratings_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Recommendations SourceTitle</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Score</th>
                                
                                
                                
                                <th>Reason</th>
                                
                                
                                
                                <th>GeneratedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.recommendations_source_title && Array.isArray(titles.recommendations_source_title) &&
                              titles.recommendations_source_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/recommendations/recommendations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="score">
                                        { item.score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reason">
                                        { item.reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="generated_at">
                                        { dataFormatter.dateTimeFormatter(item.generated_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.recommendations_source_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                <>
                    <p className={'block font-bold mb-2'}>Recommendations RecommendedTitle</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Score</th>
                                
                                
                                
                                <th>Reason</th>
                                
                                
                                
                                <th>GeneratedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.recommendations_recommended_title && Array.isArray(titles.recommendations_recommended_title) &&
                              titles.recommendations_recommended_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/recommendations/recommendations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="score">
                                        { item.score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reason">
                                        { item.reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="generated_at">
                                        { dataFormatter.dateTimeFormatter(item.generated_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.recommendations_recommended_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Trending_entries Title</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Window</th>
                                
                                
                                
                                <th>Rank</th>
                                
                                
                                
                                <th>TrendScore</th>
                                
                                
                                
                                <th>AsOfAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {titles.trending_entries_title && Array.isArray(titles.trending_entries_title) &&
                              titles.trending_entries_title.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/trending_entries/trending_entries-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="window">
                                        { item.window }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rank">
                                        { item.rank }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="trend_score">
                                        { item.trend_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="as_of_at">
                                        { dataFormatter.dateTimeFormatter(item.as_of_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!titles?.trending_entries_title?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

TitlesView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated
        
        permission={'READ_TITLES'}
        
      >
          {page}
      </LayoutAuthenticated>
    )
}

export default TitlesView;