<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }

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_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_FAQ extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-accordion',
			'title'          => 'Sifency Accordion',
			'icon'           => 'eicon-toggle',
			'category'       => 'sifency-addons-widgets',
			'keywords'       => array( 'accordion', 'faq', 'toggle', 'collapsible' ),
			'script_depends' => array( 'sifency-accordion-script' ),
			'style_depends'  => array( 'sifency-accordion-style', 'sifency-frontend-style', 'sifency-typo-style', 'sifency-icon' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Accordion Items', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_style',
			array(
				'label'        => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'layout-1',
				'options'      => array(
					'layout-1' => esc_html__( 'Layout 1: Minimal Shadow', 'sifency-addons' ),
					'layout-2' => esc_html__( 'Layout 2: Modern Boxed', 'sifency-addons' ),
					'layout-3' => esc_html__( 'Layout 3: Numbered Step', 'sifency-addons' ),
					'layout-4' => esc_html__( 'Layout 4: Iconic Left', 'sifency-addons' ),
					'layout-5' => esc_html__( 'Layout 5: Glassmorphism', 'sifency-addons' ),
				),
				'prefix_class' => 'sifency-accordion-',
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'item_title',
			array(
				'label'       => esc_html__( 'Title', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Accordion Title', 'sifency-addons' ),
				'label_block' => true,
			)
		);
		$repeater->add_control(
			'item_icon',
			array(
				'label'   => esc_html__( 'Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'fas fa-plus',
					'library' => 'fa-solid',
				),
			)
		);
		$repeater->add_control(
			'item_content',
			array(
				'label'   => esc_html__( 'Content', 'sifency-addons' ),
				'type'    => Controls_Manager::WYSIWYG,
				'default' => esc_html__( 'Accordion content goes here. You can add any text or media.', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'accordion_items',
			array(
				'label'       => esc_html__( 'Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'item_title' => 'What is your return policy?' ),
					array( 'item_title' => 'How do I track my order?' ),
				),
				'title_field' => '{{{ item_title }}}',
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_style_indicator',
			array(
				'label' => esc_html__( 'Active Indicator Bar', 'sifency-addons' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'show_indicator',
			array(
				'label'        => esc_html__( 'Show Indicator', 'sifency-addons' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);
		$this->add_control(
			'indicator_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#007bff',
				'selectors' => array(
					'{{WRAPPER}} .sifency-accordion-header::after' => 'background-color: {{VALUE}};',
				),
				'condition' => array( 'show_indicator' => 'yes' ),
			)
		);
		$this->add_responsive_control(
			'indicator_width',
			array(
				'label'      => esc_html__( 'Width', 'sifency-addons' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 20,
					),
				),
				'default'    => array(
					'size' => 4,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-accordion-header::after' => 'width: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array( 'show_indicator' => 'yes' ),
			)
		);
		$this->add_responsive_control(
			'indicator_height',
			array(
				'label'      => esc_html__( 'Height (%)', 'sifency-addons' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-accordion-item.active .sifency-accordion-header::after' => 'height: {{SIZE}}%;',
				),
				'condition'  => array( 'show_indicator' => 'yes' ),
			)
		);
		$this->add_control(
			'indicator_position',
			array(
				'label'     => esc_html__( 'Position', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => array(
					'left'  => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default'   => 'left',
				'condition' => array( 'show_indicator' => 'yes' ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-accordion-header::after' => 'left: auto; right: auto; {{VALUE}}: 0;',
					'{{WRAPPER}} .sifency-accordion-header' => 'padding-{{VALUE}}: calc(20px + {{indicator_width.SIZE}}{{indicator_width.UNIT}});',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'flex_wrapper',
				'label'    => 'Allignment',
				'selector' => '.sifency-advanced-accordion-wrapper',
			)
		);
		$this->add_responsive_control(
			'accordion_gap',
			array(
				'label'      => esc_html__( 'Item Gap', 'sifency-addons' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-advanced-accordion-wrapper' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'          => 'item_style',
				'label'           => 'Accordion Item Wrapper',
				'selector'        => '.sifency-accordion-item',
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
				'hover_selector'  => '.sifency-accordion-item.active',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'          => 'header_area_style',
				'label'           => 'Header Area Wrapper',
				'selector'        => '.sifency-accordion-header',
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
				'hover_selector'  => '.sifency-accordion-item.active .sifency-accordion-header',
				'exclude'         => array( 'dimensions', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'heading_style',
				'label'           => 'Title Style',
				'selector'        => '.sifency-heading-style',
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
				'hover_selector'  => '.sifency-accordion-item.active .sifency-heading-style',
				'exclude'         => array( 'transition', 'bg', 'radius' ),
				'color_only'      => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'content_style',
				'label'           => 'Content Style',
				'selector'        => '.sifency-acc-content-inner',
				'color_only'      => true,
				'target_children' => true,
				'exclude'         => array( 'transition', 'tabs' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'icon_style',
				'label'           => esc_html__( 'Icon Style', 'sifency-addons' ),
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
				'selector'        => '.sifency-acc-icon',
				'hover_selector'  => '.sifency-acc-icon',
				'exclude'         => array( 'transition' ),
				'show_dimensions' => true,
			)
		);

		$this->add_Sifency_Addons_Animation_Controls();
	}

	protected function render() {
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes();
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div class="sifency-advanced-accordion-wrapper" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
			foreach ( $settings['accordion_items'] as $index => $item ) :
				$active_class = ( $index == 0 ) ? 'active' : '';
				$item_count   = $index + 1;
				$item_classes = array( 'sifency-accordion-item', $active_class );
				$item_attrs   = $animation_manager->get_item_attributes( $item_classes );
				?>
				<div <?php echo wp_kses_post( $item_attrs ); ?>>
					<div class="sifency-accordion-header" role="button">
						<?php if ( $settings['layout_style'] === 'layout-3' ) : ?>
							<span class="sifency-acc-number"><?php echo sprintf( '%02d', esc_html( $item_count ) ); ?></span>
						<?php endif; ?>
						
						<span class="sifency-acc-title sifency-heading-style"><?php echo esc_html( $item['item_title'] ); ?></span>
						
						<span class="sifency-acc-icon">
							<?php \Elementor\Icons_Manager::render_icon( $item['item_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</span>
					</div>
					<div class="sifency-accordion-content" style="<?php echo ( $index == 0 ) ? 'display:block;' : 'display:none;'; ?>">
						<div class="sifency-acc-content-inner">
							<?php echo wp_kses_post( $this->parse_text_editor( $item['item_content'] ) ); ?>
						</div>
					</div>
				</div>
			<?php endforeach; ?>
		</div>
		<?php
	}
}
