<?php

class Inline_video_yt
{
    private $ci;

    public function __construct()
    {
        $this->ci = get_instance();
        $this->ci->load->model('inlineeditor/inlineeditor_text_tmodel');
        $this->ci->load->library('inlineeditor/inline_base');
    }

    public function parse($text, $i_id = null, $popup = false)
    {
        $text = trim($text);
        $i_id = empty($i_id) ? $this->ci->inlineeditor_text_tmodel->create_i_id($text) : $i_id;

        // Create and redirect if needed
        if ($this->show_edit() && $this->ci->input->get('inline_create') == $i_id) {
            $id = $this->ci->inlineeditor_text_tmodel->add_value('', $i_id);
            redirect($this->generate_url($id));
        }

        $value = $this->ci->inlineeditor_text_tmodel->get_value_by_i_id($i_id);

        $content = [
            'src'      => str_replace('https://youtu.be/', '', (!empty($value['value']) ? $value['value'] : $text)),
            'edit_btn' => '',
        ];

        if ($this->show_edit()) {
            $url = empty($value['id']) ? '?inline_create=' . $i_id : $this->generate_url($value['id']);
            $content['edit_btn'] = $this->ci->inline_base->admin_link($url);
        }

        if ($popup) {
            $this->ci->load->library('js_css')
                ->add_js('https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0/dist/fancybox/fancybox.umd.js')
                ->add_css('https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0/dist/fancybox/fancybox.css')
                ->add_js_string('Fancybox.bind("[data-fancybox]", {});')
                ->add_css_string('.video_yt{width:100%;position:relative}.video_yt iframe{width:100%}.video_yt a{float:left;width:100%;position:relative}.video_yt a .play_ico{position:absolute;width:88px;height:88px;border-radius:50%;background-color:#100f15;left:50%;top:50%;margin:-44px 0 0 -44px}.video_yt a .play_ico i{display:block;position:absolute;left:50%;top:50%;width:0px;height:0px;border-style:solid;border-width:14.5px 0 14.5px 24px;border-color:rgba(0,0,0,0) rgba(0,0,0,0) rgba(0,0,0,0) #fff;transform:rotate(0deg);margin:-14px 0 0 -8px}.video_yt img{float:left;width:100%;height:100%}@media screen and (max-width: 768px){.video_yt a .play_ico{width:60px;height:60px;margin:-30px 0 0 -30px}.video_yt a .play_ico i{border-width:10px 0 10px 15px;margin:-11px 0 0 -5px}}');
            $element = '<a href="https://www.youtube.com/watch?v={src}" data-fancybox><span class="play_ico"><i></i></span><img src="https://i.ytimg.com/vi/'
                . preg_replace('/\?.*/', '', $content['src']) . '/hqdefault.jpg" alt=""></a>';
        } else {
            $element = '<iframe src="https://www.youtube.com/embed/{src}" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
        }

        $template = '<div class="video_yt">' . $element . '{edit_btn}</div>';

        return $this->ci->parser->parse_string($template, $content, true);
    }

    protected function show_edit()
    {
        return $this->ci->inline_base->show_edit();
    }

    protected function generate_url($id)
    {
        return $this->ci->inline_base->generate_url($id, 'yt_video');
    }
}