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/mappers/mappersSlice'
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 MappersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { mappers } = useAppSelector((state) => state.mappers)
    

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

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>OsuUserNumeric</p>
                  <p>{mappers?.osu_user_numeric || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OsuUsername</p>
                    <p>{mappers?.osu_username}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AvatarUrl</p>
                    <p>{mappers?.avatar_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProfileUrl</p>
                    <p>{mappers?.profile_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BannerUrl</p>
                    <p>{mappers?.banner_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Beatmaps Mapper</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>
                            {mappers.beatmaps_mapper && Array.isArray(mappers.beatmaps_mapper) &&
                              mappers.beatmaps_mapper.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>
                        {!mappers?.beatmaps_mapper?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Mapper_stats Mapper</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>
                            {mappers.mapper_stats_mapper && Array.isArray(mappers.mapper_stats_mapper) &&
                              mappers.mapper_stats_mapper.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/mapper_stats/mapper_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>
                        {!mappers?.mapper_stats_mapper?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default MappersView;