<?php
/**
 * Shared Attribute Display Controls
 *
 * Provides reusable Elementor controls for attribute/specification styling.
 * Use this class to register consistent attribute controls across widgets.
 *
 * @package GGM_Commerce
 * @since 1.0.0
 */

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

/**
 * Class GGM_Attribute_Display_Controls
 *
 * Registers standardized attribute display styling controls for Elementor widgets.
 */
class GGM_Attribute_Display_Controls {

	/**
	 * Register all attribute display 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(
			'icon_selector'  => '.ggmc-attr-icon',
			'label_selector' => '.ggmc-attr-name, .ggmc-attr-label',
			'value_selector' => '.ggmc-attr-value',
			'unit_selector'  => '.ggmc-attr-unit',
			'prefix'         => 'attr',
			'section_id'     => 'attribute_style',
			'section_label'  => __( 'Custom Attributes', 'ggm-commerce' ),
			'include_icon'   => true,
			'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
			);
		}

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

		// Label/Name controls.
		self::register_label_controls( $widget, $args );

		// Value controls.
		self::register_value_controls( $widget, $args );

		// 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 ) {
		$icon_selector = '{{WRAPPER}} ' . $args['icon_selector'];

		$widget->add_control(
			$args['prefix'] . '_icon_heading',
			array(
				'label' => __( 'Attribute Icon', 'ggm-commerce' ),
				'type'  => \Elementor\Controls_Manager::HEADING,
			)
		);

		$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' => 10,
						'max' => 50,
					),
					'em' => array(
						'min' => 0.5,
						'max' => 3,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 16,
				),
				'selectors'  => array(
					$icon_selector          => 'font-size: {{SIZE}}{{UNIT}};',
					$icon_selector . ' svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$widget->add_control(
			$args['prefix'] . '_icon_color',
			array(
				'label'     => __( 'Icon Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					$icon_selector          => 'color: {{VALUE}};',
					$icon_selector . ' svg' => 'fill: {{VALUE}};',
				),
			)
		);

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

		$widget->add_responsive_control(
			$args['prefix'] . '_icon_box_size',
			array(
				'label'      => __( 'Icon Box Size', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 20,
						'max' => 60,
					),
				),
				'selectors'  => array(
					$icon_selector => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_icon_border_radius',
			array(
				'label'      => __( 'Icon Border Radius', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					$icon_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{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(
					$icon_selector => 'margin-right: {{SIZE}}{{UNIT}};',
				),
			)
		);
	}

	/**
	 * Register label/name controls.
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	private static function register_label_controls( $widget, $args ) {
		$label_selector = '{{WRAPPER}} ' . $args['label_selector'];

		$widget->add_control(
			$args['prefix'] . '_label_heading',
			array(
				'label'     => __( 'Attribute Label', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => $args['prefix'] . '_label_typography',
				'selector' => $label_selector,
			)
		);

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

	/**
	 * Register value controls.
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	private static function register_value_controls( $widget, $args ) {
		$value_selector = '{{WRAPPER}} ' . $args['value_selector'];
		$unit_selector  = '{{WRAPPER}} ' . $args['unit_selector'];

		$widget->add_control(
			$args['prefix'] . '_value_heading',
			array(
				'label'     => __( 'Attribute Value', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => $args['prefix'] . '_value_typography',
				'selector' => $value_selector,
			)
		);

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

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

	/**
	 * Get default styles for attribute display 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 = 'attr', $design_system = array() ) {
		// Default design system values if not provided.
		$defaults_design = array(
			'colors' => array(
				'text_primary'   => '#0f172a',
				'text_secondary' => '#475569',
				'text_muted'     => '#64748b',
				'primary'        => '#2563eb',
				'surface'        => '#f8fafc',
			),
		);

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

		return array(
			$prefix . '_icon_size'          => array(
				'unit' => 'px',
				'size' => 16,
			),
			$prefix . '_icon_color'         => $design_system['colors']['primary'],
			$prefix . '_icon_background'    => $design_system['colors']['surface'],
			$prefix . '_icon_box_size'      => array(
				'unit' => 'px',
				'size' => 32,
			),
			$prefix . '_icon_border_radius' => array(
				'top'      => '4',
				'right'    => '4',
				'bottom'   => '4',
				'left'     => '4',
				'unit'     => 'px',
				'isLinked' => true,
			),
			$prefix . '_icon_spacing'       => array(
				'unit' => 'px',
				'size' => 8,
			),
			$prefix . '_label_color'        => $design_system['colors']['text_secondary'],
			$prefix . '_value_color'        => $design_system['colors']['text_primary'],
			$prefix . '_unit_color'         => $design_system['colors']['text_muted'],
		);
	}
}
