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

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

trait ChoiceTrait {

	protected function _getFieldEntryExample( IField $field ) {
		$settings = $field->getFieldSettings();
		if ( ! empty( $settings['options']['options'] ) ) {
			return implode( ',', array_slice( array_keys( $settings['options']['options'] ), 0, 4 ) );
		}
		return 'aaa,bb,cccc';
	}

	public function fieldQueryableInfo( IField $field, $inAdmin = false ) {
		if ( $inAdmin ) {
			$tip = __( 'Enter values separated with commas.', 'directories-pro' );
		} else {
			$tip = __( 'Enter values separated with commas or "_current_" for values of current post/term if any.', 'directories-pro' );
		}
		return array(
			'example' => $this->_getFieldEntryExample( $field ),
			'tip'     => $tip,
		);
	}

	public function fieldConditionableInfo( IField $field, $isServerSide = false ) {
		return array(
			'' => array(
				'compare' => array( 'value', '!value', 'one', 'empty', 'filled' ),
				'tip'     => __( 'Enter values separated with commas.', 'directories-pro' ),
				'example' => $this->_getFieldEntryExample( $field ),
			),
		);
	}

	public function fieldConditionableRule( IField $field, $compare, $value = null, $name = '' ) {
		switch ( $compare ) {
			case 'value':
			case '!value':
			case 'one':
				$value = trim( $value );
				if ( strpos( $value, ',' ) ) {
					if ( ! $value = explode( ',', $value ) ) {
						return;
					}

					$value = array_map( 'trim', $value );
				}
				return array(
					'type'  => $compare,
					'value' => $value,
				);
			case 'empty':
				return array(
					'type'  => 'filled',
					'value' => false,
				);
			case 'filled':
				return array(
					'type'  => 'empty',
					'value' => false,
				);
			default:
				return;
		}
	}

	public function fieldHumanReadableText( IField $field, IEntity $entity, $separator = null, $key = null ) {
		return implode( isset( $separator ) ? $separator : ', ', $this->_getHumanReadableTextArray( $field, $entity ) );
	}

	protected function _getHumanReadableTextArray( IField $field, IEntity $entity ) {
		$ret = array();
		if ( $values = $entity->getFieldValue( $field->getFieldName() ) ) {
			$settings = $field->getFieldSettings();
			foreach ( $values as $value ) {
				if ( isset( $settings['options']['options'][ $value ] ) ) {
					$ret[] = $settings['options']['options'][ $value ];
				}
			}
		}
		return $ret;
	}

	public function fieldLabellableLabels( IField $field, IEntity $entity ) {
		if ( ! $values = $entity->getFieldValue( $field->getFieldName() ) ) {
			return;
		}

		$settings = $field->getFieldSettings();
		$ret      = array();
		foreach ( $values as $value ) {
			if ( isset( $settings['options']['options'][ $value ] ) ) {
				$ret[] = $settings['options']['options'][ $value ];
			}
		}
		return $ret;
	}

	public function fieldSchemaProperties( IField $field ) {
		return array( 'priceRange' );
	}

	public function fieldSchemaRenderProperty( IField $field, $property, IEntity $entity ) {
		if ( ( ! $value = $entity->getSingleFieldValue( $field->getFieldName() ) )
			|| ! isset( $settings['options']['options'][ $value ] )
		) {
			return;
		}

		return $settings['options']['options'][ $value ];
	}

	public function fieldWpRestApiSchema( IField $field ) {
		return array(
			'type'  => 'array',
			'items' => array(
				'type' => 'string',
			),
		);
	}

	public function fieldWpRestApiGetValue( IField $field, IEntity $entity, $request ) {
		$values = (array) $entity->getFieldValue( $field->getFieldName() );
		switch ( $request['context'] ) {
			case 'edit':
				return $values;
			default:
				return $this->_getHumanReadableTextArray( $field, $entity );
		}
	}
}
