<?php namespace Webien\Elementor\Widgets\Videos;


class Widget extends \Elementor\Widget_Base
{
    public function get_name()
    {
        return 'webien_videos';
    }

    public function get_title()
    {
        return __('Webien videos', 'webien');
    }

    public function get_icon()
    {
        return 'eicon-video-playlist';
    }

    public function get_categories()
    {
        return ['webien'];
    }

    public function get_script_depends() {
        return ['webien-videos'];
    }

    public function get_style_depends() {
        return [];
    }


    protected function register_controls()
    {

        $this->start_controls_section(
            'section_content',
            [
                'label' => __('Allmänt', 'webien'),
            ]
        );


        $this->add_control(
            'image',
            [
                'label' => esc_html__( 'Välj startbild', 'webien' ),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'media_types' => ['image','svg'],
                'default' => [
                    'url' => \Elementor\Utils::get_placeholder_image_src(),
                ],
            ]
        );

        $this->add_control(
            'video',
            [
                'label' => esc_html__( 'Välj video', 'webien' ),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'media_types' => ['video']
            ]
        );

        $this->add_control(
            'controls',
            [
                'label'     => __( 'Visa kontroller' , 'webien' ),
                'description'     => __( 'Om man väljer att visa kontroller kommer videospelaren fungera som en videospelare och har inte de andra funktioner som finns nedan.' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'no'
            ]
        );

        $this->add_control(
            'trigger',
            [
                'label' => __('Starta video vid', 'webien'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'hover' => __('Hover', 'webien'),
                    'click' => __('Klick', 'webien'),
                    'scroll' => __('Scroll', 'webien'),
                    'load' => __('Sidladdning', 'webien'),
                ],
                'default' => 'hover',
                'condition' => [
                    'controls!' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'loop',
            [
                'label'     => __( 'Loopa video' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'no',
                'condition' => [
                    'controls!' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'pause_leave',
            [
                'label'     => __( 'Pausa hover' , 'webien' ),
                'description'     => __( 'Pausa video om musen lämnar och hover avslutas' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'no',
                'condition' => [
                    'trigger' => 'hover',
                    'controls!' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'reset_leave',
            [
                'label'     => __( 'Reset efter hover' , 'webien' ),
                'description'     => __( 'Spola tillbaka video om musen lämnar och hover avslutas' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'no',
                'condition' => [
                    'trigger' => 'hover',
                    'controls!' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'scroll_offset',
            [
                'label' => esc_html__( 'Starta vid position', 'webien' ),
                'description'     => __( 'Hur långt upp i viewport elementet behöver komma innan video startas.' , 'webien' ),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => [ '%' ],
                'range' => [
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 10,
                ],
                'condition' => [
                    'trigger' => 'scroll',
                    'controls!' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'link',
            [
                'label' => esc_html__( 'Länk', 'webien' ),
                'type' => \Elementor\Controls_Manager::URL,
                'placeholder' => esc_html__( 'Länk..', 'webien' ),
                'default' => [
                    'url' => '',
                    'is_external' => false,
                    'custom_attributes' => '',
                ],
                'condition' => [
                    'controls!' => 'yes',
                ],
            ]
        );

        $this->end_controls_section();


    }


    protected function render()
    {
        $settings = $this->get_settings_for_display();

        $video = $settings['video']['url'];

        // Get dimensions of video file to set container ratio
        $meta = wp_get_attachment_metadata( $settings['video']['id']);
        if ( isset( $meta['width'] ) && isset( $meta['height'] ) ){
            $aspect_ratio = $meta['width'] . ' / ' . $meta['height'];
        } else {
            $aspect_ratio = 'inherit';
        }

        $img = $settings['image']['url'];
        // If image optimizer is present, get a better image with video ratio
        if (class_exists('\Webien\ResponsiveImage')) {
            if ( isset( $meta['width'] ) && isset( $meta['height'] ) ){
                $ratio = $meta['width'] . 'x' . $meta['height'];
            } else {
                $ratio = '16x9';
            }
            $img = \Webien\ResponsiveImage::render([
                'imgId' => $settings['image']['id'],
                'output' => 'url',
                'width' => '640',
                'ratio' => $ratio
            ]);
        }


        ob_start();
        include('view.php');
        $html = ob_get_clean();

        echo $html;
    }


}
