<?php
/**
 * @package    YouTubeR
 * @license  https://allforjoomla.ru/license
 *
 * Created by Oleg Micriucov for Joomla! 3.x
 * https://allforjoomla.ru
 *
 */
defined( '_JEXEC' ) or die;

class mxYouTuberData{
	
	private $_browser_key = '';
	private $_caching = 1;
	private $_cache_lifetime = 3600;
	private $_controller;	
    const YOUTUBE_API_HOST = 'https://www.googleapis.com/youtube/v3/';
    const CACHE_KEY = 'plg_system_youtuber';
	
	public function __construct($controller){
		$this->_controller = $controller;
		$this->_browser_key = $controller->params->get('googleBrowserKey');
		$this->_caching = (int)$controller->params->get('caching');
		$this->_cache_lifetime = (int)$controller->params->get('cache_lifetime');
	}
	
	public function getVideo($id){
		$response = $this->getData( 'videos' , array(
            'part'          => 'snippet,statistics,contentDetails',
            'maxResults'    => (is_array($id)?count($id):1),
            'id'            => (is_array($id)?implode(',',$id):$id)
        ));

        if( !isset($response->items[0]) ) {
            if(is_array($id)){
				throw new Exception(sprintf(JText::_('PLG_SYSTEM_YOUTUBER_VIDEOS_IDS_NOT_FOUND'), implode(' ,',$id)));
			}
			else{
				throw new Exception(sprintf(JText::_('PLG_SYSTEM_YOUTUBER_VIDEO_ID_NOT_FOUND'), $id));
			}
        }
        
        return (is_array($id)?$response->items:$response->items[0]);
	}
    
    public function getChannelIDByUser($user){
        $response = $this->getData( 'channels' , array(
            'part' => 'id',
            'forUsername' => $user
        ));
        if( !isset($response->items[0]) ) {
            throw new Exception(sprintf(JText::_('PLG_SYSTEM_YOUTUBER_CHANNEL_FOR_USER_NOT_FOUND'), $user));
        }

        return $response->items[0]->id;
	}

	public function getChannel($id){
        $response = $this->getData( 'channels' , array(
            'part' => 'snippet,contentDetails,brandingSettings,statistics',
            'id' => $id
        ));

        if( !isset($response->items[0]) ) {
            throw new Exception(sprintf(JText::_('PLG_SYSTEM_YOUTUBER_CHANNEL_ID_NOT_FOUND'), $id));
        }

        return $response->items[0];
	}
	
	public function getPlaylistItems($id,$limit=20,$pageToken='',$strict=true){
        $params = array(
            'part' => 'snippet',
            'maxResults' => $limit,
            'playlistId' => $id
        );
        if($pageToken != '') {
            $params['pageToken'] = $pageToken;
        }
		
        $response = $this->getData( 'playlistItems' , $params);

        if( $strict && !isset($response->items[0]) ) {
            throw new Exception(sprintf(JText::_('PLG_SYSTEM_YOUTUBER_PLAYLIST_ID_NOT_FOUND'), $id));
        }

        return $response;
	}
	
	public function getPlaylists($channelID){
        $params = array(
            'part' => 'snippet',
            'channelId' => $channelID,
			'maxResults' => 50
        );
        $response = $this->getData( 'playlists' , $params);
		
		if(isset($response->items) && is_array($response->items) && count($response->items)==0){
			return array();
		}
        if( !isset($response->items[0]) ) {
            throw new Exception(sprintf(JText::_('PLG_SYSTEM_YOUTUBER_PLAYLIST_FOR_CHANNEL_ID_NOT_FOUND'), $channelID));
        }

        return $response->items;
	}
	
    private function getRequestURI( $type,$data ){
        $data['key'] = $this->_browser_key;
        return self::YOUTUBE_API_HOST.$type.'?'.http_build_query( $data );
    }

	private function getData($type,$data){
		$cache = JFactory::getCache(self::CACHE_KEY, '');
		$cache->setCaching((bool)$this->_caching);
		$cache->setLifeTime($this->_cache_lifetime);
		$URI = $this->getRequestURI($type,$data);
		$qID = md5($URI);
		$cached = $cache->get( $qID );

		if($this->_caching && $cached){
			return json_decode(base64_decode($cached));
		}
	
		$request = JHttpFactory::getHttp();
		$responce = $request->get($URI,array(
			'Accept' => 'application/json',
			'Referer' => JURI::base(false)
		),10);
		
		if( (int)$responce->code != 200 ) {
			throw new Exception(JText::_('PLG_SYSTEM_YOUTUBER_SERVER_RESPONCE_ERROR').(!empty($responce->body)?'<pre>'.print_r($responce->body,true).'</pre>':''));
		}
		$result = $responce->body;
		
		if($this->_caching){
			$cache->store(base64_encode($result), $qID);
		}
		else{
			$cache->remove($qID);
		}
	
		return json_decode($result);
	}

}