<?php
/**
 * PufferGo Unit Formatter
 *
 * Formats number+unit values for display.
 *
 * @package PufferGo
 */

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

class PufferGo_Unit_Formatter {

	const TYPE_CONTACT = 'contact';
	const TYPE_FIXED   = 'fixed';
	const TYPE_RANGE   = 'range';

	public static function format( $data, $unit_type, $lang = null ) {
		if ( ! is_array( $data ) || empty( $data ) ) {
			return '';
		}

		if ( ! $lang ) {
			$lang = get_locale();
		}

		if ( isset( $data['type'] ) && self::TYPE_CONTACT === $data['type'] ) {
			return __( 'Contact for quote', 'puffergo' );
		}

		if ( isset( $data['min'] ) && isset( $data['max'] ) ) {
			return self::format_range( $data, $unit_type, $lang );
		}

		if ( isset( $data['value'] ) ) {
			return self::format_fixed( $data, $unit_type, $lang );
		}

		return '';
	}

	private static function format_fixed( $data, $unit_type, $lang ) {
		$config   = PufferGo_Unit_Config::get_instance();
		$value    = isset( $data['value'] ) ? floatval( $data['value'] ) : 0;
		$unit     = isset( $data['unit'] ) ? $data['unit'] : '';
		$position = $config->get_unit_position( $unit_type, $unit );
		$decimal  = $config->get_unit_decimal( $unit_type, $unit );

		$formatted_value = number_format_i18n( $value, $decimal );

		if ( 'currency' === $unit_type ) {
			$symbol = $config->get_unit_symbol( $unit_type, $unit );
			if ( 'before' === $position ) {
				return $symbol . $formatted_value;
			}
			return $formatted_value . ' ' . $symbol;
		}

		$label = $config->get_unit_label( $unit_type, $unit, $lang );
		if ( 'before' === $position ) {
			return $label . ' ' . $formatted_value;
		}
		return $formatted_value . ' ' . $label;
	}

	private static function format_range( $data, $unit_type, $lang ) {
		$config    = PufferGo_Unit_Config::get_instance();
		$min       = isset( $data['min'] ) ? floatval( $data['min'] ) : 0;
		$max       = isset( $data['max'] ) ? floatval( $data['max'] ) : 0;
		$unit      = isset( $data['unit'] ) ? $data['unit'] : '';
		$position  = $config->get_unit_position( $unit_type, $unit );
		$decimal   = $config->get_unit_decimal( $unit_type, $unit );

		$formatted_min = number_format_i18n( $min, $decimal );
		$formatted_max = number_format_i18n( $max, $decimal );

		if ( 'currency' === $unit_type ) {
			$symbol = $config->get_unit_symbol( $unit_type, $unit );
			if ( 'before' === $position ) {
				return $symbol . $formatted_min . ' - ' . $symbol . $formatted_max;
			}
			return $formatted_min . ' ' . $symbol . ' - ' . $formatted_max . ' ' . $symbol;
		}

		$label = $config->get_unit_label( $unit_type, $unit, $lang );
		if ( 'before' === $position ) {
			return $label . ' ' . $formatted_min . ' - ' . $formatted_max;
		}
		return $formatted_min . ' - ' . $formatted_max . ' ' . $label;
	}

	public static function parse_input( $input ) {
		if ( is_array( $input ) ) {
			return $input;
		}

		if ( is_string( $input ) ) {
			$decoded = json_decode( $input, true );
			if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
				return $decoded;
			}
		}

		return array();
	}

	public static function is_valid( $data ) {
		if ( ! is_array( $data ) || empty( $data ) ) {
			return false;
		}

		if ( isset( $data['type'] ) && self::TYPE_CONTACT === $data['type'] ) {
			return true;
		}

		if ( isset( $data['min'] ) && isset( $data['max'] ) && isset( $data['unit'] ) ) {
			return is_numeric( $data['min'] ) && is_numeric( $data['max'] );
		}

		if ( isset( $data['value'] ) && isset( $data['unit'] ) ) {
			return is_numeric( $data['value'] );
		}

		return false;
	}

	public static function get_type( $data ) {
		if ( ! is_array( $data ) ) {
			return null;
		}

		if ( isset( $data['type'] ) && self::TYPE_CONTACT === $data['type'] ) {
			return self::TYPE_CONTACT;
		}

		if ( isset( $data['min'] ) && isset( $data['max'] ) ) {
			return self::TYPE_RANGE;
		}

		if ( isset( $data['value'] ) ) {
			return self::TYPE_FIXED;
		}

		return null;
	}

	public static function sanitize( $data, $unit_type ) {
		if ( ! is_array( $data ) ) {
			return array();
		}

		$config     = PufferGo_Unit_Config::get_instance();
		$valid_units = $config->get_unit_codes( $unit_type );

		if ( isset( $data['type'] ) && self::TYPE_CONTACT === $data['type'] ) {
			return array( 'type' => self::TYPE_CONTACT );
		}

		$result = array();

		if ( isset( $data['min'] ) && isset( $data['max'] ) ) {
			$result['min'] = floatval( $data['min'] );
			$result['max'] = floatval( $data['max'] );
		} elseif ( isset( $data['value'] ) ) {
			$result['value'] = floatval( $data['value'] );
		}

		if ( isset( $data['unit'] ) && in_array( $data['unit'], $valid_units, true ) ) {
			$result['unit'] = $data['unit'];
		}

		return $result;
	}
}
