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

use SabaiApps\Directories\Component\Field;
use SabaiApps\Directories\Exception;
use SabaiApps\Framework\Criteria;

class FieldQuery extends Field\Query {

	protected $_bundleName;

	public function taxonomyTermIdIs( $taxonomyBundleType, $id, $ignoreAuto = true, $alias = null, $name = null ) {
		$this->taxonomyTermIdIn( $taxonomyBundleType, array( $id ), $ignoreAuto, $alias, $name );
	}

	public function taxonomyTermIdIn( $taxonomyBundleType, array $ids, $ignoreAuto = false, $alias = null, $name = null ) {
		$criteria_name = isset( $name ) ? $name : $taxonomyBundleType;
		if ( $this->hasNamedCriteria( $criteria_name ) ) {
			foreach ( $this->getNamedCriteria( $criteria_name ) as $criteria ) {
				if ( $criteria instanceof Criteria\InCriteria ) {
					foreach ( $criteria->getArray() as $_term_id ) {
						$ids[] = $_term_id;
					}
				} elseif ( $criteria instanceof Criteria\IsCriteria ) {
					$ids[] = $criteria->getValue();
				}
			}
			$this->removeNamedCriteria( $criteria_name );
		}
		$this->fieldIsIn( $taxonomyBundleType, $ids, 'value', $alias, null, $name );

		// whether or not to ignore terms automatically added by the system, such as parent terms that were not selected by the user explicitly
		if ( $ignoreAuto ) {
			$this->fieldIsNot( $taxonomyBundleType, true, 'auto', $alias, null, $name );
		}
	}

	public function taxonomyTermTitleContains( $taxonomyBundleName, $taxonomyBundleType, $string ) {
		throw new Exception\RuntimeException( 'Call to unsupported method: ' . __METHOD__ );
	}

	public function fieldIs( $field, $value, $column = 'value', $alias = null, $on = null, $name = null, $entityType = null ) {
		if ( $field === 'bundle_name' ) {
			$this->_bundleName = $value;
		}
		return parent::fieldIs( $field, $value, $column, $alias, $on, $name, $entityType );
	}

	public function fieldIsIn( $field, array $values, $column = 'value', $alias = null, $on = null, $name = null, $entityType = null ) {
		if ( $field === 'bundle_name' ) {
			$this->_bundleName = array_values( $values );
		}
		return parent::fieldIsIn( $field, $values, $column, $alias, $on, $name, $entityType );
	}

	public function getQueriedBundleName() {
		return $this->_bundleName;
	}

	public function removeBundleNameCriteria( $name = 'bundle_name' ) {
		$this->_bundleName = null;
		if ( ! $criteria = $this->removeNamedCriteria( $name, true ) ) {
			return array();
		}

		$bundle_names = array();
		foreach ( $criteria as $_criteria ) {
			if ( $_criteria instanceof Criteria\IsCriteria ) {
				$bundle_names[] = $_criteria->getValue();
			} elseif ( $_criteria instanceof Criteria\InCriteria ) {
				foreach ( $_criteria->getArray() as $_bundle_name ) {
					$bundle_names[] = $_bundle_name;
				}
			}
		}
		return $bundle_names;
	}

	public function addBundleNameCriteria( $bundleName, $name = 'bundle_name' ) {
		$bundle_names = $this->removeBundleNameCriteria( $name );
		foreach ( (array) $bundleName as $_bundle_name ) {
			$bundle_names[] = $_bundle_name;
		}
		return $this->fieldIsIn( 'bundle_name', $bundle_names );
	}
}
