<?php

namespace Webien\Elementor\Extensions;

use Elementor\Element_Base;

/*
 * Adds some attributes to video players
 */

class Videos
{
    function __construct()
    {
        $this->register_hooks();
    }

//-------------------------------------------------------------------//
// Hooks
//-------------------------------------------------------------------//

    private function register_hooks()
    {
        add_filter( 'elementor/widget/render_content', [$this, 'elementor_video_widget'], 10, 2 );

        add_action( 'elementor/element/before_section_end', [ $this, 'register_controls' ], 10, 2 );

        //add_action('elementor/frontend/column/before_render', [$this, 'before_render'], 20, 1);
        //add_action('elementor/frontend/column/after_render', [$this, 'after_render'], 20, 1);

        add_action('elementor/frontend/section/before_render', [$this, 'before_render'], 20, 1); // Use lower prio to not disturb other hooks since we use buffer
        add_action('elementor/frontend/section/after_render', [$this, 'after_render'], 20, 1);

        add_action('elementor/frontend/container/before_render', [$this, 'before_render'], 20, 1); // Use lower prio to not disturb other hooks since we use buffer
        add_action('elementor/frontend/container/after_render', [$this, 'after_render'], 20, 1);

    }

    public function register_controls($element, $section_id )
    {


        if ($section_id !== 'section_background') {
            return;
        }

        $element->add_control(
            'webien_bkg_video_first_frame',
            [
                'label'     => __( 'Background video, first frame' , 'webien' ),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'default' => [
                    'url' => \Elementor\Utils::get_placeholder_image_src(),
                ],
                'description'   => __( 'Shown before video starts streaming.' ),
                'separator' => 'before',
                'condition' => [
                    'background_background' => 'video',
                ],
            ]
        );

    }

    function elementor_video_widget( $widget_content, $widget ) {

        // the widget video-playlist generates the video tag from js som we also update that video tag with js

        if ( 'video' === $widget->get_name() ) {
            $settings = $widget->get_settings();

            // Add attributes to prevent download and changing speed
            if ( ! empty( $settings['video_type'] ) && $settings['video_type'] === 'hosted' ) {
                $widget_content = str_replace('<video ', '<video controlsList="nodownload noplaybackrate"', $widget_content);
            }
        }
        return $widget_content;
    }


    public static function before_render(Element_Base $element)
    {
        $settings = $element->get_settings();
        // If the setting for section link is set, buffer output and modify it on the after_render hook
        if(isset($settings['background_background']) && $settings['background_background'] === 'video'){

            // Start output buffer
            ob_start();
        }
    }


    public static function after_render(Element_Base $element)
    {
        $settings = $element->get_settings();

        if(isset($settings['background_background']) && $settings['background_background'] === 'video'){

            $content = ob_get_clean();

            $poster = '';
            // If we have a first frame, set it as the poster
            $imgId = $settings['webien_bkg_video_first_frame']['id'];

            if(isset($imgId) && $imgId > 0){

                // If webien images exists get url from there, otherwise use the default
                if (class_exists('\Webien\Images\ImageSource')) {
                    $firstFrame = \Webien\Image::render([
                        'imgId' => $imgId,
                        'output' => 'url',
                        'width' => '1000',
                        'ratio' => 'none'
                    ]);
                } else {
                    $firstFrame = wp_get_attachment_image_url($imgId, 'medium');
                }

                if($firstFrame) {
                    $content = str_replace('"elementor-background-video-container"', '"elementor-background-video-container" style="background:url(' . $firstFrame . ') center / cover no-repeat;"', $content);
                }
            }

            $content = str_replace('<video ', '<video controlsList="nodownload noplaybackrate"', $content);

            echo $content;
        }
    }

}
