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/matches/matchesSlice'
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 MatchesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { matches } = useAppSelector((state) => state.matches)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>GameCode</p>
                    <p>{matches?.game_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Host</p>
                    
                    
                        <p>{matches?.host?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Quiz</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{matches?.quiz?.title ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsLocked'>
                    <SwitchField
                      field={{name: 'is_locked', value: matches?.is_locked}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MaxPlayers</p>
                  <p>{matches?.max_players || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>QuestionCount</p>
                  <p>{matches?.question_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='RandomizeQuestions'>
                    <SwitchField
                      field={{name: 'randomize_questions', value: matches?.randomize_questions}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='RandomizeAnswers'>
                    <SwitchField
                      field={{name: 'randomize_answers', value: matches?.randomize_answers}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>WheelEventEveryNQuestions</p>
                  <p>{matches?.wheel_event_every_n_questions || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CatInteractionEveryNQuestions</p>
                  <p>{matches?.cat_interaction_every_n_questions || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>StartingSpeed(km/h)</p>
                  <p>{matches?.starting_speed_kmh || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RaceMap</p>
                    <p>{matches?.race_map ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Match_players Match</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>DisplayName</th>
                                
                                
                                
                                
                                
                                <th>PlayerStatus</th>
                                
                                
                                
                                <th>ScorePoints</th>
                                
                                
                                
                                <th>CorrectCount</th>
                                
                                
                                
                                <th>IncorrectCount</th>
                                
                                
                                
                                <th>YUXEarned</th>
                                
                                
                                
                                <th>RaceDistance(km)</th>
                                
                                
                                
                                <th>JoinedAt</th>
                                
                                
                                
                                <th>LeftAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {matches.match_players_match && Array.isArray(matches.match_players_match) &&
                              matches.match_players_match.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/match_players/match_players-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="display_name">
                                        { item.display_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="player_status">
                                        { item.player_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="score_points">
                                        { item.score_points }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="correct_count">
                                        { item.correct_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="incorrect_count">
                                        { item.incorrect_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="yux_earned">
                                        { item.yux_earned }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="race_distance_km">
                                        { item.race_distance_km }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="joined_at">
                                        { dataFormatter.dateTimeFormatter(item.joined_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="left_at">
                                        { dataFormatter.dateTimeFormatter(item.left_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!matches?.match_players_match?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Match_questions Match</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>QuestionIndex</th>
                                
                                
                                
                                <th>RevealedAt</th>
                                
                                
                                
                                <th>ClosedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {matches.match_questions_match && Array.isArray(matches.match_questions_match) &&
                              matches.match_questions_match.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/match_questions/match_questions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="question_index">
                                        { item.question_index }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="revealed_at">
                                        { dataFormatter.dateTimeFormatter(item.revealed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_at">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!matches?.match_questions_match?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Wheel_events Match</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>TriggerQuestionIndex</th>
                                
                                
                                
                                <th>RewardType</th>
                                
                                
                                
                                <th>MultiplierValue</th>
                                
                                
                                
                                <th>BonusPoints</th>
                                
                                
                                
                                <th>BonusYUX</th>
                                
                                
                                
                                <th>MusicTrack</th>
                                
                                
                                
                                <th>EventStartedAt</th>
                                
                                
                                
                                <th>EventEndedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {matches.wheel_events_match && Array.isArray(matches.wheel_events_match) &&
                              matches.wheel_events_match.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/wheel_events/wheel_events-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="trigger_question_index">
                                        { item.trigger_question_index }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reward_type">
                                        { item.reward_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="multiplier_value">
                                        { item.multiplier_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bonus_points">
                                        { item.bonus_points }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bonus_yux">
                                        { item.bonus_yux }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="music_track">
                                        { item.music_track }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="event_started_at">
                                        { dataFormatter.dateTimeFormatter(item.event_started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="event_ended_at">
                                        { dataFormatter.dateTimeFormatter(item.event_ended_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!matches?.wheel_events_match?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default MatchesView;