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

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_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Video extends Widget_Base {
    use Sifency_Animation_Controls;

    public function get_name() {
        return 'sifency-widget-video';
    }

    public function get_title() {
        return esc_html__('Sifency Video', 'sifency-addons');
    }

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

    public function get_categories() {
        return ['sifency-addons-widgets'];
    }

    public function get_script_depends() {
        return [ 'sifency-video-script' ];
    }
    public function get_style_depends() {
        return [ 'sifency-video-style' ];
    }

    protected function register_controls() {

        $this->start_controls_section(
            'section_content_video',
            [
                'label' => esc_html__('Video', 'sifency-addons'),
            ]
        );

        $this->add_control(
            'video_source',
            [
                'label' => esc_html__('Source', 'sifency-addons'),
                'type' => Controls_Manager::SELECT,
                'default' => 'youtube',
                'options' => [
                    'youtube' => 'YouTube',
                    'vimeo' => 'Vimeo',
                    'self_hosted' => 'Self Hosted',
                ],
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'youtube_url',
            [
                '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' => [
                    'video_source' => 'youtube',
                ],
            ]
        );

        $this->add_control(
            'vimeo_url',
            [
                'label' => esc_html__('Vimeo URL', 'sifency-addons'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => 'https://vimeo.com/your_video_id',
                'label_block' => true,
                'condition' => [
                    'video_source' => 'vimeo',
                ],
            ]
        );

        $this->add_control(
            'self_hosted_url',
            [
                'label' => esc_html__('Choose Video', 'sifency-addons'),
                'type' => Controls_Manager::MEDIA,
                'media_types' => ['video'],
                'condition' => [
                    'video_source' => 'self_hosted',
                ],
            ]
        );

        $this->add_control(
            'background_image',
            [
                'label' => esc_html__('Background Image', 'sifency-addons'),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => Utils::get_placeholder_image_src(),
                ],
                'separator' => 'before',
                'selectors' => [
                    '{{WRAPPER}} .sifency-video-wrapper' => 'background-image: url({{URL}});',
                ],
            ]
        );

        $this->end_controls_section();

        // --- Content Tab: Play Button ---
        $this->start_controls_section(
            'section_content_button',
            [
                'label' => esc_html__('Play Icon & Text', 'sifency-addons'),
            ]
        );

		$this->add_control(
			'selected_icon',
			[
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type' => Controls_Manager::ICONS,
				'fa4compatibility' => 'icon',
				'default' => [
					'value' => 'fas fa-play',
					'library' => 'fa-solid',
				],
			]
		);

        $this->add_control(
            'button_text',
            [
                'label' => esc_html__('Button Text', 'sifency-addons'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => esc_html__('Play Video', 'sifency-addons'),
            ]
        );

        $this->end_controls_section();

        // --- Content Tab: Video Options ---
        $this->start_controls_section(
            'section_video_options',
            [
                'label' => esc_html__('Video Options', 'sifency-addons'),
            ]
        );

        $this->add_control('autoplay', ['label' => esc_html__('Autoplay', 'sifency-addons'), 'type' => Controls_Manager::SWITCHER]);
        $this->add_control('mute', ['label' => esc_html__('Mute', 'sifency-addons'), 'type' => Controls_Manager::SWITCHER]);
        $this->add_control('loop', ['label' => esc_html__('Loop', 'sifency-addons'), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'video_source!' => 'vimeo' ]]);
        $this->add_control('player_controls', ['label' => esc_html__('Player Controls', 'sifency-addons'), 'type' => Controls_Manager::SWITCHER, 'label_off' => 'Hide', 'label_on' => 'Show', 'default' => 'yes', 'condition' => [ 'video_source!' => 'self_hosted' ]]);

        $this->end_controls_section();

        // --- Style Tab: Wrapper ---
        $this->start_controls_section(
            'section_style_wrapper',
            [
                'label' => esc_html__('Wrapper', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'wrapper_overlay',
            [
                'label' => esc_html__('Background Overlay', 'sifency-addons'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sifency-video-wrapper::before' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'wrapper_height',
            [
                'label' => esc_html__( 'Height', 'sifency-addons' ),
                'type' => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'vh' ],
                'range' => [ 'px' => [ 'min' => 100, 'max' => 1000 ], 'vh' => [ 'min' => 10, 'max' => 100 ] ],
                'default' => ['unit' => 'px', 'size' => 300],
                'selectors' => [ '{{WRAPPER}} .sifency-video-wrapper' => 'height: {{SIZE}}{{UNIT}};' ],
            ]
        );

        $this->add_responsive_control('wrapper_border_radius', ['label' => esc_html__('Border Radius', 'sifency-addons'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => ['{{WRAPPER}} .sifency-video-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};']]);

        $this->end_controls_section();

        // --- Style Tab: Play Button ---
        $this->start_controls_section(
            'section_style_button',
            [
                'label' => esc_html__('Play Button', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control('icon_size', ['label' => esc_html__('Icon Size', 'sifency-addons'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 10, 'max' => 100]], 'selectors' => ['{{WRAPPER}} .sifency-video-button-icon i' => 'font-size: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .sifency-video-button-icon svg' => 'width: {{SIZE}}{{UNIT}};']]);
        $this->add_responsive_control('button_size', ['label' => esc_html__('Button Container Size', 'sifency-addons'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 30, 'max' => 200]], 'selectors' => ['{{WRAPPER}} .sifency-video-button' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};']]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'button_typography', 'selector' => '{{WRAPPER}} .sifency-video-button-text', 'condition' => ['button_text!' => '']]);

        $this->start_controls_tabs('tabs_button_style');
        $this->start_controls_tab('tab_button_normal', ['label' => esc_html__('Normal', 'sifency-addons')]);
        $this->add_control('button_color', ['label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-video-button' => 'color: {{VALUE}};']]);
        $this->add_control('button_bg_color', ['label' => esc_html__('Background', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-video-button' => 'background-color: {{VALUE}};']]);
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'button_border', 'selector' => '{{WRAPPER}} .sifency-video-button']);
        $this->end_controls_tab();
        $this->start_controls_tab('tab_button_hover', ['label' => esc_html__('Hover', 'sifency-addons')]);
        $this->add_control('button_color_hover', ['label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-video-button:hover' => 'color: {{VALUE}};']]);
        $this->add_control('button_bg_color_hover', ['label' => esc_html__('Background', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-video-button:hover' => 'background-color: {{VALUE}};']]);
        $this->add_control('button_border_color_hover', ['label' => esc_html__('Border Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-video-button:hover' => 'border-color: {{VALUE}};']]);
        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->add_responsive_control('button_border_radius', ['label' => esc_html__('Border Radius', 'sifency-addons'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => ['{{WRAPPER}} .sifency-video-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};'], 'separator' => 'before']);

        $this->end_controls_section();
        
        $this->add_sifency_animation_controls();
    }

    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 = [];
        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', [
            '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'), [] ); ?>>
            
            <!-- Trigger Button -->
            <a <?php echo wp_kses ( $this->get_render_attribute_string('button'), [] ); ?>>
                <div class="sifency-video-button-icon">
                    <?php \Elementor\Icons_Manager::render_icon( $settings['selected_icon'], [ '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>

            <!-- Modal Markup (Hidden by default) -->
            <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
    }
}