<?php
namespace SabaiApps\Directories\Component\Field\Renderer;

use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class PriceRenderer extends AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'field_types'      => array( 'price' ),
			'default_settings' => array(
				'symbol_pos' => '',
				'_separator' => ', ',
			),
		);
	}

	protected function _fieldRendererSettingsForm( IField $field, array $settings, array $parents = array() ) {
		return array(
			'symbol_pos' => array(
				'#type'          => 'select',
				'#title'         => __( 'Currency symbol position', 'directories-pro' ),
				'#options'       => $this->_getCurrencySymbolPositionOptions(),
				'#default_value' => $settings['symbol_pos'],
			),
		);
	}

	protected function _getCurrencySymbolPositionOptions() {
		return array(
			''       => __( 'Auto', 'directories-pro' ),
			'before' => __( 'Before value', 'directories-pro' ),
			'after'  => __( 'After value', 'directories-pro' ),
		);
	}

	protected function _fieldRendererRenderField( IField $field, array &$settings, IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		switch ( $settings['symbol_pos'] ) {
			case 'before':
				$currency_position = 0;
				break;
			case 'after':
				$currency_position = 1;
				break;
			default:
				$currency_position = true;
		}
		foreach ( array_keys( $values ) as $i ) {
			$currency     = $values[ $i ]['currency'];
			$values[ $i ] = $this->_application->System_Currency_format(
				$values[ $i ]['value'],
				$currency,
				$currency_position
			);
		}
		return implode( $settings['_separator'], $values );
	}

	protected function _fieldRendererReadableSettings( IField $field, array $settings ) {
		$symbol_positions = $this->_getCurrencySymbolPositionOptions();
		return array(
			'symbol_pos' => array(
				'label' => __( 'Currency symbol position', 'directories-pro' ),
				'value' => isset( $symbol_positions[ $settings['symbol_pos'] ] ) ? $symbol_positions[ $settings['symbol_pos'] ] : __( 'Auto', 'directories-pro' ),
			),
		);
	}
}
