<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Embed;
use Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Video extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-widget-video',
			'title'          => 'Sifency Video',
			'icon'           => 'eicon-video',
			'category'       => array( 'sifency-addons-widgets' ),
			'category'       => 'sifency-addons-widgets',
			'script_depends' => array( 'sifency-video-script' ),
			'style_depends'  => array( 'sifency-video-style' ),
		);}

	protected function register_controls() {

		$this->start_controls_section(
			'section_content_video',
			array(
				'label' => esc_html__( 'Video', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'video_source',
			array(
				'label'              => esc_html__( 'Source', 'sifency-addons' ),
				'type'               => Controls_Manager::SELECT,
				'default'            => 'youtube',
				'options'            => array(
					'youtube'     => 'YouTube',
					'vimeo'       => 'Vimeo',
					'self_hosted' => 'Self Hosted',
				),
				'frontend_available' => true,
			)
		);
		$this->add_control(
			'youtube_url',
			array(
				'label'       => esc_html__( 'YouTube URL', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'https://www.youtube.com/watch?v=your_video_id',
				'label_block' => true,
				'default'     => 'https://www.youtube.com/watch?v=XHOmBV4js_E',
				'condition'   => array(
					'video_source' => 'youtube',
				),
			)
		);
		$this->add_control(
			'vimeo_url',
			array(
				'label'       => esc_html__( 'Vimeo URL', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'https://vimeo.com/your_video_id',
				'label_block' => true,
				'condition'   => array(
					'video_source' => 'vimeo',
				),
			)
		);
		$this->add_control(
			'self_hosted_url',
			array(
				'label'       => esc_html__( 'Choose Video', 'sifency-addons' ),
				'type'        => Controls_Manager::MEDIA,
				'media_types' => array( 'video' ),
				'condition'   => array(
					'video_source' => 'self_hosted',
				),
			)
		);
		$this->add_control(
			'background_image',
			array(
				'label'     => esc_html__( 'Background Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array(
					'url' => Utils::get_placeholder_image_src(),
				),
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .sifency-video-wrapper' => 'background-image: url({{URL}});',
				),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_content_button',
			array(
				'label' => esc_html__( 'Play Icon & Text', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'selected_icon',
			array(
				'label'            => esc_html__( 'Icon', 'sifency-addons' ),
				'type'             => Controls_Manager::ICONS,
				'fa4compatibility' => 'icon',
				'default'          => array(
					'value'   => 'fas fa-play',
					'library' => 'fa-solid',
				),
			)
		);
		$this->add_control(
			'button_text',
			array(
				'label'       => esc_html__( 'Button Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Play Video', 'sifency-addons' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_video_options',
			array(
				'label' => esc_html__( 'Video Options', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'autoplay',
			array(
				'label' => esc_html__( 'Autoplay', 'sifency-addons' ),
				'type'  => Controls_Manager::SWITCHER,
			)
		);
		$this->add_control(
			'mute',
			array(
				'label' => esc_html__( 'Mute', 'sifency-addons' ),
				'type'  => Controls_Manager::SWITCHER,
			)
		);
		$this->add_control(
			'loop',
			array(
				'label'     => esc_html__( 'Loop', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'condition' => array( 'video_source!' => 'vimeo' ),
			)
		);
		$this->add_control(
			'player_controls',
			array(
				'label'     => esc_html__( 'Player Controls', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'label_off' => 'Hide',
				'label_on'  => 'Show',
				'default'   => 'yes',
				'condition' => array( 'video_source!' => 'self_hosted' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style_wrapper',
			array(
				'label' => esc_html__( 'Wrapper', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'wrapper_overlay',
			array(
				'label'     => esc_html__( 'Background Overlay', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-video-wrapper::before' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'wrapper_height',
			array(
				'label'      => esc_html__( 'Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => 100,
						'max' => 1000,
					),
					'vh' => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 300,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-video-wrapper' => 'height: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();

		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector' => '.sifency-video-button',
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'         => 'button_icon_',
				'label'          => esc_html__( 'Button Icon Style', 'sifency-addons' ),
				'selector'       => '.sifency-video-button-icon i, .sifency-video-button-icon svg',
				'hover_selector' => '.sifency-video-button:hover .sifency-video-button-icon i, .sifency-video-button:hover .sifency-video-button-icon svg',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

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

		$video_type = $settings['video_source'];
		$video_url  = '';
		if ( 'youtube' === $video_type ) {
			$video_url = $settings['youtube_url'];
		} elseif ( 'vimeo' === $video_type ) {
			$video_url = $settings['vimeo_url'];
		} elseif ( 'self_hosted' === $video_type && ! empty( $settings['self_hosted_url']['url'] ) ) {
			$video_url = $settings['self_hosted_url']['url'];
		}

		if ( empty( $video_url ) ) {
			return;
		}

		$embed_params = array();
		if ( $settings['mute'] ) {
			$embed_params['mute'] = '1'; }
		if ( $settings['loop'] ) {
			$embed_params['loop'] = '1'; }
		if ( ! $settings['player_controls'] ) {
			$embed_params['controls'] = '0'; }

		$final_url = $video_url;
		if ( 'self_hosted' !== $video_type ) {
			$final_url = Embed::get_embed_url( $video_url, $embed_params );
		}

		$this->add_render_attribute(
			'wrapper',
			array(
				'class'           => 'sifency-video-wrapper',
				'data-video-type' => $video_type,
				'data-video-url'  => $final_url,
				'data-autoplay'   => $settings['autoplay'] ? 'yes' : 'no',
				'data-mute'       => $settings['mute'] ? 'yes' : 'no',
			)
		);

		$this->add_render_attribute( 'button', 'class', 'sifency-video-button' );
		$this->add_render_attribute( 'button', 'href', '#' );

		?>
		<div <?php echo wp_kses( $this->get_render_attribute_string( 'wrapper' ), array() ); ?>>
			<a <?php echo wp_kses( $this->get_render_attribute_string( 'button' ), array() ); ?>>
				<div class="sifency-video-button-icon">
					<?php \Elementor\Icons_Manager::render_icon( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				</div>
				<?php if ( ! empty( $settings['button_text'] ) ) : ?>
					<div class="sifency-video-button-text"><?php echo esc_html( $settings['button_text'] ); ?></div>
				<?php endif; ?>
			</a>

			<div class="sifency-video-modal" aria-hidden="true">
				<div class="sifency-modal-overlay"></div>
				<div class="sifency-video-modal-container">
					<button class="sifency-video-modal-close" aria-label="Close">
						<i class="eicon-close"></i>
					</button>
					<div class="sifency-video-modal-content">
					</div>
				</div>
			</div>

		</div>
		<?php
	}
}
