<?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\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;

class Sifency_Widget_Modal extends Widget_Base {

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

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

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

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

    public function get_script_depends() {
        return [ 'sifency-modal-script' ]; 
    }

    public function get_style_depends() {
        return [ 'sifency-modal-style' ]; 
    }

    protected function get_elementor_templates() {
        $templates = \Elementor\Plugin::$instance->templates_manager->get_source( 'local' )->get_items();
        $options = [ '0' => 'Select Template' ];
        if ( ! empty( $templates ) ) {
            foreach ( $templates as $template ) {
                $options[ $template['template_id'] ] = $template['title'] . ' (' . $template['type'] . ')';
            }
        }
        return $options;
    }

    protected function register_controls() {

        // --- Trigger Button Section ---
        $this->start_controls_section(
            'section_trigger',
            [ 'label' => esc_html__('Trigger Button', 'sifency-addons') ]
        );

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

        $this->add_control(
            'button_icon',
            [
                'label' => esc_html__('Icon', 'sifency-addons'),
                'type' => Controls_Manager::ICONS,
            ]
        );

        $this->add_responsive_control(
            'align',
            [
                'label' => esc_html__( 'Alignment', 'sifency-addons' ),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'left'    => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-text-align-left' ],
                    'center'  => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-text-align-center' ],
                    'right'   => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-text-align-right' ],
                ],
                'selectors' => [
                    '{{WRAPPER}}' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();

        // --- Modal Content Section ---
        $this->start_controls_section(
            'section_content',
            [ 'label' => esc_html__('Modal Content', 'sifency-addons') ]
        );

        $this->add_control(
            'content_source',
            [
                'label' => esc_html__('Content Source', 'sifency-addons'),
                'type' => Controls_Manager::SELECT,
                'default' => 'custom',
                'options' => [
                    'custom' => 'Custom Content',
                    'template' => 'Saved Template',
                ],
            ]
        );

        $this->add_control(
            'custom_content',
            [
                'label' => esc_html__('Content', 'sifency-addons'),
                'type' => Controls_Manager::WYSIWYG,
                'default' => '<p>This is your modal content. You can add text, images, or shortcodes here.</p>',
                'condition' => [ 'content_source' => 'custom' ],
            ]
        );

        $this->add_control(
            'template_id',
            [
                'label' => esc_html__('Select Template', 'sifency-addons'),
                'type' => Controls_Manager::SELECT,
                'options' => $this->get_elementor_templates(),
                'default' => '0',
                'condition' => [ 'content_source' => 'template' ],
            ]
        );

        $this->end_controls_section();

        // --- Modal Settings Section ---
        $this->start_controls_section(
            'section_settings',
            [ 'label' => esc_html__('Modal Settings', 'sifency-addons') ]
        );

        $this->add_responsive_control(
            'modal_width',
            [
                'label' => esc_html__('Modal Width', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%', 'vw'],
                'range' => [
                    'px' => [ 'min' => 200, 'max' => 1200 ],
                    '%' => [ 'min' => 10, 'max' => 100 ],
                ],
                'default' => [ 'unit' => 'px', 'size' => 600 ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-modal-container' => 'width: {{SIZE}}{{UNIT}}; max-width: 95vw;',
                ],
            ]
        );

        $this->add_control(
            'entrance_animation',
            [
                'label' => esc_html__('Animation', 'sifency-addons'),
                'type' => Controls_Manager::SELECT,
                'default' => 'fade',
                'options' => [
                    'fade' => 'Fade In',
                    'zoom' => 'Zoom In',
                    'slide_up' => 'Slide Up',
                    'slide_down' => 'Slide Down',
                ],
            ]
        );

        $this->end_controls_section();

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

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [ 'name' => 'btn_typography', 'selector' => '{{WRAPPER}} .sifency-modal-trigger-btn' ]
        );

        $this->start_controls_tabs('tabs_button_style');

        $this->start_controls_tab( 'tab_btn_normal', [ 'label' => esc_html__('Normal', 'sifency-addons') ] );
        $this->add_control( 'btn_text_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-modal-trigger-btn' => 'color: {{VALUE}};' ] ] );
        $this->add_control( 'btn_bg_color', [ 'label' => 'Background Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-modal-trigger-btn' => 'background-color: {{VALUE}};' ] ] );
        $this->end_controls_tab();

        $this->start_controls_tab( 'tab_btn_hover', [ 'label' => esc_html__('Hover', 'sifency-addons') ] );
        $this->add_control( 'btn_text_color_hover', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-modal-trigger-btn:hover' => 'color: {{VALUE}};' ] ] );
        $this->add_control( 'btn_bg_color_hover', [ 'label' => 'Background Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-modal-trigger-btn:hover' => 'background-color: {{VALUE}};' ] ] );
        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->add_control( 'btn_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px' ], 'selectors' => [ '{{WRAPPER}} .sifency-modal-trigger-btn' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ], 'separator' => 'before' ] );
        $this->add_control( 'btn_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px' ], 'selectors' => [ '{{WRAPPER}} .sifency-modal-trigger-btn' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_modal',
            [
                'label' => esc_html__('Modal Window', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'modal_background',
                'types' => [ 'classic', 'gradient' ],
                'selector' => '{{WRAPPER}} .sifency-modal-container',
            ]
        );

        $this->add_control( 'modal_padding', [ 'label' => 'Content Padding', 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px' ], 'default' => ['top'=>30,'right'=>30,'bottom'=>30,'left'=>30,'unit'=>'px'], 'selectors' => [ '{{WRAPPER}} .sifency-modal-body' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
        $this->add_control( 'modal_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px' ], 'selectors' => [ '{{WRAPPER}} .sifency-modal-container' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
        
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [ 'name' => 'modal_box_shadow', 'selector' => '{{WRAPPER}} .sifency-modal-container' ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_close',
            [
                'label' => esc_html__('Close Button', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control( 'close_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-modal-close' => 'color: {{VALUE}};' ] ] );
        $this->add_control( 'close_color_hover', [ 'label' => 'Hover Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-modal-close:hover' => 'color: {{VALUE}};' ] ] );
        $this->add_responsive_control( 'close_size', [ 'label' => 'Size', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-modal-close' => 'font-size: {{SIZE}}px;' ] ] );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $content_html = '';
        if ( 'template' === $settings['content_source'] && ! empty( $settings['template_id'] ) ) {
            $content_html = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $settings['template_id'] );
        } else {
            $content_html = $settings['custom_content'];
        }

        $anim_class = 'sifency-anim-' . $settings['entrance_animation'];
        
        ?>
        <div class="sifency-modal-widget-wrapper">
            
            <div class="sifency-modal-trigger-btn">
                <?php if ( ! empty( $settings['button_icon']['value'] ) ) : ?>
                    <span class="sifency-btn-icon">
                        <?php \Elementor\Icons_Manager::render_icon( $settings['button_icon'], [ 'aria-hidden' => 'true' ] ); ?>
                    </span>
                <?php endif; ?>
                <span class="sifency-btn-text"><?php echo esc_html( $settings['button_text'] ); ?></span>
            </div>

            <div class="sifency-modal-overlay-wrapper <?php echo esc_attr( $anim_class ); ?>" aria-hidden="true">
                <div class="sifency-modal-backdrop"></div>
                <div class="sifency-modal-container">
                    <button class="sifency-modal-close" aria-label="Close">
                        <i class="eicon-close"></i>
                    </button>
                    <div class="sifency-modal-body">
                        <?php echo esc_html ( $content_html ); ?>
                    </div>
                </div>
            </div>

        </div>
        <?php
    }
}