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/beatmaps/beatmapsSlice'
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 BeatmapsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { beatmaps } = useAppSelector((state) => state.beatmaps)
    

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

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>OsuBeatmapNumeric</p>
                  <p>{beatmaps?.osu_beatmap_numeric || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DifficultyName</p>
                    <p>{beatmaps?.difficulty_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Mode</p>
                    <p>{beatmaps?.mode ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Playcount</p>
                  <p>{beatmaps?.playcount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Passcount</p>
                  <p>{beatmaps?.passcount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>StarRating</p>
                  <p>{beatmaps?.star_rating || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Bpm</p>
                  <p>{beatmaps?.bpm || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>LengthSeconds</p>
                  <p>{beatmaps?.length_seconds || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Cs</p>
                  <p>{beatmaps?.cs || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Ar</p>
                  <p>{beatmaps?.ar || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Od</p>
                  <p>{beatmaps?.od || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Hp</p>
                  <p>{beatmaps?.hp || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BeatmapSet</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{beatmaps?.beatmap_set?.title ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Mapper</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{beatmaps?.mapper?.osu_username ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_rounds OptionABeatmap</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>RoundNumber</th>
                                
                                
                                
                                <th>RoundMode</th>
                                
                                
                                
                                <th>State</th>
                                
                                
                                
                                <th>TimeLimitMs</th>
                                
                                
                                
                                <th>PresentedAt</th>
                                
                                
                                
                                <th>AnsweredAt</th>
                                
                                
                                
                                <th>PlayerChoice</th>
                                
                                
                                
                                <th>CorrectChoice</th>
                                
                                
                                
                                <th>IsCorrect</th>
                                
                                
                                
                                <th>LoseReason</th>
                                
                                
                                
                                <th>ValueA</th>
                                
                                
                                
                                <th>ValueB</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {beatmaps.game_rounds_option_a_beatmap && Array.isArray(beatmaps.game_rounds_option_a_beatmap) &&
                              beatmaps.game_rounds_option_a_beatmap.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_rounds/game_rounds-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="round_number">
                                        { item.round_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="round_mode">
                                        { item.round_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="state">
                                        { item.state }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="time_limit_ms">
                                        { item.time_limit_ms }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="presented_at">
                                        { dataFormatter.dateTimeFormatter(item.presented_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="answered_at">
                                        { dataFormatter.dateTimeFormatter(item.answered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="player_choice">
                                        { item.player_choice }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="correct_choice">
                                        { item.correct_choice }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_correct">
                                        { dataFormatter.booleanFormatter(item.is_correct) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lose_reason">
                                        { item.lose_reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_a">
                                        { item.value_a }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_b">
                                        { item.value_b }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!beatmaps?.game_rounds_option_a_beatmap?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                <>
                    <p className={'block font-bold mb-2'}>Game_rounds OptionBBeatmap</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>RoundNumber</th>
                                
                                
                                
                                <th>RoundMode</th>
                                
                                
                                
                                <th>State</th>
                                
                                
                                
                                <th>TimeLimitMs</th>
                                
                                
                                
                                <th>PresentedAt</th>
                                
                                
                                
                                <th>AnsweredAt</th>
                                
                                
                                
                                <th>PlayerChoice</th>
                                
                                
                                
                                <th>CorrectChoice</th>
                                
                                
                                
                                <th>IsCorrect</th>
                                
                                
                                
                                <th>LoseReason</th>
                                
                                
                                
                                <th>ValueA</th>
                                
                                
                                
                                <th>ValueB</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {beatmaps.game_rounds_option_b_beatmap && Array.isArray(beatmaps.game_rounds_option_b_beatmap) &&
                              beatmaps.game_rounds_option_b_beatmap.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_rounds/game_rounds-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="round_number">
                                        { item.round_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="round_mode">
                                        { item.round_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="state">
                                        { item.state }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="time_limit_ms">
                                        { item.time_limit_ms }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="presented_at">
                                        { dataFormatter.dateTimeFormatter(item.presented_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="answered_at">
                                        { dataFormatter.dateTimeFormatter(item.answered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="player_choice">
                                        { item.player_choice }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="correct_choice">
                                        { item.correct_choice }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_correct">
                                        { dataFormatter.booleanFormatter(item.is_correct) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lose_reason">
                                        { item.lose_reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_a">
                                        { item.value_a }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_b">
                                        { item.value_b }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!beatmaps?.game_rounds_option_b_beatmap?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default BeatmapsView;