<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;

use EcafeAddons\EcafeElementLoad;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class Ecafe_Sticky_Video extends Widget_Base {

	public function get_name() {
		return 'ecafe-sticky-video';
	}

	public function get_title() {
		return esc_html__( 'Sticky Custom Video', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-sticky-video-icon';
	}

	public function get_categories() {
		return array( 'essential' );
	}

	public function get_keywords() {
		return array( 'video', 'sticky video', 'video sticky' );
	}

	public function get_script_depends() {
		return array( 'ecafe-sticky-video' );
	}

	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}

	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	protected function register_controls() {

		$this->start_controls_section(
			'content_section',
			array(
				'label' => esc_html__( 'Sticky Video', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'externalurl',
			array(
				'label' => esc_html__( 'External URL', 'essential-classy-addons-for-elementor' ),
				'type'  => Controls_Manager::SWITCHER,
			)
		);
		$this->add_control(
			'customURLU',
			array(
				'label'       => esc_html__( 'Video File', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::MEDIA,
				'media_types' => array(
					'video',
				),
				'condition'   => array(
					'externalurl!' => 'yes',
				),
			)
		);
		$this->add_control(
			'customURLI',
			array(
				'label'       => esc_html__( 'Video URL', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'label_block' => true,
				'condition'   => array(
					'externalurl' => 'yes',
				),
			)
		);
		$this->add_responsive_control(
			'videoWidth',
			array(
				'label'      => esc_html__( 'Width', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min'  => 1,
						'max'  => 500,
						'step' => 1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 300,
				),
				'selectors'  => array(
					'{{WRAPPER}} .ec-sticky-video-wrapper .ec-sticky-video.ec-video-stick' => 'width:{{SIZE}}{{UNIT}};',
				),
				'separator'  => 'before',
			)
		);
		$this->add_responsive_control(
			'videoBottom',
			array(
				'label'      => esc_html__( 'Bottom Offset', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min'  => 1,
						'max'  => 200,
						'step' => 1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 20,
				),
				'selectors'  => array(
					'{{WRAPPER}} .ec-sticky-video-wrapper .ec-sticky-video.ec-video-stick' => 'bottom:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'videoRight',
			array(
				'label'      => esc_html__( 'Right Offset', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min'  => 1,
						'max'  => 200,
						'step' => 1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 20,
				),
				'selectors'  => array(
					'{{WRAPPER}} .ec-sticky-video-wrapper .ec-sticky-video.ec-video-stick' => 'right:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'videoBorder',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-sticky-video-wrapper .ec-sticky-video .ec-custom-video',
			)
		);
		$this->add_responsive_control(
			'videoBr',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-sticky-video-wrapper .ec-sticky-video .ec-custom-video' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdoclink = esc_url( 'https://ecaddons.com/docs/sticky-video-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';
	}

	protected function render() {

		$settings            = $this->get_settings_for_display();
		$output              = '<div class="ec-sticky-video-wrapper">';
			$output         .= '<div class="ec-sticky-video">';
				$externalurl = isset( $settings['externalurl'] ) ? $settings['externalurl'] : 'no';
		if ( isset( $externalurl ) && $externalurl == 'yes' ) {
			$customURLSrc = ! empty( $settings['customURLI'] ) ? $settings['customURLI'] : '';
		} else {
			$customURLSrc = ! empty( $settings['customURLU']['url'] ) ? $settings['customURLU']['url'] : '';
		}

		if ( ! empty( $customURLSrc ) ) {
			$output .= '<video class="ec-custom-video" controls> <source src="' . esc_url( $customURLSrc ) . '"></video>';
		}
			$output .= '</div>';
			$output .= '</div>';

			echo wp_kses_normalize_entities( $output );
	}
}
