<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Digital_Product_Faq extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'digital';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-digital-product-faq',
			'title'          => 'Product FAQ',
			'icon'           => 'eicon-meta-data',
			'category'       => array( 'sifency-digital-components' ),
			'keywords'       => array( 'faq', 'accordion', 'toggle', 'question', 'answer', 'meta', 'sifency' ),
			'style_depends'  => array( 'sifency-digital-style' ),
			'script_depends' => array( 'sifency-digital-script' ),
		);}
	protected function register_controls() {
		$this->start_controls_section( 'section_accordion_content', array( 'label' => esc_html__( 'Accordion', 'sifency-addons' ) ) );
		$this->add_control(
			'icon',
			array(
				'label'   => esc_html__( 'Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'fas fa-chevron-down',
					'library' => 'fa-solid',
				),
			)
		);
		$this->add_control(
			'allow_multiple_open',
			array(
				'label'        => esc_html__( 'Multiple Open Items', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => 'Yes',
				'label_off'    => 'No',
				'return_value' => 'yes',
				'default'      => 'no',
				'description'  => 'Allow multiple FAQ items to be open at the same time.',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_general_style',
			array(
				'label' => esc_html__( 'Accordion', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'accordion_border',
				'selector' => '{{WRAPPER}} .sifency-faq-accordion',
			)
		);
		$this->add_responsive_control(
			'accordion_radius',
			array(
				'label'     => 'Border Radius',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-accordion' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_question_style',
			array(
				'label' => esc_html__( 'Question', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'question_typography',
				'selector' => '{{WRAPPER}} .sifency-faq-question-text',
			)
		);
		$this->start_controls_tabs( 'question_style_tabs' );
		$this->start_controls_tab( 'question_normal', array( 'label' => 'Normal' ) );
		$this->add_control(
			'question_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-question' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'question_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-question' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( 'question_active', array( 'label' => 'Active / Hover' ) );
		$this->add_control(
			'question_active_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-item:hover .sifency-faq-question, {{WRAPPER}} .sifency-faq-item--active .sifency-faq-question' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'question_active_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-item:hover .sifency-faq-question, {{WRAPPER}} .sifency-faq-item--active .sifency-faq-question' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->add_responsive_control(
			'question_padding',
			array(
				'label'     => 'Padding',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-question' => 'padding: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ),
				'separator' => 'before',
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_answer_style',
			array(
				'label' => esc_html__( 'Answer', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'answer_typography',
				'selector' => '{{WRAPPER}} .sifency-faq-answer',
			)
		);
		$this->add_control(
			'answer_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-answer' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'answer_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-answer' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_responsive_control(
			'answer_padding',
			array(
				'label'     => 'Padding',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-answer' => 'padding: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_icon_style',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'icon[value]!' => '' ),
			)
		);
		$this->add_control(
			'icon_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-icon' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'icon_active_color',
			array(
				'label'     => 'Active Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-item--active .sifency-faq-icon' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_responsive_control(
			'icon_size',
			array(
				'label'     => 'Size',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-faq-icon i' => 'font-size: {{SIZE}}px;' ),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		$faq_list = sifency_get_field( 'digital_faq' );

		if ( empty( $faq_list ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$faq_list = array(
					array(
						'faq_question' => 'What is the refund policy?',
						'faq_answer'   => 'We offer a 30-day money-back guarantee on all our products, no questions asked.',
					),
					array(
						'faq_question' => 'Is this a one-time purchase?',
						'faq_answer'   => 'Yes, this product is a one-time purchase. You will receive lifetime updates.',
					),
					array(
						'faq_question' => 'Do you offer customer support?',
						'faq_answer'   => 'Absolutely! We provide premium customer support 24/7. You can reach us via email or our support ticket system.',
					),
				);
			} else {
				return;
			}
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		$this->add_render_attribute( 'accordion', 'class', 'sifency-faq-accordion' );
		$this->add_render_attribute( 'accordion', 'data-allow-multiple', $settings['allow_multiple_open'] );

		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<div <?php echo wp_kses( $this->get_render_attribute_string( 'accordion' ), array() ); ?>>
				<?php
				foreach ( $faq_list as $index => $item ) :
					if ( empty( $item['faq_question'] ) ) {
						continue;
					}

					$item_key = 'item_' . $index;
					$this->add_render_attribute( $item_key, 'class', 'sifency-faq-item' );
					?>
					<div <?php echo wp_kses( $this->get_render_attribute_string( $item_key ), array() ); ?>>
						<div class="sifency-faq-question" role="button" tabindex="0">
							<span class="sifency-faq-question-text"><?php echo esc_html( $item['faq_question'] ); ?></span>
							<?php if ( ! empty( $settings['icon']['value'] ) ) : ?>
								<span class="sifency-faq-icon"><?php Icons_Manager::render_icon( $settings['icon'], array( 'aria-hidden' => 'true' ) ); ?></span>
							<?php endif; ?>
						</div>
						<div class="sifency-faq-answer">
							<?php
							$answer = isset( $item['faq_answer'] ) ? $item['faq_answer'] : '';
							echo wp_kses_post( wpautop( $answer ) );
							?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}
}
