<?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_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;
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_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Addons_Widget_Tabs extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-horizontal-tabs',
			'title'          => 'Sifency Horizontal Tab',
			'icon'           => 'eicon-tabs',
			'category'       => array( 'sifency-advance-widgets' ),
			'style_depends'  => array( 'sifency-horizontal-tab-style' ),
			'script_depends' => array( 'sifency-horizontal-tab-script' ),
			'keywords'       => array( 'horizontal tab', 'tabbed content', 'tab switcher', 'content switcher', 'tabbed interface' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Tabs Items', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'pills',
				'options' => array(
					'underline' => esc_html__( 'Material Underline', 'sifency-addons' ),
					'pills'     => esc_html__( 'Soft Pills', 'sifency-addons' ),
					'cards'     => esc_html__( 'Elevated Cards', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'tab_effect',
			array(
				'label'   => esc_html__( 'Content Animation', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'fade',
				'options' => array(
					'fade'     => esc_html__( 'Fade In', 'sifency-addons' ),
					'slide-up' => esc_html__( 'Slide Up', 'sifency-addons' ),
					'scale-up' => esc_html__( 'Scale Up', 'sifency-addons' ),
				),
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'tab_title',
			array(
				'label'   => esc_html__( 'Title', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Tab Title',
			)
		);
		$repeater->add_control(
			'tab_icon',
			array(
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);
		$repeater->add_control(
			'content_source',
			array(
				'label'   => esc_html__( 'Content Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'editor',
				'options' => array(
					'editor'   => esc_html__( 'Editor', 'sifency-addons' ),
					'template' => esc_html__( 'Template', 'sifency-addons' ),
				),
			)
		);
		$repeater->add_control(
			'content_editor',
			array(
				'label'     => esc_html__( 'Content', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
				'condition' => array( 'content_source' => 'editor' ),
			)
		);
		$repeater->add_control(
			'template_id',
			array(
				'label'     => esc_html__( 'Choose Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '0',
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array( 'content_source' => 'template' ),
			)
		);
		$this->add_control(
			'tabs',
			array(
				'label'       => esc_html__( 'Tabs Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'tab_title' => 'Development' ),
					array( 'tab_title' => 'Design' ),
					array( 'tab_title' => 'Marketing' ),
				),
				'title_field' => '{{{ tab_title }}}',
			)
		);
		$this->add_responsive_control(
			'align_nav',
			array(
				'label'     => esc_html__( 'Nav Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'center'     => array(
						'title' => 'Center',
						'icon'  => 'eicon-h-align-center',
					),
					'flex-end'   => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default'   => 'flex-start',
				'selectors' => array(
					'{{WRAPPER}} .sifency-tabs-nav' => 'justify-content: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_nav',
			array(
				'label' => esc_html__( 'Navigation Bar', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'nav_bg_color',
			array(
				'label'     => 'Nav Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-tabs-nav' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_responsive_control(
			'nav_gap',
			array(
				'label'     => 'Gap Between Tabs',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-tabs-nav' => 'gap: {{SIZE}}px;' ),
			)
		);
		$this->add_responsive_control(
			'nav_padding',
			array(
				'label'      => 'Padding',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-tabs-nav' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'nav_margin',
			array(
				'label'      => 'Margin',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-tabs-nav' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'nav_radius',
			array(
				'label'     => 'Border Radius',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .sifency-tabs-nav' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ),
			)
		);

		$this->end_controls_section();

		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'          => 'nav_item_style',
				'label'           => 'Nav Item',
				'selector'        => '.sifency-tab-title',
				'hover_selector'  => '.sifency-tab-title.active',
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Content',
				'selector'   => '.sifency-tab-content',
				'color_only' => true,
				'exclude'    => array( 'tabs', 'transition', 'stroke', 'shadow' ),
			)
		);
		$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();
		$id_int                  = substr( $this->get_id_int(), 0, 3 );

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-tabs-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-tabs-layout-' . $settings['layout_type'] );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-tabs-effect-' . $settings['tab_effect'] );
		$this->add_render_attribute( 'wrapper', 'id', 'sifency-tabs-' . $id_int );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_wrapper_attrs ); ?>>
			
			<!-- Navigation -->
			<div class="sifency-tabs-nav" role="tablist">
				<?php
				foreach ( $settings['tabs'] as $index => $tab ) :
					$tab_key              = 'tab_title_' . $index;
					$is_active            = $index === 0 ? 'active' : '';
					$animation_item_attrs = $animation_manager->get_item_attributes();

					$this->add_render_attribute( $tab_key, 'class', array( 'sifency-tab-title', $is_active ) );
					$this->add_render_attribute( $tab_key, 'role', 'tab' );
					$this->add_render_attribute( $tab_key, 'data-tab', $index + 1 );
					$this->add_render_attribute( $tab_key, 'tabindex', '0' );
					?>
					<div <?php echo wp_kses_post( $this->get_render_attribute_string( $tab_key ) . $animation_item_attrs ); ?>>
						<?php if ( ! empty( $tab['tab_icon']['value'] ) ) : ?>
							<span class="sifency-tab-icon">
								<?php Icons_Manager::render_icon( $tab['tab_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</span>
						<?php endif; ?>
						<span class="sifency-tab-text"><?php echo esc_html( $tab['tab_title'] ); ?></span>
					</div>
				<?php endforeach; ?>
			</div>

			<!-- Content -->
			<div class="sifency-tabs-content-wrapper">
				<?php
				foreach ( $settings['tabs'] as $index => $tab ) :
					$content_key = 'tab_content_' . $index;
					$is_active   = $index === 0 ? 'active' : '';
					$this->add_render_attribute( $content_key, 'class', array( 'sifency-tab-content', $is_active ) );
					$this->add_render_attribute( $content_key, 'data-tab', $index + 1 );
					$this->add_render_attribute( $content_key, 'role', 'tabpanel' );
					?>
					<div <?php echo wp_kses_post( $this->get_render_attribute_string( $content_key ) ); ?>>
						<?php
						if ( 'template' === $tab['content_source'] && ! empty( $tab['template_id'] ) ) {
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
							echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $tab['template_id'] );
						} else {
							echo wp_kses_post( $tab['content_editor'] );
						}
						?>
					</div>
				<?php endforeach; ?>
			</div>

		</div>
		<?php
	}
}
