<?php
namespace Sifency\Traits;

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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Icons_Manager;

trait Sifency_Addons_Combine_Button_Controls {
	protected function add_sifency_addons_combine_button_content_controls( $args = array() ) {
		$args = wp_parse_args(
			$args,
			array(
				'prefix'    => 'button_',
				'label'     => esc_html__( 'Button', 'sifency-addons' ),
				'condition' => array(),
			)
		);

		$prefix = rtrim( $args['prefix'], '_' ) . '_';

		$this->start_controls_section(
			$prefix . 'content_section',
			array(
				'label'     => $args['label'],
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => $args['condition'],
			)
		);

		$this->add_control(
			$prefix . 'enable',
			array(
				'label'        => esc_html__( 'Enable Button', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			$prefix . 'text',
			array(
				'label'     => esc_html__( 'Button Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Click Here', 'sifency-addons' ),
				'condition' => array( $prefix . 'enable' => 'yes' ),
			)
		);

		$this->add_control(
			$prefix . 'link',
			array(
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => 'https://your-link.com',
				'default'     => array( 'url' => '#' ),
				'condition'   => array( $prefix . 'enable' => 'yes' ),
			)
		);

		$this->add_control(
			$prefix . 'icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'condition' => array( $prefix . 'enable' => 'yes' ),
			)
		);

		$this->add_control(
			$prefix . 'icon_pos',
			array(
				'label'     => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'after',
				'options'   => array(
					'before' => esc_html__( 'Before', 'sifency-addons' ),
					'after'  => esc_html__( 'After', 'sifency-addons' ),
				),
				'condition' => array(
					$prefix . 'enable'       => 'yes',
					$prefix . 'icon[value]!' => '',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function add_Sifency_Addons_Combine_Button_Controls( $args = array() ) {
		$args = wp_parse_args(
			$args,
			array(
				'prefix'          => 'button_',
				'label'           => esc_html__( 'Button Style', 'sifency-addons' ),
				'selector'        => '.sifency-button-style',
				'hover_selector'  => '',
				'target_children' => true,
				'condition'       => array(),
				'show_typography' => true,
				'show_dimensions' => false,
				'hover_tab_label' => esc_html__( 'Hover', 'sifency-addons' ),
				'exclude'         => array(),
			)
		);

		$prefix        = rtrim( $args['prefix'], '_' ) . '_';
		$exclude       = (array) $args['exclude'];
		$base_selector = ( strpos( $args['selector'], '{{WRAPPER}}' ) === false ) ? '{{WRAPPER}} ' . $args['selector'] : $args['selector'];

		$master_condition = array_merge( $args['condition'], array( $prefix . 'enable' => 'yes' ) );

		$h_trigger = $args['hover_selector'];
		if ( empty( $h_trigger ) ) {
			$h_trigger = $base_selector . ':hover';
		} elseif ( strpos( $h_trigger, '{{WRAPPER}}' ) === false ) {
			$h_trigger = '{{WRAPPER}} ' . $h_trigger;
		}

		if ( $args['target_children'] ) {
			$tags            = array( '', 'span', 'i', 'svg' );
			$text_selectors  = implode( ',', array_map( fn( $tag ) => $base_selector . ( $tag ? ' ' . $tag : '' ), $tags ) );
			$hover_selectors = implode( ',', array_map( fn( $tag ) => $h_trigger . ( $tag ? ' ' . $tag : '' ), $tags ) );
		} else {
			$text_selectors  = $base_selector;
			$hover_selectors = $h_trigger;
		}

		$this->start_controls_section(
			$prefix . 'style_section',
			array(
				'label'     => $args['label'],
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => $master_condition,
			)
		);

		if ( $args['show_dimensions'] && ! in_array( 'dimensions', $exclude ) ) {
			$this->add_responsive_control(
				$prefix . 'width',
				array(
					'label'     => 'Width',
					'type'      => Controls_Manager::SLIDER,
					'selectors' => array( $base_selector => 'width: {{SIZE}}{{UNIT}}; display:inline-flex; align-items:center; justify-content:center;' ),
				)
			);
			$this->add_responsive_control(
				$prefix . 'height',
				array(
					'label'     => 'Height',
					'type'      => Controls_Manager::SLIDER,
					'selectors' => array( $base_selector => 'height: {{SIZE}}{{UNIT}}; display:inline-flex; align-items:center; justify-content:center;' ),
				)
			);
		}

		if ( $args['show_typography'] && ! in_array( 'typography', $exclude ) ) {
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => $prefix . 'typography',
					'selector' => $text_selectors,
				)
			);
		}

		$this->start_controls_tabs( $prefix . 'tabs' );

		$this->start_controls_tab( $prefix . 'normal_tab', array( 'label' => 'Normal' ) );
		$this->add_control(
			$prefix . 'text_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $text_selectors => 'color: {{VALUE}}; fill: {{VALUE}};' ),
			)
		);
		if ( ! in_array( 'background', $exclude ) ) {
			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => $prefix . 'bg',
					'selector' => $base_selector,
				)
			);
		}
		$this->end_controls_tab();

		$this->start_controls_tab( $prefix . 'hover_tab', array( 'label' => $args['hover_tab_label'] ) );
		$this->add_control(
			$prefix . 'hover_text_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $hover_selectors => 'color: {{VALUE}}; fill: {{VALUE}};' ),
			)
		);
		if ( ! in_array( 'background', $exclude ) ) {
			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => $prefix . 'hover_bg',
					'selector' => $h_trigger,
				)
			);
		}
		$this->end_controls_tab();
		$this->end_controls_tabs();

		if ( ! in_array( 'border', $exclude ) ) {
			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'      => $prefix . 'border',
					'selector'  => $base_selector,
					'separator' => 'before',
				)
			);
			$this->add_responsive_control(
				$prefix . 'radius',
				array(
					'label'     => 'Border Radius',
					'type'      => Controls_Manager::DIMENSIONS,
					'selectors' => array( $base_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				)
			);
		}

		if ( ! in_array( 'shadow', $exclude ) ) {
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => $prefix . 'shadow',
					'selector' => $base_selector,
				)
			);
		}

		$this->add_responsive_control(
			$prefix . 'padding',
			array(
				'label'     => 'Padding',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( $base_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		$this->add_responsive_control(
			$prefix . 'margin',
			array(
				'label'     => 'Margin',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( $base_selector => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		$this->end_controls_section();
	}
	protected function add_sifency_addons_combine_button_render( $settings, $prefix = 'button_', $class = '' ) {
		$prefix = rtrim( $prefix, '_' ) . '_';

		if ( 'yes' !== ( $settings[ $prefix . 'enable' ] ?? '' ) ) {
			return;
		}

		$this->add_render_attribute( $prefix . 'attr', 'class', array( 'sifency-button-style', $class ) );
		$this->add_link_attributes( $prefix . 'attr', $settings[ $prefix . 'link' ] );

		$icon_pos = $settings[ $prefix . 'icon_pos' ];
		$has_icon = ! empty( $settings[ $prefix . 'icon' ]['value'] );

		?>
		<a <?php echo wp_kses_post( $this->get_render_attribute_string( $prefix . 'attr' ) ); ?>>
			<?php if ( $has_icon && 'before' === $icon_pos ) : ?>
				<span class="btn-icon icon-before">
					<?php Icons_Manager::render_icon( $settings[ $prefix . 'icon' ], array( 'aria-hidden' => 'true' ) ); ?>
				</span>
			<?php endif; ?>

			<?php if ( ! empty( $settings[ $prefix . 'text' ] ) ) : ?>
				<span class="btn-text"><?php echo esc_html( $settings[ $prefix . 'text' ] ); ?></span>
			<?php endif; ?>

			<?php if ( $has_icon && 'after' === $icon_pos ) : ?>
				<span class="btn-icon icon-after">
					<?php Icons_Manager::render_icon( $settings[ $prefix . 'icon' ], array( 'aria-hidden' => 'true' ) ); ?>
				</span>
			<?php endif; ?>
		</a>
		<?php
	}
}
