<?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\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_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Dual_Button extends Widget_Base {

	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-dual-button',
			'title'         => 'Sifency Dual Button',
			'icon'          => 'eicon-t-letter',
			'category'      => array( 'sifency-addons-widgets' ),
			'category'      => 'sifency-addons-widgets',
			'style_depends' => array( 'sifency-icon', 'sifency-button-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_btn1_content',
			array(
				'label' => esc_html__( 'Primary Button', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'btn1_text',
			array(
				'label'   => esc_html__( 'Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Hello Universe', 'sifency-addons' ),
				'dynamic' => array( 'active' => true ),
			)
		);
		$this->add_control(
			'btn1_link',
			array(
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => esc_html__( 'https://your-link.com', 'sifency-addons' ),
				'default'     => array(
					'url' => '#',
				),
			)
		);
		$this->add_control(
			'btn1_icon',
			array(
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);
		$this->add_control(
			'btn1_icon_align',
			array(
				'label'     => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'left',
				'options'   => array(
					'left'  => esc_html__( 'Before', 'sifency-addons' ),
					'right' => esc_html__( 'After', 'sifency-addons' ),
				),
				'condition' => array(
					'btn1_icon[value]!' => '',
				),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_middle_content',
			array(
				'label' => esc_html__( 'Middle Text / Separator', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'middle_text',
			array(
				'label'       => esc_html__( 'Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'OR', 'sifency-addons' ),
				'placeholder' => 'OR',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_btn2_content',
			array(
				'label' => esc_html__( 'Secondary Button', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'btn2_text',
			array(
				'label'   => esc_html__( 'Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Hello World', 'sifency-addons' ),
				'dynamic' => array( 'active' => true ),
			)
		);
		$this->add_control(
			'btn2_link',
			array(
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => esc_html__( 'https://your-link.com', 'sifency-addons' ),
				'default'     => array(
					'url' => '#',
				),
			)
		);
		$this->add_control(
			'btn2_icon',
			array(
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);
		$this->add_control(
			'btn2_icon_align',
			array(
				'label'     => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'right',
				'options'   => array(
					'left'  => esc_html__( 'Before', 'sifency-addons' ),
					'right' => esc_html__( 'After', 'sifency-addons' ),
				),
				'condition' => array(
					'btn2_icon[value]!' => '',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- LAYOUT STYLE ---
		$this->start_controls_section(
			'section_layout_style',
			array(
				'label' => esc_html__( 'Layout & Alignment', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'buttons_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start'    => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'        => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'      => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
					'space-between' => array(
						'title' => esc_html__( 'Justify', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'default'   => 'center',
				'selectors' => array(
					'{{WRAPPER}} .sifency-dual-btn-wrapper' => 'justify-content: {{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'buttons_gap',
			array(
				'label'      => esc_html__( 'Gap', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 20,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-dual-btn-wrapper' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'          => 'primary_button_',
				'label'           => esc_html__( 'Primary Button', 'sifency-addons' ),
				'selector'        => '.sifency-button.primary.sifency-btn-1',
				'show_dimensions' => true,
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'icon_primary_button_',
				'label'           => esc_html__( 'B1 Icon Style Override', 'sifency-addons' ),
				'selector'        => '.sifency-button.primary.sifency-btn-1 .sifency-btn-icon',
				'hover_selector'  => '.sifency-button.primary.sifency-btn-1:hover .sifency-btn-icon',
				'show_dimensions' => true,
				'condition'       => array(
					'btn1_icon[value]!' => '',
				),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'separator_text_',
				'label'     => esc_html__( 'Separator Text', 'sifency-addons' ),
				'selector'  => '.sifency-content-style',
				'exclude'   => array( 'alignment', 'tabs', 'transition', 'border', 'radius', 'shadow', 'stroke', 'bg' ),
				'condition' => array( 'middle_text!' => '' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'          => 'secondary_button_',
				'label'           => esc_html__( 'Secondary Button', 'sifency-addons' ),
				'selector'        => '.sifency-button.secondary.sifency-btn-2',
				'show_dimensions' => true,
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'icon_secondary_button_',
				'label'           => esc_html__( 'B2 Icon Style Override', 'sifency-addons' ),
				'selector'        => '.sifency-button.secondary.sifency-btn-2 .sifency-btn-icon',
				'hover_selector'  => '.sifency-button.secondary.sifency-btn-2:hover .sifency-btn-icon',
				'show_dimensions' => true,
				'condition'       => array(
					'btn2_icon[value]!' => '',
				),
			)
		);

		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	protected function render() {
		$settings = $this->get_settings_for_display();

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-dual-btn-wrapper' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		if ( ! empty( $settings['btn1_link']['url'] ) ) {
			$this->add_link_attributes( 'btn1_link', $settings['btn1_link'] );
		}
		if ( ! empty( $settings['btn2_link']['url'] ) ) {
			$this->add_link_attributes( 'btn2_link', $settings['btn2_link'] );
		}

		$this->add_render_attribute( 'btn1_link', 'class', 'sifency-button primary sifency-btn-1' );
		$this->add_render_attribute( 'btn2_link', 'class', 'sifency-button secondary sifency-btn-2' );

		?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			
			<a <?php $this->print_render_attribute_string( 'btn1_link' ); ?>>
				<?php $this->render_button_content( $settings, 'btn1' ); ?>
			</a>

			<?php if ( ! empty( $settings['middle_text'] ) ) : ?>
				<span class="sifency-dual-sep sifency-content-style"><?php echo esc_html( $settings['middle_text'] ); ?></span>
			<?php endif; ?>

			 <a <?php $this->print_render_attribute_string( 'btn2_link' ); ?>>
				<?php $this->render_button_content( $settings, 'btn2' ); ?>
			</a>

		</div>
		<?php
	}

	/**
	 * Helper to render button content (Icon + Text)
	 */
	protected function render_button_content( $settings, $key ) {
		$text  = $settings[ $key . '_text' ];
		$icon  = $settings[ $key . '_icon' ];
		$align = $settings[ $key . '_icon_align' ];

		if ( ! empty( $icon['value'] ) && 'left' === $align ) {
			echo '<span class="sifency-btn-icon sifency-icon-left">';
			Icons_Manager::render_icon( $icon, array( 'aria-hidden' => 'true' ) );
			echo '</span>';
		}

		if ( ! empty( $text ) ) {
			echo '<span class="sifency-btn-text">' . esc_html( $text ) . '</span>';
		}

		if ( ! empty( $icon['value'] ) && 'right' === $align ) {
			echo '<span class="sifency-btn-icon sifency-icon-right">';
			Icons_Manager::render_icon( $icon, array( 'aria-hidden' => 'true' ) );
			echo '</span>';
		}
	}
}
