// import PDFF from '@/assets/MemoireFinal.pdf';
import { useEffect, useState } from 'react';
import ChaptersBar from './components/chaptersBar';
// import { Document, Page } from 'react-pdf';
import { ArrowLeft, ArrowRight, FileTextIcon } from 'lucide-react';
import { Link, useParams } from 'react-router-dom';
import { useGetSnai3iCourseByIdQuery } from '@/app/backend/endpoints/courses';
import useTitle from '@/hooks/useTitle';
import Fallback from '@/components/Fallback';
// const course: Snai3iCourseI = {
// course_id: 37,
// title: 'test creqte',
// description: 'test test',
// thumbnail:
// 'https://res.cloudinary.com/dx8l1izei/image/upload/v1716667110/thumbnails/majzalf5tqltzggzs7bz.png',
// totalHours: 1,
// status: 'active',
// createdAt: new Date('2024-05-25T19:59:53.000Z'),
// updatedAt: new Date('2024-05-29T14:06:26.000Z'),
// chaptersCount: 2,
// type: 'snai3i',
// chapters: [
// {
// chapter_id: 50,
// title: 'Untitled Chapter',
// course_id: 37,
// position: 1,
// data: [
// {
// video_id: 49,
// title: 'Untitled Video',
// url: 'https://www.youtube.com/watch?v=I7EaD9GkNaA',
// chapter_id: 50,
// videoLength: 12,
// position: 1,
// },
// {
// document_id: 38,
// title: 'Untitled Document',
// url: 'test',
// chapter_id: 50,
// position: 2,
// },
// ],
// },
// {
// chapter_id: 53,
// title: 'test2',
// course_id: 37,
// position: 2,
// data: [
// {
// document_id: 40,
// title: '13',
// url: 'asdasd',
// chapter_id: 53,
// position: 1,
// },
// ],
// },
// ],
// };
function Snai3iCourse() {
const { courseId } = useParams();
const { data: courseData, isLoading } = useGetSnai3iCourseByIdQuery(
Number(courseId)
);
const course = courseData?.data;
const chapters = course?.chapters;
useTitle(course?.title ?? 'Course');
const [current, setCurrent] = useState({
chapterIndex: 1,
subChapterIndex: 1,
});
const [disabledNext, setDisabledNext] = useState(false);
const [disabledPrev, setDisabledPrev] = useState(false);
const handleSubChapterClick = ({
chapterIndex,
subChapterIndex,
}: {
chapterIndex: number;
subChapterIndex: number;
}) => {
setIsVideoLoading(true);
setCurrent({ chapterIndex, subChapterIndex });
};
const currentContent = chapters
? chapters[current.chapterIndex - 1].data[current.subChapterIndex - 1]
: null;
const currentContentType = (currentContent as VideoI)?.video_id
? 'video'
: 'document';
const getVideoID = (url: string) => {
const videoID = url.split('v=')[1];
return videoID;
};
const [isVideoLoading, setIsVideoLoading] = useState(true);
useEffect(() => {
if (course && chapters) {
if (current.chapterIndex === 1 && current.subChapterIndex === 1) {
setDisabledPrev(true);
} else {
setDisabledPrev(false);
}
if (
current.chapterIndex === chapters.length &&
current.subChapterIndex ===
chapters[current.chapterIndex - 1].data.length
) {
setDisabledNext(true);
} else {
setDisabledNext(false);
}
}
}, [current]);
if (isLoading) {
return <Fallback/>;
}
if (!course) {
return <div>Course not found</div>;
}
return (
<div className="flex flex-row items-start top-0 -mx-12 -mt-12">
<ChaptersBar
thumbnail={course.thumbnail!}
chapters={chapters!}
handleSubChapterClick={handleSubChapterClick}
current={current}
/>
<div className="flex flex-col flex-1 h-fill gap-6 items-center justify-center px-12 pt-12">
<div className="flex flex-row justify-between items-center w-[900px]">
<div className="text-slate-700 text-[26px] font-semibold">
{currentContent?.title}
</div>
<div className="flex flex-row items-center gap-3">
<button
onClick={() => {
setCurrent((prev) => {
setIsVideoLoading(true);
if (prev.subChapterIndex === 1) {
return {
chapterIndex: prev.chapterIndex - 1,
subChapterIndex:
chapters![prev.chapterIndex - 2].data.length,
};
}
return {
chapterIndex: prev.chapterIndex,
subChapterIndex: prev.subChapterIndex - 1,
};
});
}}
disabled={disabledPrev}
className="h-10 px-5 rounded-lg bg-white hover:bg-slate-100 border text-slate-800 transition duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-amber-500 focus:ring-opacity-50 disabled:opacity-50 disabled:cursor-not-allowed"
>
<ArrowLeft />
</button>
<button
onClick={() => {
setIsVideoLoading(true);
setCurrent((prev) => {
if (
prev.subChapterIndex ===
chapters![prev.chapterIndex - 1].data.length
) {
return {
chapterIndex: prev.chapterIndex + 1,
subChapterIndex: 1,
};
}
return {
chapterIndex: prev.chapterIndex,
subChapterIndex: prev.subChapterIndex + 1,
};
});
}}
disabled={disabledNext}
className="h-10 px-5 rounded-lg bg-amber-500 hover:bg-amber-600 text-white transition duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-amber-500 focus:ring-opacity-50 disabled:opacity-50 disabled:cursor-not-allowed"
>
<ArrowRight />
</button>
</div>
</div>
{currentContent && currentContentType === 'video' && (
<>
{isVideoLoading && (
// <div className="flex items-center justify-center w-[900px] h-[506px] bg-slate-100 text-slate-700 text-[26px] font-semibold">
// Loading...
// </div>
<div className="flex items-center justify-center w-[900px] h-[506px] bg-slate-200">
<svg
aria-hidden="true"
className="flex items-center justify-center w-8 h-8 text-secondary animate-spin fill-primary"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
</div>
)}
<div className="flex justify-center md:flex-none">
<iframe
width="900"
height="506"
src={`https://www.youtube.com/embed/${getVideoID(
currentContent.url
)}?&rel=0&loop=1`}
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media;
gyroscope; picture-in-picture;
web-share"
allowFullScreen
// isVideoLoading ? 'hidden' : ''
className={`
${isVideoLoading ? 'hidden' : ''}
transition-opacity duration-300 ease-in-out
`}
onLoad={() => setIsVideoLoading(false)}
></iframe>
</div>
</>
)}
{currentContent && currentContentType === 'document' && (
<div className="text-slate-700 text-[26px] font-semibold w-[900px]">
{/* {currentContent.title} */}
{/* <div className="pdf-viewer">
<Document file={PDFF}>
<Page pageNumber={1} />
</Document>
</div> */}
<div className="flex items-center justify-center w-[900px] h-[506px] bg-slate-200 rounded-md">
<Link
to={currentContent.url}
target="_blank"
rel="noreferrer"
className="bg-primary text-white shadow hover:bg-primary/90 h-12 flex items-center justify-center px-4 rounded-md text-xl"
>
Open PDF
<FileTextIcon className='ml-2 mb-1'/>
</Link>
</div>
</div>
)}
</div>
</div>
);
}
export default Snai3iCourse;