<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Image_Accordion extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_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-image-accordion',
			'title'          => 'Sifency Image Accordion',
			'icon'           => 'eicon-accordion',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'sifency-image-accordion-script' ),
			'style_depends'  => array( 'sifency-image-accordion-style', 'sifency-typo-style' ),
		);}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_settings',
			array(
				'label' => esc_html__( 'Layout & Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Design Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'classic',
				'options' => array(
					'classic' => esc_html__( '1. Classic (Bottom Text)', 'sifency-addons' ),
					'zoom'    => esc_html__( '2. Modern Zoom', 'sifency-addons' ),
					'glass'   => esc_html__( '3. Glassmorphism', 'sifency-addons' ),
					'grid'    => esc_html__( '4. Separated / Grid', 'sifency-addons' ),
					'minimal' => esc_html__( '5. Minimal Reveal', 'sifency-addons' ),
				),
			)
		);
		$this->add_responsive_control(
			'orientation',
			array(
				'label'        => esc_html__( 'Orientation', 'sifency-addons' ),
				'type'         => Controls_Manager::CHOOSE,
				'options'      => array(
					'horizontal' => array(
						'title' => 'Horizontal',
						'icon'  => 'eicon-h-align-stretch',
					),
					'vertical'   => array(
						'title' => 'Vertical',
						'icon'  => 'eicon-v-align-stretch',
					),
				),
				'default'      => 'horizontal',
				'prefix_class' => 'sifency-ia-dir-',
			)
		);
		$this->add_control(
			'trigger_event',
			array(
				'label'   => esc_html__( 'Trigger Event', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'click',
				'options' => array(
					'click' => esc_html__( 'Click', 'sifency-addons' ),
					'hover' => esc_html__( 'Hover', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'active_item',
			array(
				'label'       => esc_html__( 'Active Item Index', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'default'     => 1,
				'description' => 'Which slide is open by default. Set to 0 to close all (if allowed by JS logic).',
			)
		);
		$this->add_responsive_control(
			'height',
			array(
				'label'     => esc_html__( 'Container Height', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 300,
						'max' => 1000,
					),
				),
				'default'   => array( 'size' => 400 ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-ia-wrapper' => 'height: {{SIZE}}px;',
				),
			)
		);
		$this->end_controls_section();

		// --- CONTENT SECTION ---
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Items', 'sifency-addons' ) )
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'image',
			array(
				'label'   => esc_html__( 'Image', 'sifency-addons' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array( 'url' => Utils::get_placeholder_image_src() ),
			)
		);
		$repeater->add_control(
			'title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Slide Title',
			)
		);
		$repeater->add_control(
			'desc',
			array(
				'label'   => 'Description',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Short description here.',
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label' => 'Link',
				'type'  => Controls_Manager::URL,
			)
		);
		$repeater->add_control(
			'btn_text',
			array(
				'label'   => 'Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Read More',
			)
		);

		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'Slides', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'title' => 'Adventure' ),
					array( 'title' => 'Nature' ),
					array( 'title' => 'Travel' ),
					array( 'title' => 'Lifestyle' ),
				),
				'title_field' => '{{{ title }}}',
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'image',
				'default' => 'large',
			)
		);
		$this->add_control(
			'arrow_next_icon',
			array(
				'label'   => esc_html__( 'Next Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-right-open-big',
					'library' => 'sifency-icons',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- STYLE: ITEM ---
		$this->start_controls_section(
			'style_item',
			array(
				'label' => 'Item Style',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'overlay_color',
			array(
				'label'     => 'Overlay Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-ia-overlay' => 'background: {{VALUE}};' ),
			)
		);
		$this->add_responsive_control(
			'item_gap',
			array(
				'label'     => 'Gap',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-ia-wrapper' => 'gap: {{SIZE}}px;' ),
			)
		);
		$this->add_responsive_control(
			'item_radius',
			array(
				'label'     => 'Border Radius',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .sifency-ia-item' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ),
			)
		);

		$this->end_controls_section();

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'content_wrapper_style_',
				'label'    => 'Content Wrapper Style',
				'selector' => '.sifency-ia-content-inner',
				'exclude'  => array( 'tabs' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Title Style',
				'selector' => '.sifency-heading-style',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style',
				'label'          => 'Content Style',
				'selector'       => '.sifency-content-style',
				'color_only'     => true,
				'show_max_width' => true,
				'exclude'        => array( 'tabs', 'transition', 'stroke', 'shadow', 'bg', 'border' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array()
		);
		$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();
		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-ia-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-ia-layout-' . $settings['layout_type'] );

		$js_settings = array( 'trigger' => $settings['trigger_event'] );
		$this->add_render_attribute( 'wrapper', 'data-ia-settings', wp_json_encode( $js_settings ) );

		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_wrapper_attrs ); ?>>
			
			<?php
			foreach ( $settings['items'] as $index => $item ) :
				$item_key             = 'item_' . $index;
				$animation_item_attrs = $animation_manager->get_item_attributes();
				$active_class         = ( ( $index + 1 ) === $settings['active_item'] ) ? 'active' : '';
				$this->add_render_attribute( $item_key, 'class', array( 'sifency-ia-item', $active_class ) );

				$img_src = '';
				if ( ! empty( $item['image']['id'] ) ) {
					$img_src = Group_Control_Image_Size::get_attachment_image_src( $item['image']['id'], 'image', $settings );
				} elseif ( ! empty( $item['image']['url'] ) ) {
					$img_src = $item['image']['url'];
				}

				$tag = 'div';
				if ( ! empty( $item['link']['url'] ) ) {
					$tag = 'a';
					$this->add_link_attributes( $item_key, $item['link'] );
				}
				?>
				<<?php echo wp_kses_post( $tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( $item_key ) . $animation_item_attrs ); ?>>
					
					<div class="sifency-ia-img-wrap">
						<img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( $item['title'] ); ?>">
						<div class="sifency-ia-overlay"></div>
					</div>

					<div class="sifency-ia-content">
						<?php if ( 'zoom' === $settings['layout_type'] || 'minimal' === $settings['layout_type'] ) : ?>
							<div class="sifency-ia-vertical-title"><?php echo esc_html( $item['title'] ); ?></div>
						<?php endif; ?>

						<div class="sifency-ia-content-inner">
							<h3 class="sifency-ia-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h3>
							<div class="sifency-ia-desc sifency-content-style"><?php echo wp_kses_post( $item['desc'] ); ?></div>
							<?php if ( ! empty( $item['btn_text'] ) ) : ?>
								<span class="sifency-button-style sifency-ia-btn sifency-addons-primary-button"><?php echo esc_html( $item['btn_text'] ); ?>
									<?php
									$next_icon = $settings['arrow_next_icon'] ?? '';
									\Elementor\Icons_Manager::render_icon( $next_icon, array( 'aria-hidden' => 'true' ) );
									?>
									</span>
							<?php endif; ?>
						</div>
					</div>

				</<?php echo wp_kses_post( $tag ); ?>>
			<?php endforeach; ?>

		</div>
		<?php
	}
}
