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

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

trait ConditionableStringTrait {

	public function fieldConditionableInfo( IField $field, $isServerSide = false ) {
		return array(
			'' => array(
				'compare' => array( 'value', '!value', '^value', '$value', '*value', '!*value', 'empty', 'filled' ),
			),
		);
	}

	public function fieldConditionableRule( IField $field, $compare, $value = null, $name = '' ) {
		switch ( $compare ) {
			case 'value':
			case '!value':
			case '^value':
			case '$value':
			case '*value':
			case '!*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 fieldConditionableMatch( IField $field, array $rule, array $values, IEntity $entity ) {
		switch ( $rule['type'] ) {
			case 'value':
			case '!value':
				if ( empty( $values ) ) {
					return $rule['type'] === '!value';
				}

				foreach ( (array) $rule['value'] as $rule_value ) {
					foreach ( $values as $input ) {
						if ( is_array( $input ) ) {
							$input = $input[ isset( $this->_valueColumn ) ? $this->_valueColumn : 'value' ];
						}
						if ( $input == $rule_value ) {
							if ( $rule['type'] === '!value' ) {
								return false;
							}
							continue 2;
						}
					}
					// One of rules did not match
					if ( $rule['type'] === 'value' ) {
						return false;
					}
				}
				// All rules matched or did not match.
				return true;
			case '^value':
			case '$value':
				if ( ! empty( $values ) ) {
					foreach ( $values as $input ) {
						if ( is_array( $input ) ) {
							$input = $input[ isset( $this->_valueColumn ) ? $this->_valueColumn : 'value' ];
						}
						foreach ( (array) $rule['value'] as $rule_value ) {
							if ( false === $pos = strpos( $input, $rule_value ) ) {
								continue;
							}

							if ( $rule['type'] === '^value' ) {
								if ( $pos === 0 ) {
									return true;
								}
							} elseif ( $rule['type'] === '$value' ) {
								if ( substr( $input, $pos ) === $rule_value ) {
									return true;
								}
							}
						}
					}
				}
				return false;
			case '*value':
			case '!*value':
				if ( ! empty( $values ) ) {
					foreach ( $values as $input ) {
						if ( is_array( $input ) ) {
							$input = $input[ isset( $this->_valueColumn ) ? $this->_valueColumn : 'value' ];
						}
						foreach ( (array) $rule['value'] as $rule_value ) {
							if ( false !== strpos( $input, $rule_value ) ) {
								return $rule['type'] === '*value';
							}
						}
					}
				}
				return $rule['type'] === '!*value';
			case 'empty':
			case 'filled':
				$_values_to_check = array();
				foreach ( $values as $input ) {
					if ( is_array( $input ) ) {
						$input = $input[ isset( $this->_valueColumn ) ? $this->_valueColumn : 'value' ];
					}
					if ( strlen( $input ) ) {
						$_values_to_check[] = $input;
					}
				}
				return $rule['type'] === 'empty' ? empty( $_values_to_check ) === $rule['value'] : ! empty( $_values_to_check ) === $rule['value'];
			default:
				return false;
		}
	}
}
