<?php 
    if(!isset($_GET["course"]) || empty($_GET["course"])){
        header("Location: /panel/kurslarim");
        exit();
    }
    $guid = htmlspecialchars($_GET["course"]);
    $user_id = $session->data['user_id'];
    $course_details = $course->getUserCourseByGuid($user_id, $guid);
    if(!$course_details){
        header("Location: /panel/kurslarim");
        exit();
    }

    if($course_details['valid_until'] != '0000-00-00') {
        $now = date("Y-m-d H:i");
        $valid_until = date("Y-m-d H:i", strtotime($course_details['valid_until']));
        if($now > $valid_until){
            header("Location: /panel/kurslarim");
            exit();
        }
    }

    $course_videos = $course->getVideos($course_details["id"]);
    $course_sections = $course->getSections($course_details["id"], $user_id);

    if(!$course->isCourseInAccount($course_details["id"], $user_id)){
        header("Location: /panel/kurslarim");
        exit();
    }

    $selected_video = isset($_GET["video"]) && !empty($_GET["video"]) ? htmlspecialchars($_GET["video"]) : 0;
    $selected_video_details = $course->getSelectedVideo($course_details["id"], $selected_video, $user_id);

    $meta_title = $course_details["title"];
    $meta_description = $course_details["description"];

    $data = array();
    $data['title'] = $meta_title . " | " . $App->title;
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $App->version;
    $data['csrf_token'] = $csrf_generator->create();
    $data['sidebar_menu'] = $menu->get(array('position'=>'sidebar'));
    $data['sidebar_active_menu'] = $menu->getActiveMenuItemId(trim($_SERVER['REQUEST_URI']));
    $data['footer']['scripts'] = [];
    
    if($selected_video_details['type'] == 'interactive-video') {
        $data['footer']['scripts'][] = 'https://player.vimeo.com/api/player.js';
        $data['footer']['scripts'][] = $App->url.'/public/panel/assets/js/pages/interactive-video.js';
    }else if($selected_video_details['type'] == 'video') {
        $data['footer']['scripts'][] = 'https://player.vimeo.com/api/player.js';
        $data['footer']['scripts'][] = $App->url.'/public/panel/assets/js/pages/view-course.js';
    }else if($selected_video_details['type'] == 'file') {
        $data['footer']['scripts'][] = $App->url.'/public/panel/assets/js/pages/course-file.js';
    }
    
    $data['cards']["1"] = 0;
    $data['cards']["2"] = 0;
    $data['cards']["3"] = 0;

    $data['results']["1"] = 0;
    $data['results']["2"] = 0;
    $data['results']["3"] = 0;
    $data['results']["4"] = 0;

    $menu_items = $menu->get(array('position'=>'footer'));
    foreach($menu_items as $item){
        if($item['col'] == "1"){
            $data['menu']['footer']["1"][] = $item;
        }
        if($item['col'] == "2"){
            $data['menu']['footer']["2"][] = $item;
        }
        if($item['col'] == "3"){
            $data['menu']['footer']["3"][] = $item;
        }
        if($item['col'] == "4"){
            $data['menu']['footer']["4"][] = $item;
        }
    }
    $data['breadcrumbs'] = array();
    $data['breadcrumbs'][] = array(
        'text' => "Anasayfa",
        'href' => "/panel",
        'index' => 1
    );
    $data['breadcrumbs'][] = array(
        'text' => "Öğrenim İçeriğim",
        'href' => "/panel/kurslarim",
        'index' => 2
    );
