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/flashcards/flashcardsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";


const FlashcardsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { flashcards } = useAppSelector((state) => state.flashcards)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Deck</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{flashcards?.deck?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={flashcards?.hint} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CardType</p>
                    <p>{flashcards?.card_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SortOrder</p>
                  <p>{flashcards?.sort_order || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsSuspended'>
                    <SwitchField
                      field={{name: 'is_suspended', value: flashcards?.is_suspended}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Spaced_repetition_states Flashcard</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Algorithm</th>
                                
                                
                                
                                <th>Repetitions</th>
                                
                                
                                
                                <th>Lapses</th>
                                
                                
                                
                                <th>EaseFactor</th>
                                
                                
                                
                                <th>IntervalDays</th>
                                
                                
                                
                                <th>Stability</th>
                                
                                
                                
                                <th>Difficulty</th>
                                
                                
                                
                                <th>DueAt</th>
                                
                                
                                
                                <th>LastReviewedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {flashcards.spaced_repetition_states_flashcard && Array.isArray(flashcards.spaced_repetition_states_flashcard) &&
                              flashcards.spaced_repetition_states_flashcard.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/spaced_repetition_states/spaced_repetition_states-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="algorithm">
                                        { item.algorithm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="repetitions">
                                        { item.repetitions }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lapses">
                                        { item.lapses }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ease_factor">
                                        { item.ease_factor }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="interval_days">
                                        { item.interval_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stability">
                                        { item.stability }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="difficulty">
                                        { item.difficulty }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_at">
                                        { dataFormatter.dateTimeFormatter(item.due_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_reviewed_at">
                                        { dataFormatter.dateTimeFormatter(item.last_reviewed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!flashcards?.spaced_repetition_states_flashcard?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Review_events Flashcard</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Rating</th>
                                
                                
                                
                                <th>ResponseTimeMs</th>
                                
                                
                                
                                <th>IsCorrect</th>
                                
                                
                                
                                <th>ReviewedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {flashcards.review_events_flashcard && Array.isArray(flashcards.review_events_flashcard) &&
                              flashcards.review_events_flashcard.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/review_events/review_events-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rating">
                                        { item.rating }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="response_time_ms">
                                        { item.response_time_ms }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_correct">
                                        { dataFormatter.booleanFormatter(item.is_correct) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reviewed_at">
                                        { dataFormatter.dateTimeFormatter(item.reviewed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!flashcards?.review_events_flashcard?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default FlashcardsView;