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/genres/genresSlice'
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 GenresView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { genres } = useAppSelector((state) => state.genres)
    
    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 genres')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View genres')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/genres/genres-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Origin</p>
                    <p>{genres?.origin ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ParentGenre</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{genres?.parent_genre?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TypicalBPMMin</p>
                  <p>{genres?.typical_bpm_min || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TypicalBPMMax</p>
                  <p>{genres?.typical_bpm_max || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{genres?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Projects PrimaryGenre</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ProjectName</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                <th>TargetBPM</th>
                                
                                
                                
                                <th>TargetKey</th>
                                
                                
                                
                                <th>Mood</th>
                                
                                
                                
                                <th>TargetDurationSeconds</th>
                                
                                
                                
                                <th>CollaborationEnabled</th>
                                
                                
                                
                                <th>LastOpenedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {genres.projects_primary_genre && Array.isArray(genres.projects_primary_genre) &&
                              genres.projects_primary_genre.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/projects/projects-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="target_bpm">
                                        { item.target_bpm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_key">
                                        { item.target_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mood">
                                        { item.mood }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_duration_seconds">
                                        { item.target_duration_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="collaboration_enabled">
                                        { dataFormatter.booleanFormatter(item.collaboration_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_opened_at">
                                        { dataFormatter.dateTimeFormatter(item.last_opened_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!genres?.projects_primary_genre?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Songs Genre</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SongTitle</th>
                                
                                
                                
                                <th>ArtistDisplay</th>
                                
                                
                                
                                
                                
                                <th>BPM</th>
                                
                                
                                
                                <th>KeySignature</th>
                                
                                
                                
                                <th>Mood</th>
                                
                                
                                
                                <th>DurationSeconds</th>
                                
                                
                                
                                
                                
                                <th>ExplicitContent</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {genres.songs_genre && Array.isArray(genres.songs_genre) &&
                              genres.songs_genre.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/songs/songs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="artist_display">
                                        { item.artist_display }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="bpm">
                                        { item.bpm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="key_signature">
                                        { item.key_signature }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mood">
                                        { item.mood }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duration_seconds">
                                        { item.duration_seconds }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="explicit_content">
                                        { dataFormatter.booleanFormatter(item.explicit_content) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!genres?.songs_genre?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Generation_requests TargetGenre</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>RequestType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>TargetBPM</th>
                                
                                
                                
                                <th>TargetKey</th>
                                
                                
                                
                                <th>Mood</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>QueuedAt</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>FinishedAt</th>
                                
                                
                                
                                <th>ErrorMessage</th>
                                
                                
                                
                                <th>ComputeSeconds</th>
                                
                                
                                
                                <th>EstimatedCost</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {genres.generation_requests_target_genre && Array.isArray(genres.generation_requests_target_genre) &&
                              genres.generation_requests_target_genre.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/generation_requests/generation_requests-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="request_type">
                                        { item.request_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="target_bpm">
                                        { item.target_bpm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_key">
                                        { item.target_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mood">
                                        { item.mood }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="queued_at">
                                        { dataFormatter.dateTimeFormatter(item.queued_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finished_at">
                                        { dataFormatter.dateTimeFormatter(item.finished_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="compute_seconds">
                                        { item.compute_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="estimated_cost">
                                        { item.estimated_cost }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!genres?.generation_requests_target_genre?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Vocal_processing_presets RecommendedGenre</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>PresetName</th>
                                
                                
                                
                                <th>PresetType</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Intensity</th>
                                
                                
                                
                                <th>IsDefault</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {genres.vocal_processing_presets_recommended_genre && Array.isArray(genres.vocal_processing_presets_recommended_genre) &&
                              genres.vocal_processing_presets_recommended_genre.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/vocal_processing_presets/vocal_processing_presets-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="preset_type">
                                        { item.preset_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="intensity">
                                        { item.intensity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_default">
                                        { dataFormatter.booleanFormatter(item.is_default) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!genres?.vocal_processing_presets_recommended_genre?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Mastering_presets Genre</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>PresetName</th>
                                
                                
                                
                                <th>Target</th>
                                
                                
                                
                                
                                
                                <th>LUFSTarget</th>
                                
                                
                                
                                <th>TruePeak(dB)</th>
                                
                                
                                
                                <th>IsDefault</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {genres.mastering_presets_genre && Array.isArray(genres.mastering_presets_genre) &&
                              genres.mastering_presets_genre.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/mastering_presets/mastering_presets-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target">
                                        { item.target }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="lufs_target">
                                        { item.lufs_target }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="true_peak_db">
                                        { item.true_peak_db }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_default">
                                        { dataFormatter.booleanFormatter(item.is_default) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!genres?.mastering_presets_genre?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Marketplace_listings Genre</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ListingType</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Price</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>PublishedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {genres.marketplace_listings_genre && Array.isArray(genres.marketplace_listings_genre) &&
                              genres.marketplace_listings_genre.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/marketplace_listings/marketplace_listings-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="listing_type">
                                        { item.listing_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="price">
                                        { item.price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="published_at">
                                        { dataFormatter.dateTimeFormatter(item.published_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!genres?.marketplace_listings_genre?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default GenresView;