<?php
/**
 * Shared Add to Cart Button Controls
 *
 * Provides reusable Elementor controls for Add to Cart button styling.
 * Use this class to register consistent button controls across widgets.
 *
 * @package GGM_Commerce
 * @since 1.0.0
 */

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

/**
 * Class GGM_Add_To_Cart_Controls
 *
 * Registers standardized Add to Cart button styling controls for Elementor widgets.
 */
class GGM_Add_To_Cart_Controls {

	/**
	 * Register all Add to Cart button controls on a widget.
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	public static function register( $widget, $args = array() ) {
		$defaults = array(
			'selector'           => '.ggmc-add-to-cart-btn',
			'prefix'             => 'button',
			'section_id'         => 'add_to_cart_style',
			'section_label'      => __( 'Add to Cart Button', 'ggm-commerce' ),
			'include_icon'       => false,
			'include_box_shadow' => false,
			'start_section'      => true,
			'end_section'        => true,
			'condition'          => array(),
		);

		$args = wp_parse_args( $args, $defaults );

		// Start section if requested.
		if ( $args['start_section'] ) {
			$section_args = array(
				'label' => $args['section_label'],
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			);

			if ( ! empty( $args['condition'] ) ) {
				$section_args['condition'] = $args['condition'];
			}

			$widget->start_controls_section(
				$args['section_id'],
				$section_args
			);
		}

		// Typography.
		$widget->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => $args['prefix'] . '_typography',
				'selector' => '{{WRAPPER}} ' . $args['selector'],
			)
		);

		// Normal/Hover tabs.
		$widget->start_controls_tabs( $args['prefix'] . '_tabs' );

		// Normal tab.
		$widget->start_controls_tab(
			$args['prefix'] . '_normal_tab',
			array( 'label' => __( 'Normal', 'ggm-commerce' ) )
		);

		$widget->add_control(
			$args['prefix'] . '_text_color',
			array(
				'label'     => __( 'Text Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} ' . $args['selector'] => 'color: {{VALUE}};',
				),
			)
		);

		$widget->add_control(
			$args['prefix'] . '_background_color',
			array(
				'label'     => __( 'Background Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} ' . $args['selector'] => 'background-color: {{VALUE}};',
				),
			)
		);

		// Icon color in normal tab (if icons enabled).
		if ( $args['include_icon'] ) {
			$widget->add_control(
				$args['prefix'] . '_icon_color',
				array(
					'label'     => __( 'Icon Color', 'ggm-commerce' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} ' . $args['selector'] . ' i'   => 'color: {{VALUE}};',
						'{{WRAPPER}} ' . $args['selector'] . ' svg' => 'fill: {{VALUE}};',
					),
				)
			);
		}

		$widget->end_controls_tab();

		// Hover tab.
		$widget->start_controls_tab(
			$args['prefix'] . '_hover_tab',
			array( 'label' => __( 'Hover', 'ggm-commerce' ) )
		);

		$widget->add_control(
			$args['prefix'] . '_hover_text_color',
			array(
				'label'     => __( 'Text Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} ' . $args['selector'] . ':hover' => 'color: {{VALUE}};',
				),
			)
		);

		$widget->add_control(
			$args['prefix'] . '_hover_background_color',
			array(
				'label'     => __( 'Background Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} ' . $args['selector'] . ':hover' => 'background-color: {{VALUE}};',
				),
			)
		);

		$widget->add_control(
			$args['prefix'] . '_hover_border_color',
			array(
				'label'     => __( 'Border Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} ' . $args['selector'] . ':hover' => 'border-color: {{VALUE}};',
				),
			)
		);

		// Icon hover color (if icons enabled).
		if ( $args['include_icon'] ) {
			$widget->add_control(
				$args['prefix'] . '_hover_icon_color',
				array(
					'label'     => __( 'Icon Color', 'ggm-commerce' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} ' . $args['selector'] . ':hover i'   => 'color: {{VALUE}};',
						'{{WRAPPER}} ' . $args['selector'] . ':hover svg' => 'fill: {{VALUE}};',
					),
				)
			);
		}

		$widget->end_controls_tab();
		$widget->end_controls_tabs();

		// Icon controls (outside tabs).
		if ( $args['include_icon'] ) {
			self::register_icon_controls( $widget, $args );
		}

		// Spacing controls.
		self::register_spacing_controls( $widget, $args );

		// Box shadow (optional).
		if ( $args['include_box_shadow'] ) {
			$widget->add_group_control(
				\Elementor\Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => $args['prefix'] . '_box_shadow',
					'selector' => '{{WRAPPER}} ' . $args['selector'],
				)
			);
		}

		// End section if requested.
		if ( $args['end_section'] ) {
			$widget->end_controls_section();
		}
	}

	/**
	 * Register icon-related controls.
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	private static function register_icon_controls( $widget, $args ) {
		$widget->add_control(
			$args['prefix'] . '_icon_heading',
			array(
				'label'     => __( 'Icon', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_control(
			$args['prefix'] . '_icon',
			array(
				'label'   => __( 'Icon', 'ggm-commerce' ),
				'type'    => \Elementor\Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'fas fa-shopping-cart',
					'library' => 'fa-solid',
				),
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_icon_size',
			array(
				'label'      => __( 'Icon Size', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 8,
						'max' => 50,
					),
					'em' => array(
						'min' => 0.5,
						'max' => 3,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 14,
				),
				'selectors'  => array(
					'{{WRAPPER}} ' . $args['selector'] . ' i'   => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} ' . $args['selector'] . ' svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_icon_spacing',
			array(
				'label'      => __( 'Icon Spacing', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 30,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 8,
				),
				'selectors'  => array(
					'{{WRAPPER}} ' . $args['selector'] . ' i'   => 'margin-right: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} ' . $args['selector'] . ' svg' => 'margin-right: {{SIZE}}{{UNIT}};',
				),
			)
		);
	}

	/**
	 * Register spacing controls (padding, border, border-radius).
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	private static function register_spacing_controls( $widget, $args ) {
		$widget->add_control(
			$args['prefix'] . '_spacing_heading',
			array(
				'label'     => __( 'Spacing & Border', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_padding',
			array(
				'label'      => __( 'Padding', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} ' . $args['selector'] => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$widget->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => $args['prefix'] . '_border',
				'selector' => '{{WRAPPER}} ' . $args['selector'],
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_border_radius',
			array(
				'label'      => __( 'Border Radius', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} ' . $args['selector'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
	}

	/**
	 * Get default styles for Add to Cart button controls.
	 *
	 * @param string $prefix Control name prefix.
	 * @param array  $design_system Design system values.
	 *
	 * @return array Default control values.
	 */
	public static function get_defaults( $prefix = 'button', $design_system = array() ) {
		// Default design system values if not provided.
		$defaults_design = array(
			'colors' => array(
				'primary'      => '#2563eb',
				'primary_dark' => '#1d4ed8',
			),
		);

		$design_system = wp_parse_args( $design_system, $defaults_design );

		return array(
			$prefix . '_text_color'             => '#FFFFFF',
			$prefix . '_background_color'       => $design_system['colors']['primary'],
			$prefix . '_hover_text_color'       => '#FFFFFF',
			$prefix . '_hover_background_color' => $design_system['colors']['primary_dark'],
			$prefix . '_hover_border_color'     => $design_system['colors']['primary_dark'],
			$prefix . '_padding'                => array(
				'top'      => '12',
				'right'    => '24',
				'bottom'   => '12',
				'left'     => '24',
				'unit'     => 'px',
				'isLinked' => false,
			),
			$prefix . '_border_radius'          => array(
				'top'      => '6',
				'right'    => '6',
				'bottom'   => '6',
				'left'     => '6',
				'unit'     => 'px',
				'isLinked' => true,
			),
			$prefix . '_icon'                   => array(
				'value'   => 'fas fa-shopping-cart',
				'library' => 'fa-solid',
			),
			$prefix . '_icon_size'              => array(
				'unit' => 'px',
				'size' => 14,
			),
			$prefix . '_icon_spacing'           => array(
				'unit' => 'px',
				'size' => 8,
			),
		);
	}
}
