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/quizzes/quizzesSlice'
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 QuizzesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { quizzes } = useAppSelector((state) => state.quizzes)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>QuizTitle</p>
                    <p>{quizzes?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>QuizType</p>
                    <p>{quizzes?.quiz_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Owner</p>
                    
                    
                        <p>{quizzes?.owner?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Visibility</p>
                    <p>{quizzes?.visibility ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsVerified'>
                    <SwitchField
                      field={{name: 'is_verified', value: quizzes?.is_verified}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>VerifiedBadgeText</p>
                    <p>{quizzes?.verified_badge_text}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Subject</p>
                    <p>{quizzes?.subject ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AgeGroup</p>
                    <p>{quizzes?.age_group ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Difficulty</p>
                    <p>{quizzes?.difficulty ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='AllowImagesInQuestions'>
                    <SwitchField
                      field={{name: 'allow_images_in_questions', value: quizzes?.allow_images_in_questions}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quiz_questions Quiz</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>TimeLimit(Seconds)</th>
                                
                                
                                
                                <th>Points</th>
                                
                                
                                
                                <th>QuestionType</th>
                                
                                
                                
                                <th>OrderIndex</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {quizzes.quiz_questions_quiz && Array.isArray(quizzes.quiz_questions_quiz) &&
                              quizzes.quiz_questions_quiz.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quiz_questions/quiz_questions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="time_limit_seconds">
                                        { item.time_limit_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="points">
                                        { item.points }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="question_type">
                                        { item.question_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_index">
                                        { item.order_index }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!quizzes?.quiz_questions_quiz?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quiz_tag_links Quiz</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {quizzes.quiz_tag_links_quiz && Array.isArray(quizzes.quiz_tag_links_quiz) &&
                              quizzes.quiz_tag_links_quiz.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quiz_tag_links/quiz_tag_links-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!quizzes?.quiz_tag_links_quiz?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Orbix_quiz_assistant_sessions GeneratedQuiz</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Topic</th>
                                
                                
                                
                                <th>TargetQuestionCount</th>
                                
                                
                                
                                <th>AgeGroup</th>
                                
                                
                                
                                <th>Difficulty</th>
                                
                                
                                
                                <th>IncludeImages</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>FinishedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {quizzes.orbix_quiz_assistant_sessions_generated_quiz && Array.isArray(quizzes.orbix_quiz_assistant_sessions_generated_quiz) &&
                              quizzes.orbix_quiz_assistant_sessions_generated_quiz.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/orbix_quiz_assistant_sessions/orbix_quiz_assistant_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="topic">
                                        { item.topic }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_question_count">
                                        { item.target_question_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="age_group">
                                        { item.age_group }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="difficulty">
                                        { item.difficulty }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="include_images">
                                        { dataFormatter.booleanFormatter(item.include_images) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finished_at">
                                        { dataFormatter.dateTimeFormatter(item.finished_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!quizzes?.orbix_quiz_assistant_sessions_generated_quiz?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Matches Quiz</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>
                            {quizzes.matches_quiz && Array.isArray(quizzes.matches_quiz) &&
                              quizzes.matches_quiz.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>
                        {!quizzes?.matches_quiz?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default QuizzesView;