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

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

class IconRenderer extends AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'label'            => __( 'Icon', 'directories-pro' ),
			'field_types'      => array( $this->_name, 'number', 'price' ),
			'default_settings' => array(
				'size'  => '',
				'color' => array(
					'type'       => '',
					'custom'     => null,
					'_separator' => ', ',
				),
				'icon'  => null,
			),
		);
	}

	protected function _fieldRendererSettingsForm( IField $field, array $settings, array $parents = array() ) {
		$form = array(
			'size'  => array(
				'#type'          => 'select',
				'#title'         => __( 'Icon size', 'directories-pro' ),
				'#default_value' => $settings['size'],
				'#options'       => $this->_application->System_Icon_sizeOptions(),
				'#weight'        => 5,
			),
			'color' => array( '#weight' => 6 ) + $this->_application->System_Icon_colorSettingsForm(
				$field->Bundle,
				isset( $settings['color'] ) && is_array( $settings['color'] ) ? $settings['color'] : array(),
				array_merge( $parents, array( 'color' ) )
			),
		);
		if ( $field->getFieldType() !== $this->_name ) {
			$form['icon'] = array(
				'#title'         => __( 'Icon', 'directories-pro' ),
				'#type'          => 'iconpicker',
				'#default_value' => $settings['icon'],
				'#weight'        => 1,
			);
		}

		return $form;
	}

	protected function _fieldRendererRenderField( IField $field, array &$settings, Entity\Type\IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		$style = ( $color = $this->_getIconColor( $field, $settings, $entity ) ) ? 'style="background-color:' . $color . ';color:#fff;"' : '';
		$class = $settings['size'] ? 'drts-icon-' . $settings['size'] . ' ' : '';
		$icons = array();
		foreach ( $values as $value ) {
			if ( ( ! $icon_class = $this->_getIconClass( $field, $settings, $value ) )
				|| ( 0 >= $count = $this->_getIconCount( $field, $settings, $value ) )
			) {
				continue;
			}

			$icon = '<i ' . $style . ' class="' . $this->_application->H( $class . $icon_class ) . '"></i> ';
			if ( $count > 1 ) {
				if ( $count > 100 ) {
					$icon = str_repeat( $icon, 100 ) . '...(' . $count . ')';
				} else {
					$icon = str_repeat( $icon, $count );
				}
			}
			$icons[] = $icon;
		}

		return implode( $settings['_separator'], $icons );
	}

	protected function _fieldRendererReadableSettings( IField $field, array $settings ) {
		$ret = array();
		if ( $field->getFieldType() !== $this->_name ) {
			$ret['icon'] = array(
				'label' => __( 'Icon', 'directories-pro' ),
				'value' => $settings['icon'],
			);
		}
		$ret['size'] = array(
			'label' => __( 'Icon size', 'directories-pro' ),
			'value' => $this->_application->System_Icon_sizeOptions()[ $settings['size'] ],
		);
		if ( $color = $this->_getIconColor( $field, $settings ) ) {
			$ret['color'] = array(
				'label' => __( 'Icon color', 'directories-pro' ),
				'value' => $color,
			);
		}
		return $ret;
	}

	protected function _getIconClass( IField $field, array $settings, $value ) {
		return isset( $settings['icon'] ) ? $settings['icon'] : $value;
	}

	protected function _getIconCount( IField $field, array $settings, $value ) {
		return ( $field->getFieldType() === $this->_name ) || is_array( $value ) ? 1 : (int) $value;
	}

	protected function _getIconColor( IField $field, array $settings, ?Entity\Type\IEntity $entity = null ) {
		if ( $settings['color']['type'] === '_custom' ) {
			return $settings['color']['custom'];
		}
		if ( $settings['color']['type'] !== ''
			&& isset( $entity )
			&& ( $color = $entity->getSingleFieldValue( $settings['color']['type'] ) )
		) {
			return $color;
		}
	}
}
