<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Plugin as ElementorPlugin;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Eltemplate extends Widget_Base {
    use Sifency_Animation_Controls;

    public function get_name() {
        return 'el-template';
    }

    public function get_title() {
        return __('Sifency Elementor Template', 'sifency-addons');
    }

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

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

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

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

    protected function register_controls() {
        $this->start_controls_section(
            'content_section',
            [ 'label' => __('Template', 'sifency-addons') ]
        );

        $this->add_control(
            'template_id',
            [
                'label' => __('Choose Template', 'sifency-addons'),
                'type' => Controls_Manager::SELECT2,
                'label_block' => true,
                'options' => sifency_addons_get_elementor_templates(),
                'description' => __('A list of your saved Elementor templates.', 'sifency-addons'),
            ]
        );

        $this->end_controls_section();
        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();
        $template_id = $settings['template_id'];
        $animation_manager = new Sifency_Animation_Manager($settings);
        if ( empty($template_id) ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                ?>
                <div class="sifency-template-placeholder">
                    <?php esc_html_e( 'Please select an Elementor template from the panel.', 'sifency-addons' ); ?>
                </div>
                <?php
            }
            return;
        }
        $wrapper_classes = ['sifency-el-template-container'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);

        ?>
        <div <?php 
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php
                echo wp_kses_post ( \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template_id ) );
            ?>
        </div>
        <?php
    }
}