<?php
/**
 * Background Variable Processor
 *
 * @package Brandy\DynamicCss\Processors
 */

namespace Brandy\DynamicCss\Processors;

use Brandy\Utils\StylesDataHelpers;

/**
 * Processor for background CSS variables
 */
class BackgroundVariableProcessor extends AbstractVariableProcessor {

	/**
	 * Check if this processor supports the given type
	 *
	 * @param string $type Variable type.
	 * @return bool
	 */
	public function supports( string $type ): bool {
		return 'background' === $type;
	}

	/**
	 * Process background variable data
	 *
	 * @param array $variable_data Variable configuration.
	 * @return array Device-keyed CSS variables
	 */
	public function process( array $variable_data ): array {
		$result   = $this->responsive_helper->get_empty_device_result();
		$selector = $variable_data['selector'] ?? '';
		$value    = $variable_data['value'];

		$color         = null;
		$overlay_color = null;
		$size          = null;
		$position      = null;

		if ( 'solid' === $value['type'] ) {
			$color = StylesDataHelpers::get_css_variable( $variable_data['name'] . '-main', $value['solid_color'] );
		}

		if ( 'gradient' === $value['type'] ) {
			$color = StylesDataHelpers::get_css_variable( $variable_data['name'] . '-main', $value['gradient_color'] );
		}

		if ( 'image' === $value['type'] ) {
			$image_data    = $value['image'];
			$color         = StylesDataHelpers::get_css_variable( $variable_data['name'] . '-main', 'url(' . $image_data['url'] . ')' );
			$overlay_color = StylesDataHelpers::get_css_variable( $variable_data['name'] . '-overlay-color', $image_data['overlay_color'] );
			$size          = StylesDataHelpers::get_css_variable( $variable_data['name'] . '-size', $image_data['size'] );
			$position      = $this->get_position_value( $variable_data['name'], $image_data );
		}

		$this->add_if_not_empty( $result, $selector, $color );
		$this->add_if_not_empty( $result, $selector, $overlay_color );
		$this->add_if_not_empty( $result, $selector, $size );
		$this->add_if_not_empty( $result, $selector, $position );

		return $result;
	}

	/**
	 * Get position CSS variable value
	 *
	 * @param string $name       Variable name prefix.
	 * @param array  $image_data Image configuration.
	 * @return string CSS variable declaration
	 */
	private function get_position_value( string $name, array $image_data ): string {
		$position_value = $image_data['position'];

		if ( 'custom' === $position_value ) {
			$position_value = StylesDataHelpers::get_dimension_css( $image_data['left'] ) . ' ' . StylesDataHelpers::get_dimension_css( $image_data['top'] );
		}

		return StylesDataHelpers::get_css_variable( $name . '-position', $position_value );
	}

	/**
	 * Add CSS variable to result if not empty
	 *
	 * @param array  $result   Result array (passed by reference).
	 * @param string $selector CSS selector.
	 * @param mixed  $css_var  CSS variable declaration.
	 * @return void
	 */
	private function add_if_not_empty( array &$result, string $selector, $css_var ): void {
		if ( ! empty( $css_var ) ) {
			$this->responsive_helper->add_to_result( $result, 'desktop', $selector, $css_var );
		}
	}
}
