<?php
/**
 * Yt_EVL_Source
 * 
 * Shared Source functions for setting source-level properties
 * 
 * @package Yt_Evolution
 * @subpackage Classes
 * @author  Chris Derenberger <https://www.ytevolution.com>
 * @copyright Copyright 2020, YTEVOLUTION®
 * @link https://www.ytevolution.com
 * @since 1.0.0
 */
 


class Yt_EVL_Source {
  
  public static $instance;
  
  public $ID;
  public $autosync;
  public $guid;
  public $site;
  public $feed_type;
  public $title;
  public $image;
  public $external_url;
  public $term_ids;
  
  public static function init() {
      if ( is_null( $this -> instance ) )
          $this -> instance = new Yt_EVL_Source();
      return $this -> instance;
  }
  
  function __construct( $id = NULL ) {  
    
    if ( $id ):
      $post_id = $id;
    elseif ( is_array($_POST) && array_key_exists('post_ID', $_POST) ):
      $post_id = filter_var($_POST['post_ID'], FILTER_VALIDATE_INT);
    elseif ( is_array($_GET) && array_key_exists('post', $_GET) ):
      $post_id = filter_var($_GET['post'], FILTER_VALIDATE_INT);
    endif;
    
    if ( isset($post_id) ):
      $this -> set_source_properties( $post_id );
    endif;
    
  }


  function set_source_properties( $post_id ){
    
    //print_r($post_id);
    
    // Title (channel name)
    $this -> ID = $post_id;
    
    // Title (channel name)
    $this -> title = get_the_title( $post_id );
    
    $post_meta = get_post_meta( $post_id );
    
    // Site
    $this -> site = $post_meta['tube_source_site'][0];
    
    // Feed Type
    if ( array_key_exists('tube_feed_type', $post_meta) ):
      $this -> feed_type = $post_meta['tube_feed_type'][0];
    endif;
    
    if ( array_key_exists('tube_source_external_guid', $post_meta) ):
      
      // Channel GUID
      $this -> guid =  $post_meta['tube_source_external_guid'][0];
    
    endif;
    
    //print_r($post_meta);
    
    // Image
    $this -> image = get_the_post_thumbnail( $post_id, 'medium', array( 'class' => 'img-responsive' ) );
    
    
    if ( array_key_exists('tube_source_external_url', $post_meta) ):
      
      // Channel GUID
      $this -> external_url =  $post_meta['tube_source_external_url'][0];
    
    endif;
        
    $this -> term_ids = NULL;
    
    if ( array_key_exists('tube_source_term_ids', $post_meta) && ! empty( $post_meta['tube_source_term_ids'][0] ) ):
      
        // get a string of IDs
        $this -> term_ids = $post_meta['tube_source_term_ids'][0];
        
       // get_post_meta( $source_post_id, 'tube_source_term_ids', true );
        
        //if ( $source_term_ids ):
          
          // make sure they're integers
          //$source_term_ids = array_map( 'intval', $source_term_ids );
          
          // make it a string
          //$source_term_ids_list = ( $source_term_ids ) ? implode( ',', $source_term_ids ) : '';
        
        //endif;
    
    endif;   
        
    // Channel Autosync Setting
    $this -> autosync = array_key_exists( 'tube_source_autosync', $post_meta );
    
  }
  
  
 
  // Attempt to get new videos for import based on last import date
  // This is part of the "Auto Sync" capability
  
  function get_new_videos_for_import() {
    
   // wp_mail( EMAILADDRESSHERE, 'importnew', 'importnew');
    
    // Set timezone to LA for YouTube HQ
    //$timezone = 'America/Los_Angeles';      
    $timezone = 'UTC';     
    
    // THIS IS JUST FOR TESTING
    //$date_old = new DateTime( "30 days ago", new DateTimeZone($timezone) );
    //update_post_meta( $this -> ID, 'tube_source_last_import_date', $date_old);    
    
    
    // get the current time
    $current_date_obj = new DateTime( "now", new DateTimeZone($timezone) );    
    
    // Switch to ATOM format per YouTube specs
    //$current_date = $current_date_obj->format($current_date_obj::RFC3339);
    
    $current_date = $current_date_obj->format('Y-m-d\TH:i:s\Z'); // force solid hour, no mins secs
     
    //print_r( $current_date_obj );
    //print_r( $current_date );
     
    
    // get last import date    
    $last_import_date_obj = get_post_meta( $this -> ID, 'tube_source_last_import_date', true);
    
    
    // no previous imports, so set last import attempt to current and return
    if ( ! $last_import_date_obj ):  
        
      // update the last import date to current date
      update_post_meta( $this -> ID, 'tube_source_last_import_date', $current_date_obj);
    
      return;
      
    endif;
    
    // Switch to ATOM format per YouTube specs
    //$last_import_date = $last_import_date_obj->format($last_import_date_obj::RFC3339); 
    $last_import_date = $last_import_date_obj->format('Y-m-d\TH:i:s\Z'); 
    
    // calculate the hours since last import
    $date_diff = $current_date_obj->diff($last_import_date_obj);

    $hours_diff = $date_diff->h;
    
    $hours_diff = $hours_diff + ($date_diff->days * 24);    
    
    // less than an hour, so do nothing
    if ( $hours_diff < 1 ):
      //print_r(' $last_import_date == $current_date ');
      return;
    endif;
            
        
    //$last_import_date = str_replace('+00:00', 'Z', $last_import_date);
      $tube_feed_type = get_post_meta( $this -> ID, 'tube_feed_type', true);
    // get the new videos
       if($tube_feed_type == 'youtube-playlist'){
          $token = get_post_meta( $this -> ID, 'tube_next_page_token', true);
       
         $new_videos = $this -> get_source_videos_since_last_import( $last_import_date, $token );
      if(!empty($new_videos))   update_post_meta( $this -> ID, 'tube_next_page_token', $new_videos['next_page_token']);
     }else{
          $new_videos = $this -> get_source_videos_since_last_import( $last_import_date );
     }
   
    
    //print_r( $new_videos );
        
    // update the last import date to current date
    update_post_meta( $this -> ID, 'tube_source_last_import_date', $current_date_obj);
    
    return $new_videos;
          
  }

  

}