<?php

/*
 * Copyright (c) 2018 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 *  contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');
if (!function_exists('get_thumbnail')) {

    function get_thumbnail($url = null, $options = array()) {
        $CI = & get_instance();
        if (!function_exists('img')) {
            $CI->load->helper('html');
        }
        $youtube_id = get_video_id($url);
        $preset = 'sddefault';
        if (!$youtube_id) {
            return false;
        }
        if (isset($options['preset'])) {
            $preset = $options['preset'];
            unset($options['preset']);
        }
        $options['src'] = 'https://img.youtube.com/vi/' . $youtube_id . '/' . $preset . '.jpg';
        $options['class'] = (isset($options['class'])) ? $options['class'] .' youtube-embed-'.$youtube_id : 'youtube-embed-'.$youtube_id;
        return img($options);
    }
}
if(!function_exists('get_video')){
    function get_video($url, $options = array()) {
        $CI = & get_instance();
        $youtube_id = get_video_id($url);
        $img = get_thumbnail($url, $options);
        $playurl = base_url('Media/static/play-button-small.png');
        if (!$youtube_id || !$img) {
            return false;
        }
        $width = (isset($options['width'])) ? $options['width'] : '100%';
        $height = (isset($options['height'])) ? $options['height'] : '300px';
        $CI->load->add_css(<<<CSS
            .youtube-wrapper-{$youtube_id} {position:relative; width:{$width}; height:{$height}; min-height: 250px;}
            .youtube-embed-{$youtube_id} {z-index:1; width:{$width}; height:{$height};}
            .youtube-button{position:absolute; width:100%; height: 100%; top:0; left:0; background-image:url({$playurl}); background-position: center center; background-size: 80px 80px; background-repeat: no-repeat;}
            .youtube-button:hover{opacity:0.8}
            .youtube-wrapper-{$youtube_id} > iframe {z-index:1; width:{$width}; height:{$height};}
CSS
, 'embed');
        $CI->load->add_js(<<<EOD
$(document).ready(function(){
$('#youtube-{$youtube_id}').click(function () {
    var iframe = document.createElement("iframe");
    iframe.setAttribute("frameborder", "0");
    iframe.setAttribute("allowfullscreen", "");
    iframe.setAttribute("src", "https://www.youtube.com/embed/{$youtube_id}?rel=0&showinfo=0&autoplay=1");
    $(this).parent().html(iframe);
});
}); 
EOD
, 'embed');
        return <<<HTML
            <div class="youtube-wrapper-{$youtube_id}">
                {$img}
                <span class="youtube-button" id="youtube-{$youtube_id}"></span>
            </div>
HTML;
    }
}
if(!function_exists('get_video_id')) {
    function get_video_id($url) {
        $youtube_id = null;
        if (strpos($url, 'https://youtu.be/') !== false) {
            $youtube_id = str_replace('https://youtu.be/', '', $url);
        }
        elseif (strpos($url, 'https://www.youtube.com/watch?v=') !== false){
            $youtube_id = str_replace('https://www.youtube.com/watch?v=', '', $url);
        }
        return $youtube_id;
    }
}