<?php
/**
 * Template Leasons
 */
get_header();
$UlpPublicLesson = new UlpPublicLesson();
$videoLesson = new \Indeed\Ulp\PublicSection\VideoLesson( $UlpPublicLesson->getLessonId() );

if($UlpPublicLesson->getCourseId() != 0){
    $course = new UlpCourse($UlpPublicLesson->getCourseId());
}else{
    $course = new UlpCourse($UlpPublicLesson->getCourseIdByLessionId());
}
// var_dump($course->IsEntrolled());
if(is_user_logged_in() && $course->IsEntrolled()){
        
        /* Added check by ocean to restrict user by accessing other tabs before completing his'her profile information */
        
        $flag = false;
        $user_ID = get_current_user_id();
    $userData = get_user_meta( $user_ID );

//    if(isset($userData['first_name'][0]) && $userData['first_name'][0] != '' &&  isset($userData['last_name'][0]) && $userData['last_name'][0] != '' && isset($userData['driverslicense'][0]) && $userData['driverslicense'][0] != '' && isset($userData['phone'][0]) && $userData['phone'][0] != '' && isset($userData['duidate'][0]) && $userData['duidate'][0] != '' && isset($userData['citation'][0]) && $userData['citation'][0] != '' && isset($userData['hearaboutus'][0]) && $userData['hearaboutus'][0] != '' && isset($userData['courtlocation1'][0]) && $userData['courtlocation1'][0] != '' && isset($userData['dateofbirth'][0]) && $userData['dateofbirth'][0] != ''){
    if(isset($userData['complete_profile'][0]) && $userData['complete_profile'][0] == 1){
        $flag = true;
    }else{
        if(isset($userData['first_name'][0]) && $userData['first_name'][0] != '' &&  isset($userData['last_name'][0]) && $userData['last_name'][0] != '' && isset($userData['driverslicense'][0]) && $userData['driverslicense'][0] != '' && isset($userData['phone'][0]) && $userData['phone'][0] != '' && isset($userData['duidate'][0]) && $userData['duidate'][0] != '' && isset($userData['citation'][0]) && $userData['citation'][0] != '' && isset($userData['hearaboutus'][0]) && $userData['hearaboutus'][0] != '' && isset($userData['courtlocation1'][0]) && $userData['courtlocation1'][0] != '' && isset($userData['dateofbirth'][0]) && $userData['dateofbirth'][0] != ''){
            update_user_meta($user_ID, 'complete_profile', '1');
            $flag = true;
        }else{
            update_user_meta($user_ID, 'complete_profile', '0');
            $flag = false;
        }
    }
    
        $current_url= $_SERVER['REQUEST_URI'];
        
    if(!$flag && !stripos($current_url,"ihc_ap_menu=profile")){
      wp_safe_redirect(get_home_url()."/arizona-dui-account-page/?ihc_ap_menu=profile");
          exit;
    }
    
    if(!$flag){ ?>
        <script>
        setTimeout(function(){
            jQuery('.ihc-ap-wrap h2 strong').css('color', '#dd3333');
        }, 2000);
            setTimeout(function(){ 
                jQuery('.ihc-ap-wrap h2 strong').css('color', '#333333');
            }, 5000);
        </script>
    <?php }
    
    /* Ocean ends */

    
    
}
?>
<?php do_action('ulp_before_single_lesson');?>

<div class="page-content ulp-lesson-wrapp">
  <?php if ($UlpPublicLesson->FeatureImage()):?>
      <div class="ulp-lesson-main-featureimage">
          <img src="<?php echo $UlpPublicLesson->FeatureImage();?>" />
      </div>
  <?php endif;?>
  <?php while (have_posts()):?>
    <?php the_post();?>
    <div class="ulp-lesson-main-title"><?php the_title();?></div>
        
      <?php if ( $videoLesson->isYoutube() ):?>
                  <div class="ulp-lesson-video-warpper">
                      <div id="ulp_youtube_player"></div>
                  </div>
          <?php elseif ( $videoLesson->isVimeo() ):?>
                  <div class="ulp-lesson-video-warpper">
                      <div id="ulp_vimeo_player" allow="autoplay"></div>
                  </div>
          <?php endif;?>
          
      <div class="ulp-lesson-main-content"><?php the_content();?></div>
  <?php endwhile;?>


  <div class="ulp-lesson-navigation-wrapper">
    <?php echo $UlpPublicLesson->Navigation();?>
  </div>
  <div class="ulp-lesson-complete-button-wrapper">
    <?php echo $UlpPublicLesson->CompleteButton();?>
  </div>
  <?php if ($UlpPublicLesson->CoursePermalink()):?>
      <div class="ulp-lesson-course-link">
        <div><?php _e('Back to', 'ulp');?></div>
        <?php echo $UlpPublicLesson->CoursePermalink();?>
      </div>
  <?php endif; ?>
</div>

<!-- Ocean added code for count time user spent on this lesson page -->

<?php 

$duration_value = get_post_meta($UlpPublicLesson->getLessonId(), 'ulp_lesson_duration', TRUE);
$duration_type =  get_post_meta($UlpPublicLesson->getLessonId(), 'ulp_lesson_duration_type', TRUE);

?>

<input type="hidden" id="user_id" value="<?php echo get_current_user_id(); ?>">
<input type="hidden" id="course_id" value="<?php echo $UlpPublicLesson->getCourseId(); ?>">
<input type="hidden" id="item_id" value="<?php echo $UlpPublicLesson->getLessonId(); ?>">
<input type="hidden" id="item_type" value="lesson">
<input type="hidden" id="duration" value="<?php echo $duration_value; ?>">
<input type="hidden" id="duration_type" value="<?php echo $duration_type; ?>">

	
<script type="text/javascript">
  //var timerVar = setInterval(countTimer, 1000);
  var totalSeconds = 0;
  function countTimer() {
       ++totalSeconds;
       var hour = Math.floor(totalSeconds /3600);
       var minute = Math.floor((totalSeconds - hour*3600)/60);
       var seconds = totalSeconds - (hour*3600 + minute*60);
       if(hour < 10)
         hour = "0"+hour;
       if(minute < 10)
         minute = "0"+minute;
       if(seconds < 10)
         seconds = "0"+seconds;

       if(totalSeconds%60 == 0){
        jQuery.ajax({
            type    : "post",
            url     : '<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php',
            data    : {
                       action    : "ulp_user_custom_timer",
                       user_id   : '<?php echo get_current_user_id(); ?>',
                       course_id   : '<?php echo $UlpPublicLesson->getCourseId(); ?>',
                       item_id   : '<?php echo $UlpPublicLesson->getLessonId(); ?>',
                       item_type   : 'lesson',
                       duration   : '<?php echo $duration_value; ?>',
                       duration_type   : '<?php echo $duration_type; ?>',
            },
            success : function (response) {
              
            }
        });
       }
       jQuery("#timer").html(hour + ":" + minute + ":" + seconds);
    }
</script>
<?php do_action( 'ulp_after_single_lesson' );?>
<?php do_action( 'ulp_course_curriculum_item', $UlpPublicLesson->getCourseId(), $UlpPublicLesson->getLessonId() );?>
<?php
global $post;
get_footer();
