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/artists/artistsSlice'
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 ArtistsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { artists } = useAppSelector((state) => state.artists)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ImageSource</p>
                    <p>{artists?.image_source ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ImageUrl</p>
                    <p>{artists?.image_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ExternalUrl</p>
                    <p>{artists?.external_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{artists?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Beatmaps Artist</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>OsuBeatmapNumeric</th>
                                
                                
                                
                                <th>DifficultyName</th>
                                
                                
                                
                                <th>Mode</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Playcount</th>
                                
                                
                                
                                <th>Passcount</th>
                                
                                
                                
                                <th>StarRating</th>
                                
                                
                                
                                <th>Bpm</th>
                                
                                
                                
                                <th>LengthSeconds</th>
                                
                                
                                
                                <th>Cs</th>
                                
                                
                                
                                <th>Ar</th>
                                
                                
                                
                                <th>Od</th>
                                
                                
                                
                                <th>Hp</th>
                                
                                
                                
                                <th>LastSyncedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {artists.beatmaps_artist && Array.isArray(artists.beatmaps_artist) &&
                              artists.beatmaps_artist.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/beatmaps/beatmaps-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="osu_beatmap_numeric">
                                        { item.osu_beatmap_numeric }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="difficulty_name">
                                        { item.difficulty_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mode">
                                        { item.mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="playcount">
                                        { item.playcount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="passcount">
                                        { item.passcount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="star_rating">
                                        { item.star_rating }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bpm">
                                        { item.bpm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="length_seconds">
                                        { item.length_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cs">
                                        { item.cs }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ar">
                                        { item.ar }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="od">
                                        { item.od }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="hp">
                                        { item.hp }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_synced_at">
                                        { dataFormatter.dateTimeFormatter(item.last_synced_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!artists?.beatmaps_artist?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Artist_stats Artist</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>TotalPlays</th>
                                
                                
                                
                                <th>RankedLovedMapsCount</th>
                                
                                
                                
                                <th>ComputedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {artists.artist_stats_artist && Array.isArray(artists.artist_stats_artist) &&
                              artists.artist_stats_artist.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/artist_stats/artist_stats-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="total_plays">
                                        { item.total_plays }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ranked_loved_maps_count">
                                        { item.ranked_loved_maps_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="computed_at">
                                        { dataFormatter.dateTimeFormatter(item.computed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!artists?.artist_stats_artist?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default ArtistsView;