<?php
/**
* Created by PhpStorm.
* User: lambasoft
* Date: 8/29/17
* Time: 8:09 AM
*/


function getStarRatingHTML($CI,$videoID){
    $rating = $CI->CMS_model->getVideoRating($videoID);
    $HTML = $CI->load->view('includes/star_rating', array("videoID" => $videoID, "rating" => $rating), TRUE);
    return $HTML;
}

$this->load->model('CMS_model');
$videoID = $this->uri->segment(2);

//$_SESSION['validSchoolCode'] = false;
if (!isset($_SESSION['validSchoolCode'])) {
    $_SESSION['validSchoolCode'] = false;
}
$sendGACodeTrigger = false;
if (!empty($_POST['school_code'])) {
    $sendGACodeTrigger = $_POST['school_code'];
    $validCode = $this->CMS_model->validateSchoolCode($_POST['school_code']);
    if ($validCode) {
        $_SESSION['validSchoolCode'] = true;
    }else {
        $_SESSION['validSchoolCodeMsg'] = 'Invalid code.';
    }
}

$this->data['sendGACodeTrigger'] = $sendGACodeTrigger;

if(empty($videoID) || !$this->CMS_model->isVideo($videoID)){
    redirect("home");
}

if (!isset($_SESSION['video_count'][$videoID])) {
    $_SESSION['video_count'][$videoID] = 1;
} else {
    $_SESSION['video_count'][$videoID]++;
}

$video_limit = $this->CMS_model->getViewsLimit();

$this->data['video_limit'] = $video_limit[0]['value'];
$this->data['video'] = $this->CMS_model->getVideo($videoID);
$this->data['video']['categories'] = $this->CMS_model->getVideoCategories($videoID);
$this->data['video']['tags'] = $this->CMS_model->getVideoTags($videoID);

if(empty($this->data['video'])){
    redirect("home");
}

if(!empty($this->input->get("search"))){

}else{
    $this->data['related_videos'] = $this->CMS_model->getVideosByCategories(array_map(function($item){ return $item['id']; },$this->data['video']['categories']));
}


$_count = count($this->data['related_videos']);
for($i=0;$i<$_count;$i++){
    if($this->data['related_videos'][$i]['id'] == $videoID){
        unset($this->data['related_videos'][$i]);
    }
}


$this->data['_categories'] = $this->CMS_model->getCategories();

$this->load->library('session');

if(!empty($this->session->userdata('search_videos'))){
    if(array_filter($this->session->userdata('search_videos'),'is_int')){
        $this->data['related_videos'] = $this->CMS_model->getVideos($this->session->userdata('search_videos'),15);
    }
}

if(!empty($this->session->userdata('search_categories'))){
    $this->data['related_videos'] = array_merge($this->data['related_videos'],$this->CMS_model->getVideosByCategories($this->session->userdata('search_categories')));
}

if(empty($this->data['related_videos'])){
    $this->data['related_videos'] = $this->CMS_model->getVideos();
}

$this->data['related_videos'] = array_slice($this->data['related_videos'],0,15);


//Remove Duplicates
$this->data['related_videos'] = array_map("unserialize", array_unique(array_map("serialize", $this->data['related_videos'])));
//Remove Current Video
for($i =0;$i < count($this->data['related_videos']) - 1 ;$i++){
    if (isset($this->data['related_videos'][$i])) {
        if($this->data['related_videos'][$i]['id'] == $videoID){
            unset($this->data['related_videos'][$i]);
        }
    }
}

$this->data['related_videos'] = array_slice($this->data['related_videos'],0,10);

foreach ($this->data['related_videos'] AS $k=>$v) {
    if ($v['id'] == $videoID) {
        unset($this->data['related_videos'][$k]);
    }
}


$this->data['star_rating'] = getStarRatingHTML($this,$videoID);
$isLoggedIn = FALSE;
if($this->ion_auth->logged_in()){
    $isLoggedIn = TRUE;
    $modalData = array(
        "schools" => $this->CMS_model->getSchools(),
        "videoID" => $videoID
    );
    $this->data['modals'][] = $this->load->view("modals/job_shadowing",$modalData, TRUE);
}

$this->data['isLoggedIn'] = $isLoggedIn;

$_SESSION['fromvideopage'] = "video-playing/$videoID";
$this->data['search_categories'] = $this->session->userdata('search_categories');