?>
<?php include DIR_PUBLIC."panel/parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="card mb-3">
                    <div class="row">
                        <div class="col-md-8 px-md-0">
                            <div class="card-body">
                                <?php if(!empty($selected_video_details)): ?>
                                    <?php if($selected_video_details['type'] == 'video'): ?>
                                        <div class="ratio ratio-16x9 mt-3 mb-5">
                                            <iframe src="<?php echo $selected_video_details['link']; ?>" id="course-video" allow="autoplay" allowfullscreen></iframe>
                                        </div>
                                        <script>
                                            var courseId = '<?php echo $course_details["id"]; ?>';
                                            var videoId = '<?php echo $selected_video_details["id"]; ?>';
                                            var videoProgress = '<?php echo $selected_video_details["progress"]; ?>';
                                        </script>
                                    <?php elseif($selected_video_details['type'] == 'interactive-video'): ?>
                                        <div class="interactive-video mt-3 mb-5">
                                            <div class="interactive-video-wrapper">
                                                <iframe src="<?php echo $selected_video_details['link']; ?>" title="<?php echo $selected_video_details['title']; ?>" id="course-video" allow="autoplay" allowfullscreen></iframe>
                                                <div id="iframe-click-overlay">
                                                    <div class="btn-play">
                                                        <i class="bi bi-play-fill"></i>
                                                    </div>
                                                    <div class="btn-pause">
                                                        <i class="bi bi-pause-fill"></i>
                                                    </div>
                                                </div>
                                                <div class="controls">
                                                    <button class="btn-play" id="btn-play">
                                                        <i class="bi bi-play-fill" id="play-icon"></i>
                                                        <i class="bi bi-pause-fill d-none" id="pause-icon"></i>
                                                    </button>
                                                    <button class="btn-replay d-none" id="btn-replay">
                                                        <i class="bi bi-arrow-clockwise" id="replay-icon"></i>
                                                    </button>
                                                    <div class="time-text">
                                                        <span id="video-time">00:00</span>
                                                        <span id="video-time-current">00:00</span>
                                                    </div>
                                                    <div class="iv-progress" id="video-progress">
                                                        <div id="progress-bar" class="progress-bar" style="width:0%"></div>
                                                        <div id="progress-question-holder">
                                                            
                                                        </div>
                                                        <div class="button-holder">
                                                            <div class="progress-button" style="left:-1.5%"></div>
                                                        </div>
                                                    </div>
                                                    <button class="btn-volume" id="btn-volume">
                                                        <i class="bi bi-volume-mute-fill d-none" id="mute-icon"></i>
                                                        <i class="bi bi-volume-up-fill" id="volume-icon"></i>
                                                    </button>
                                                    <button class="btn-fullscreen" id="btn-fullscreen">
                                                        <i class="bi bi-arrows-fullscreen" id="fullscreen-icon"></i>
                                                        <i class="bi bi-fullscreen-exit d-none" id="fullscreen-exit-icon"></i>
                                                    </button>
                                                </div>
                                                <script>
                                                    var cuePoints = '<?php echo json_encode($selected_video_details['questions']); ?>';
                                                    var courseId = '<?php echo $course_details["id"]; ?>';
                                                    var videoId = '<?php echo $selected_video_details["id"]; ?>';
                                                </script>
                                            </div>
                                        </div>
                                    <?php elseif($selected_video_details['type'] == 'file'): ?>
                                        <div class="mt-3 mb-5">
                                            <iframe src="<?php echo $selected_video_details['link']; ?>" width="100%" height="600" id="file-iframe"></iframe>
                                            <script>
                                                var courseId = '<?php echo $course_details["id"]; ?>';
                                                var fileId = '<?php echo $selected_video_details["id"]; ?>';
                                                var fileProgress = '<?php echo $selected_video_details["progress"]; ?>';
                                            </script>
                                        </div>
                                        
                                    <?php else: ?>

                                    <?php endif; ?>
                                    <div class="row mt-3">
                                        <div class="col-12">
                                            <h5 class="course-video-title" id="course-video-title"><?php echo $selected_video_details['title']; ?></h5>
                                        </div>
                                        <div class="col-12">
                                            <div class="course-video-description" id="course-video-description"><?php echo $selected_video_details['description']; ?></div>
                                        </div>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="col-md-4 course-content-panel px-md-0">
                            <div class="course-content-list" id="course-accordion">
                                <div class="title">
                                    <h5>Kurs İçeriği</h5>
                                </div>
                                <?php 
                                    $i=1;
                                    $prev_video_progress = -1;
                                    foreach($course_sections as $item) {
                                        $in = $utils->is_in_array($item["videos"], "id", $selected_video_details['id']) ? "show" : "";
                                        echo "<div class=\"accordion-item rounded-0\">
                                                    <h2 class=\"accordion-header\">";
                                                        if($in == "show") {
                                                            echo "<button class=\"accordion-button\" type=\"button\" data-bs-toggle=\"collapse\" data-bs-target=\"#course-accordion-content-{$item['id']}\" aria-expanded=\"true\" aria-controls=\"faq-accordion-content-{$item['id']}\">
                                                                    Bölüm {$i}: {$item['title']}
                                                                </button>";
                                                        }else{
                                                            echo "<button class=\"accordion-button collapsed\" type=\"button\" data-bs-toggle=\"collapse\" data-bs-target=\"#course-accordion-content-{$item['id']}\" aria-expanded=\"false\" aria-controls=\"faq-accordion-content-{$item['id']}\">
                                                                    Bölüm {$i}: {$item['title']}
                                                                </button>";
                                                        }
                                        echo        "</h2>
                                                    <div id=\"course-accordion-content-{$item['id']}\" class=\"accordion-collapse collapse $in\" data-bs-parent=\"#course-accordion\">
                                                        <div class=\"accordion-body p-0\">";
                                                            echo "<ul class=\"list-group rounded-0\">";
                                                            $videoCount = count($item['videos']);
                                                            for($j=0; $j<$videoCount; $j++){
                                                                $current_video_progress = $item['videos'][$j]['progress'];
                                                                $nextVideo = ($videoCount-1) != $j ? $item['videos'][$j+1]['id'] : $item['videos'][$j]['id'];
                                                                if($prev_video_progress == -1 || $prev_video_progress == 100) {
                                                                    if($selected_video_details['id'] == $item['videos'][$j]['id']) {
                                                                        echo "<li class=\"list-group-item py-3 active\">
                                                                                <a href=\"".$App->url."/panel/kurslarim/izle?course=".$guid."&video=".$item['videos'][$j]['id']."&next=".$nextVideo."\" aria-current=\"true\"><i class=\"bi bi-play-circle-fill me-2\"></i> ".$item['videos'][$j]['title']."</a>
                                                                                <div class=\"video-progress-bar\"><div class=\"video-progress\" style=\"width:".$item['videos'][$j]['progress']."%\"></div></div>
                                                                            </li>";
                                                                    }else{
                                                                        echo "<li class=\"list-group-item py-3\">
                                                                                <a href=\"".$App->url."/panel/kurslarim/izle?course=".$guid."&video=".$item['videos'][$j]['id']."&next=".$nextVideo."\"><i class=\"bi bi-play-circle-fill me-2\"></i> ".$item['videos'][$j]['title']."</a>
                                                                                <div class=\"video-progress-bar\"><div class=\"video-progress\" style=\"width:".$item['videos'][$j]['progress']."%\"></div></div>
                                                                            </li>";
                                                                    }
                                                                }else{
                                                                    if($selected_video_details['id'] == $item['videos'][$j]['id']) {
                                                                        echo "<li class=\"list-group-item py-3 active\">
                                                                                <a href=\"#\" aria-current=\"true\"><i class=\"bi bi-play-circle-fill me-2\"></i> ".$item['videos'][$j]['title']."</a>
                                                                                <div class=\"video-progress-bar\"><div class=\"video-progress\" style=\"width:".$item['videos'][$j]['progress']."%\"></div></div>
                                                                            </li>";
                                                                    }else{
                                                                        echo "<li class=\"list-group-item py-3\">
                                                                                <a href=\"#\"><i class=\"bi bi-play-circle-fill me-2\"></i> ".$item['videos'][$j]['title']."</a>
                                                                                <div class=\"video-progress-bar\"><div class=\"video-progress\" style=\"width:".$item['videos'][$j]['progress']."%\"></div></div>
                                                                            </li>";
                                                                    }
                                                                }
                                                                
                                                                $prev_video_progress = $current_video_progress;
                                                            }
                                                            echo "</ul>";
                                                            
                                        echo            "</div>
                                                    </div>
                                                </div>";
                                        $i++;
                                    }
                                ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_PUBLIC."panel/parts/footer.php"; ?>