<?php
/**
 * Widget Name: Dual Button
 *
 * @package    captain-widgets-kit
 * 
 * @since      1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Icons_Manager;

/**
 * Class Captwiki_Dual_Button
 *
 * @since 1.0.0
 */
class Captwiki_Dual_Button extends Widget_Base {

	/**
	 * Get Widget Name.
	 *
	 * @since 1.0.0
	 */
	public function get_name() {
		return 'captwiki-dual-button';
	}

	/**
	 * Get Widget Title.
	 *
	 * @since 1.0.0
	 */
	public function get_title() {
		return esc_html__( 'Dual Button', 'captain-widgets-kit' );
	}

	/**
	 * Get Widget Icon.
	 *
	 * @since 1.0.0
	 */
	public function get_icon() {
		return 'eicon-button captwiki-el-logo';
	}

	/**
	 * Get Widget Categories.
	 *
	 * @since 1.0.0
	 */
	public function get_categories() {
		return array( 'captwiki-essential' );
	}

	/**
	 * Get Widget Keywords.
	 *
	 * @since 1.0.0
	 */
	public function get_keywords() {
		return array( 'dual', 'button', 'split', 'icon', 'captain-widgets-kit' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.0.0
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Register controls
	 *
	 * @since 1.0.0
	 * */
	public function register_controls() {

		$this->start_controls_section( 'section_left', array(
			'label' => esc_html__( 'Left Button', 'captain-widgets-kit' ),
			'tab'   => Controls_Manager::TAB_CONTENT,
		) );

		$this->add_control( 'left_text', array(
			'label'   => esc_html__( 'Text', 'captain-widgets-kit' ),
			'type'    => Controls_Manager::TEXT,
			'default' => esc_html__( 'Preview', 'captain-widgets-kit' ),
		) );

		$this->add_control( 'left_link', array(
			'label'   => esc_html__( 'Link', 'captain-widgets-kit' ),
			'type'    => Controls_Manager::URL,
			'default' => array( 'url' => '#' ),
			'dynamic' => array( 'active' => true ),
		) );

		$this->end_controls_section();

		// ── Right Button ──────────────────────────────────────────────────────
		$this->start_controls_section( 'section_right', array(
			'label' => esc_html__( 'Right Button', 'captain-widgets-kit' ),
			'tab'   => Controls_Manager::TAB_CONTENT,
		) );

		$this->add_control( 'right_text', array(
			'label'   => esc_html__( 'Text', 'captain-widgets-kit' ),
			'type'    => Controls_Manager::TEXT,
			'default' => esc_html__( 'Download', 'captain-widgets-kit' ),
		) );

		$this->add_control( 'right_link', array(
			'label'   => esc_html__( 'Link', 'captain-widgets-kit' ),
			'type'    => Controls_Manager::URL,
			'default' => array( 'url' => '#' ),
			'dynamic' => array( 'active' => true ),
		) );

		$this->end_controls_section();

		// ── Center Badge ──────────────────────────────────────────────────────
		$this->start_controls_section( 'section_badge', array(
			'label' => esc_html__( 'Center Badge', 'captain-widgets-kit' ),
			'tab'   => Controls_Manager::TAB_CONTENT,
		) );

		$this->add_control( 'badge_text', array(
			'label'   => esc_html__( 'Badge Text', 'captain-widgets-kit' ),
			'type'    => Controls_Manager::TEXT,
			'default' => esc_html__( 'or', 'captain-widgets-kit' ),
		) );

		$this->end_controls_section();

		$this->start_controls_section( 'section_style_layout', array(
			'label' => esc_html__( 'Layout', 'captain-widgets-kit' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		) );

		$this->add_control( 'equal_width', array(
			'label'        => esc_html__( 'Equal Button Width', 'captain-widgets-kit' ),
			'type'         => Controls_Manager::SWITCHER,
			'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
			'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
			'return_value' => 'yes',
			'default'      => '',
			'description'  => esc_html__( 'Both buttons will take equal width regardless of text length.', 'captain-widgets-kit' ),
		) );

		$this->add_responsive_control( 'button_min_width', array(
			'label'      => esc_html__( 'Button Min Width', 'captain-widgets-kit' ),
			'type'       => Controls_Manager::SLIDER,
			'size_units' => array( 'px' ),
			'range'      => array( 'px' => array( 'min' => 60, 'max' => 400 ) ),
			'default'    => array( 'unit' => 'px', 'size' => 150 ),
			'selectors'  => array(
				'{{WRAPPER}} .cw-dual-btn-left'  => 'min-width: {{SIZE}}{{UNIT}};',
				'{{WRAPPER}} .cw-dual-btn-right' => 'min-width: {{SIZE}}{{UNIT}};',
			),
		) );

		$this->add_responsive_control( 'badge_position', array(
			'label'      => esc_html__( 'Badge Position (Left ↔ Right)', 'captain-widgets-kit' ),
			'type'       => Controls_Manager::SLIDER,
			'size_units' => array( '%', 'px' ),
			'range'      => array(
				'%'  => array( 'min' => 0, 'max' => 100 ),
				'px' => array( 'min' => 0, 'max' => 600 ),
			),
			'default'    => array( 'unit' => '%', 'size' => 50 ),
			'description' => esc_html__( 'Default 50% = center. Badhao ya ghataao to OR left/right move karega.', 'captain-widgets-kit' ),
			'selectors'  => array(
				'{{WRAPPER}} .cw-dual-btn-badge' => 'left: {{SIZE}}{{UNIT}};',
			),
		) );

		$this->end_controls_section();
	}

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

		$left_text  = ! empty( $settings['left_text'] )  ? $settings['left_text']  : '';
		$right_text = ! empty( $settings['right_text'] ) ? $settings['right_text'] : '';
		$badge_text = ! empty( $settings['badge_text'] ) ? $settings['badge_text'] : 'or';

		$equal_width = ! empty( $settings['equal_width'] ) && 'yes' === $settings['equal_width'];

		$left_url    = ! empty( $settings['left_link']['url'] )         ? esc_url( $settings['left_link']['url'] )  : '#';
		$left_target = ! empty( $settings['left_link']['is_external'] ) ? ' target="_blank"'                        : '';
		$left_rel    = ! empty( $settings['left_link']['nofollow'] )    ? ' rel="nofollow"'                         : '';

		$right_url    = ! empty( $settings['right_link']['url'] )         ? esc_url( $settings['right_link']['url'] ) : '#';
		$right_target = ! empty( $settings['right_link']['is_external'] ) ? ' target="_blank"'                        : '';
		$right_rel    = ! empty( $settings['right_link']['nofollow'] )    ? ' rel="nofollow"'                         : '';

		?>
		<div class="cw-dual-btn style-1 <?php echo $equal_width ? 'cw-dual-btn--equal' : ''; ?>">
			<div class="cw-dual-btn-inner">

				<a class="cw-dual-btn-left"
					href="<?php echo $left_url; // phpcs:ignore ?>"
					<?php echo $left_target . $left_rel; // phpcs:ignore ?>>
					<?php echo esc_html( $left_text ); ?>
				</a>

				<span class="cw-dual-btn-divider" aria-hidden="true"></span>

				<a class="cw-dual-btn-right"
					href="<?php echo $right_url; // phpcs:ignore ?>"
					<?php echo $right_target . $right_rel; // phpcs:ignore ?>>
					<?php echo esc_html( $right_text ); ?>
				</a>

			</div>

			<?php /* Badge outside inner — not clipped by overflow:hidden */ ?>
			<span class="cw-dual-btn-badge" aria-hidden="true">
				<?php echo esc_html( $badge_text ); ?>
			</span>

		</div>
		<?php
	}
}
