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_modes/game_modesSlice'
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_modesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { game_modes } = useAppSelector((state) => state.game_modes)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ModeKey</p>
                    <p>{game_modes?.mode_key ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Description</p>
                  {game_modes.description
                    ? <p dangerouslySetInnerHTML={{__html: game_modes.description}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ModeImages</p>
                    {game_modes?.mode_images?.length
                      ? (
                        <ImageField
                          name={'mode_images'}
                          image={game_modes?.mode_images}
                          className='w-20 h-20'
                        />
                      ) : <p>No ModeImages</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsOfficial'>
                    <SwitchField
                      field={{name: 'is_official', value: game_modes?.is_official}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsEnabled'>
                    <SwitchField
                      field={{name: 'is_enabled', value: game_modes?.is_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Matches Mode</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>GameCode</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>IsLocked</th>
                                
                                
                                
                                <th>MaxPlayers</th>
                                
                                
                                
                                <th>QuestionCount</th>
                                
                                
                                
                                <th>RandomizeQuestions</th>
                                
                                
                                
                                <th>RandomizeAnswers</th>
                                
                                
                                
                                <th>WheelEventEveryNQuestions</th>
                                
                                
                                
                                <th>CatInteractionEveryNQuestions</th>
                                
                                
                                
                                <th>StartingSpeed(km/h)</th>
                                
                                
                                
                                <th>RaceMap</th>
                                
                                
                                
                                <th>ScheduledStartAt</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {game_modes.matches_mode && Array.isArray(game_modes.matches_mode) &&
                              game_modes.matches_mode.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/matches/matches-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="game_code">
                                        { item.game_code }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_locked">
                                        { dataFormatter.booleanFormatter(item.is_locked) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_players">
                                        { item.max_players }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="question_count">
                                        { item.question_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="randomize_questions">
                                        { dataFormatter.booleanFormatter(item.randomize_questions) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="randomize_answers">
                                        { dataFormatter.booleanFormatter(item.randomize_answers) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="wheel_event_every_n_questions">
                                        { item.wheel_event_every_n_questions }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cat_interaction_every_n_questions">
                                        { item.cat_interaction_every_n_questions }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="starting_speed_kmh">
                                        { item.starting_speed_kmh }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="race_map">
                                        { item.race_map }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_start_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!game_modes?.matches_mode?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Leaderboard_entries Mode</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Scope</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>RankPosition</th>
                                
                                
                                
                                <th>ScoreValue</th>
                                
                                
                                
                                <th>PeriodStartAt</th>
                                
                                
                                
                                <th>PeriodEndAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {game_modes.leaderboard_entries_mode && Array.isArray(game_modes.leaderboard_entries_mode) &&
                              game_modes.leaderboard_entries_mode.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/leaderboard_entries/leaderboard_entries-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="scope">
                                        { item.scope }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rank_position">
                                        { item.rank_position }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="score_value">
                                        { item.score_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="period_start_at">
                                        { dataFormatter.dateTimeFormatter(item.period_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="period_end_at">
                                        { dataFormatter.dateTimeFormatter(item.period_end_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!game_modes?.leaderboard_entries_mode?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

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

export default Game_modesView;