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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Release</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{tracks?.release?.title ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Tracktitle</p>
                    <p>{tracks?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Tracknumber</p>
                  <p>{tracks?.track_number || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Version</p>
                    <p>{tracks?.version}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ISRCcode</p>
                    <p>{tracks?.isrc_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ISRCsource</p>
                    <p>{tracks?.isrc_source ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Audiofile</p>
                    {tracks?.audio_file?.length
                      ? dataFormatter.filesFormatter(tracks.audio_file).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No Audiofile</p>
                    }
                </div>
              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Durationseconds</p>
                  <p>{tracks?.duration_seconds || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SamplerateHz</p>
                  <p>{tracks?.sample_rate_hz || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Bitratekbps</p>
                  <p>{tracks?.bitrate_kbps || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Audioformat</p>
                    <p>{tracks?.audio_format ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Explicitcontent'>
                    <SwitchField
                      field={{name: 'explicit_content', value: tracks?.explicit_content}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={tracks?.lyrics} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Audiofingerprint</p>
                    <p>{tracks?.audio_fingerprint}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Qualitystatus</p>
                    <p>{tracks?.quality_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={tracks?.quality_notes} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Track_contributors Track</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Contributorname</th>
                                
                                
                                
                                <th>Role</th>
                                
                                
                                
                                <th>SpotifyartistURL</th>
                                
                                
                                
                                <th>AppleartistURL</th>
                                
                                
                                
                                <th>Societyidentifier</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tracks.track_contributors_track && Array.isArray(tracks.track_contributors_track) &&
                              tracks.track_contributors_track.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/track_contributors/track_contributors-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="role">
                                        { item.role }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="spotify_artist_url">
                                        { item.spotify_artist_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="apple_artist_url">
                                        { item.apple_artist_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="society_identifier">
                                        { item.society_identifier }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tracks?.track_contributors_track?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Pitch_requests Track</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Pitchtitle</th>
                                
                                
                                
                                
                                
                                <th>Mood</th>
                                
                                
                                
                                <th>Similarartists</th>
                                
                                
                                
                                <th>Achievements</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Submittedat</th>
                                
                                
                                
                                <th>Decidedat</th>
                                
                                
                                
                                <th>Decisionnotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tracks.pitch_requests_track && Array.isArray(tracks.pitch_requests_track) &&
                              tracks.pitch_requests_track.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/pitch_requests/pitch_requests-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="pitch_title">
                                        { item.pitch_title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="mood">
                                        { item.mood }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="similar_artists">
                                        { item.similar_artists }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="achievements">
                                        { item.achievements }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submitted_at">
                                        { dataFormatter.dateTimeFormatter(item.submitted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="decided_at">
                                        { dataFormatter.dateTimeFormatter(item.decided_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="decision_notes">
                                        { item.decision_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tracks?.pitch_requests_track?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Playlist_placements Track</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Placedat</th>
                                
                                
                                
                                <th>Removedat</th>
                                
                                
                                
                                <th>Position</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tracks.playlist_placements_track && Array.isArray(tracks.playlist_placements_track) &&
                              tracks.playlist_placements_track.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/playlist_placements/playlist_placements-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="placed_at">
                                        { dataFormatter.dateTimeFormatter(item.placed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="removed_at">
                                        { dataFormatter.dateTimeFormatter(item.removed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="position">
                                        { item.position }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tracks?.playlist_placements_track?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Royalty_lines Track</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Territory</th>
                                
                                
                                
                                <th>Storename</th>
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>Unitrate</th>
                                
                                
                                
                                <th>Grossamount</th>
                                
                                
                                
                                <th>Netamount</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>Eventat</th>
                                
                                
                                
                                <th>Usagetype</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tracks.royalty_lines_track && Array.isArray(tracks.royalty_lines_track) &&
                              tracks.royalty_lines_track.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/royalty_lines/royalty_lines-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="territory">
                                        { item.territory }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="store_name">
                                        { item.store_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_rate">
                                        { item.unit_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gross_amount">
                                        { item.gross_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="net_amount">
                                        { item.net_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="event_at">
                                        { dataFormatter.dateTimeFormatter(item.event_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="usage_type">
                                        { item.usage_type }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tracks?.royalty_lines_track?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Analytics_daily Track</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Metricdate</th>
                                
                                
                                
                                <th>Streams</th>
                                
                                
                                
                                <th>Uniquelisteners</th>
                                
                                
                                
                                <th>Saves</th>
                                
                                
                                
                                <th>Likes</th>
                                
                                
                                
                                <th>Shares</th>
                                
                                
                                
                                <th>Country</th>
                                
                                
                                
                                <th>City</th>
                                
                                
                                
                                <th>Trafficsource</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tracks.analytics_daily_track && Array.isArray(tracks.analytics_daily_track) &&
                              tracks.analytics_daily_track.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/analytics_daily/analytics_daily-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="metric_date">
                                        { dataFormatter.dateTimeFormatter(item.metric_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="streams">
                                        { item.streams }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unique_listeners">
                                        { item.unique_listeners }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="saves">
                                        { item.saves }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="likes">
                                        { item.likes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shares">
                                        { item.shares }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="country">
                                        { item.country }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="city">
                                        { item.city }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="traffic_source">
                                        { item.traffic_source }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tracks?.analytics_daily_track?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default TracksView;