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/game_sessions/game_sessionsSlice'
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 Game_sessionsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { game_sessions } = useAppSelector((state) => state.game_sessions)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SessionToken</p>
                    <p>{game_sessions?.session_token}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>StartingLives</p>
                  <p>{game_sessions?.starting_lives || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>LivesRemaining</p>
                  <p>{game_sessions?.lives_remaining || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Streak</p>
                  <p>{game_sessions?.streak || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>BestStreak</p>
                  <p>{game_sessions?.best_streak || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TimerProfile</p>
                    <p>{game_sessions?.timer_profile ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ModeSelection</p>
                    <p>{game_sessions?.mode_selection ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>User</p>
                    
                    
                        <p>{game_sessions?.user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_rounds Session</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>
                            {game_sessions.game_rounds_session && Array.isArray(game_sessions.game_rounds_session) &&
                              game_sessions.game_rounds_session.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>
                        {!game_sessions?.game_rounds_session?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Leaderboard_entries Session</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>BoardType</th>
                                
                                
                                
                                <th>SeedKey</th>
                                
                                
                                
                                <th>Score</th>
                                
                                
                                
                                <th>RoundsSurvived</th>
                                
                                
                                
                                <th>BestStreak</th>
                                
                                
                                
                                <th>AchievedAt</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {game_sessions.leaderboard_entries_session && Array.isArray(game_sessions.leaderboard_entries_session) &&
                              game_sessions.leaderboard_entries_session.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/leaderboard_entries/leaderboard_entries-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="board_type">
                                        { item.board_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="seed_key">
                                        { item.seed_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="score">
                                        { item.score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rounds_survived">
                                        { item.rounds_survived }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="best_streak">
                                        { item.best_streak }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="achieved_at">
                                        { dataFormatter.dateTimeFormatter(item.achieved_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!game_sessions?.leaderboard_entries_session?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default Game_sessionsView;