<?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\Repeater;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Icon_Style_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Accordion_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Widget_FAQ extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Icon_Style_Controls;
    use Sifency_Accordion_Controls;
    use Sifency_Core_Text_Style;

    public function get_name() {
        return 'sifency-addons-accordion';
    }
    
    public function get_title() {
        return esc_html__('Sifency FAQ Accordion', 'sifency-addons');
    }
    
    public function get_icon() {
        return 'eicon-accordion';
    }
    
    public function get_categories() {
        return ['sifency-addons-widgets'];
    }

    public function get_keywords() {
        return ['faq', 'accordion', 'toggle', 'question', 'answer'];
    }
    
    public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-icon', 'sifency-accordion-style', 'sifency-frontend-style' ];
    }

    public function get_script_depends() {
        return [ 'sifency-accordion-script'];
    }
    protected function register_controls() {
        $this->add_sifency_accordion_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Answer Content Style',
            'selector' => '.sifency-content-style',
            'target_children' => true,
        ]);
        $this->add_sifency_icon_style_controls();
        $this->add_sifency_animation_controls();
    }
    protected function render() {
        $settings = $this->get_settings_for_display();
        if ( empty($settings['faq_items']) ) {
            return;
        }
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-accordion'];
        $this->add_render_attribute('wrapper', [
            'data-multiple-open' => $settings['multiple_open'],
            'data-toggle-speed' => $settings['toggle_speed']['size'] ?? 300,
        ]);

        if ( 'yes' === $settings['enable_schema'] ) {
            $this->add_render_attribute('wrapper', [
                'itemscope' => '',
                'itemtype' => 'https://schema.org/FAQPage'
            ]);
        }
        $animation_wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        $final_wrapper_attrs = $this->get_render_attribute_string( 'wrapper' ) . ' ' . $animation_wrapper_attrs;
        ?>
        <div <?php echo wp_kses_post( $final_wrapper_attrs ); ?>>
            <?php foreach ( $settings['faq_items'] as $index => $item ) :
                $is_active = ( $index === 0 && 'yes' === $settings['first_item_active'] );
                
                $item_key = $this->get_repeater_setting_key('item', 'faq_items', $index);
                $item_classes = ['sifency-accordion-item'];
                if ($is_active) {
                    $item_classes[] = 'active';
                }

                if ( 'yes' === $settings['enable_schema'] ) {
                    $this->add_render_attribute($item_key, [
                        'itemscope' => '',
                        'itemprop' => 'mainEntity',
                        'itemtype' => 'https://schema.org/Question'
                    ]);
                }
                $animation_item_attrs = $animation_manager->get_item_attributes($item_classes);
                $question_key = $this->get_repeater_setting_key('question', 'faq_items', $index);
                $this->add_render_attribute($question_key, 'class', 'sifency-accordion-question');
                $this->add_inline_editing_attributes($question_key, 'basic');
                
                $answer_key = $this->get_repeater_setting_key('answer', 'faq_items', $index);
                $this->add_render_attribute($answer_key, 'class', 'sifency-accordion-answer sifency-content-style');
                $this->add_inline_editing_attributes($answer_key, 'advanced');

                $item_attrs = $this->get_render_attribute_string( $item_key );
                $final_item_attrs = $item_attrs . ' ' . $animation_item_attrs;
                
                ?>
                <div <?php echo wp_kses_post( $final_item_attrs ); ?>>
                    <<?php echo tag_escape($settings['question_html_tag']); ?> <?php echo wp_kses_post ($this->get_render_attribute_string($question_key)); ?>>
                       <span class="sifency-accordion-title">
                            <?php echo wp_kses_post( $item['question'] ); ?>
                        </span>
                        <span class="sifency-accordion-icon">
                            <span class="sifency-accordion-icon-closed sifency-icon-style"><?php \Elementor\Icons_Manager::render_icon( $settings['icon'] ); ?></span>
                            <span class="sifency-accordion-icon-opened sifency-icon-style"><?php \Elementor\Icons_Manager::render_icon( $settings['active_icon'] ); ?></span>
                        </span>
                    </<?php echo tag_escape($settings['question_html_tag']); ?>>
                    
                    <div <?php echo wp_kses_post ($this->get_render_attribute_string($answer_key)); ?>>
                        <?php echo wp_kses_post( $this->parse_text_editor($item['answer']) ); ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        <?php
    }
}