<?php
/**
 * PufferGo Unit Configuration
 *
 * Centralized configuration for units (currency, quantity, time, etc.)
 * Used by both PHP backend and frontend (via REST API).
 *
 * @package PufferGo
 */

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

class PufferGo_Unit_Config {

	const VERSION = '1.0.0';

	private static $instance = null;

	private $config = null;

	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->config = $this->get_default_config();
	}

	private function get_default_config() {
		return array(
			'currency' => array(
				'USD' => array(
					'symbol'   => '$',
					'position' => 'before',
					'decimal'  => 2,
					'labels'   => array(
						'en_US' => 'US Dollar',
						'zh_CN' => '美元',
					),
				),
				'EUR' => array(
					'symbol'   => '€',
					'position' => 'after',
					'decimal'  => 2,
					'labels'   => array(
						'en_US' => 'Euro',
						'zh_CN' => '欧元',
					),
				),
				'CNY' => array(
					'symbol'   => '¥',
					'position' => 'before',
					'decimal'  => 2,
					'labels'   => array(
						'en_US' => 'Chinese Yuan',
						'zh_CN' => '人民币',
					),
				),
				'GBP' => array(
					'symbol'   => '£',
					'position' => 'before',
					'decimal'  => 2,
					'labels'   => array(
						'en_US' => 'British Pound',
						'zh_CN' => '英镑',
					),
				),
				'JPY' => array(
					'symbol'   => '¥',
					'position' => 'before',
					'decimal'  => 0,
					'labels'   => array(
						'en_US' => 'Japanese Yen',
						'zh_CN' => '日元',
					),
				),
				'HKD' => array(
					'symbol'   => 'HK$',
					'position' => 'before',
					'decimal'  => 2,
					'labels'   => array(
						'en_US' => 'Hong Kong Dollar',
						'zh_CN' => '港币',
					),
				),
			),
			'quantity' => array(
				'pieces' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'pieces',
						'zh_CN' => '件',
					),
				),
				'units' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'units',
						'zh_CN' => '个',
					),
				),
				'cartons' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'cartons',
						'zh_CN' => '箱',
					),
				),
				'boxes' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'boxes',
						'zh_CN' => '盒',
					),
				),
				'sets' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'sets',
						'zh_CN' => '套',
					),
				),
				'pairs' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'pairs',
						'zh_CN' => '对',
					),
				),
				'kg' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'kg',
						'zh_CN' => '千克',
					),
				),
				'tons' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'tons',
						'zh_CN' => '吨',
					),
				),
				'lbs' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'lbs',
						'zh_CN' => '磅',
					),
				),
				'meters' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'meters',
						'zh_CN' => '米',
					),
				),
				'feet' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'feet',
						'zh_CN' => '英尺',
					),
				),
			),
			'time' => array(
				'days' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'days',
						'zh_CN' => '天',
					),
				),
				'weeks' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'weeks',
						'zh_CN' => '周',
					),
				),
				'months' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'months',
						'zh_CN' => '月',
					),
				),
				'hours' => array(
					'position' => 'after',
					'labels'   => array(
						'en_US' => 'hours',
						'zh_CN' => '小时',
					),
				),
			),
		);
	}

	public function get_config() {
		return $this->config;
	}

	public function get_unit_types() {
		return array_keys( $this->config );
	}

	public function get_units( $type ) {
		if ( ! isset( $this->config[ $type ] ) ) {
			return array();
		}
		return $this->config[ $type ];
	}

	public function get_unit_codes( $type ) {
		return array_keys( $this->get_units( $type ) );
	}

	public function get_unit_config( $type, $unit ) {
		if ( ! isset( $this->config[ $type ][ $unit ] ) ) {
			return null;
		}
		return $this->config[ $type ][ $unit ];
	}

	public function get_unit_label( $type, $unit, $lang = null ) {
		if ( ! $lang ) {
			$lang = get_locale();
		}
		$unit_config = $this->get_unit_config( $type, $unit );
		if ( ! $unit_config ) {
			return $unit;
		}
		if ( isset( $unit_config['labels'][ $lang ] ) ) {
			return $unit_config['labels'][ $lang ];
		}
		if ( isset( $unit_config['labels']['en_US'] ) ) {
			return $unit_config['labels']['en_US'];
		}
		return $unit;
	}

	public function get_unit_symbol( $type, $unit ) {
		$unit_config = $this->get_unit_config( $type, $unit );
		if ( ! $unit_config ) {
			return '';
		}
		if ( 'currency' === $type && isset( $unit_config['symbol'] ) ) {
			return $unit_config['symbol'];
		}
		return '';
	}

	public function get_unit_position( $type, $unit ) {
		$unit_config = $this->get_unit_config( $type, $unit );
		if ( ! $unit_config ) {
			return 'after';
		}
		return isset( $unit_config['position'] ) ? $unit_config['position'] : 'after';
	}

	public function get_unit_decimal( $type, $unit ) {
		$unit_config = $this->get_unit_config( $type, $unit );
		if ( ! $unit_config ) {
			return 0;
		}
		return isset( $unit_config['decimal'] ) ? $unit_config['decimal'] : 0;
	}

	public function to_array() {
		return array(
			'version' => self::VERSION,
			'config'  => $this->config,
		);
	}

	public static function format_for_js() {
		$instance = self::get_instance();
		return $instance->to_array();
	}
}
