<?php

namespace Engine\Form\Input;

/**
 * base multiple-checkboxes class
 * @author Lucian
 */
class Checkboxes extends \Engine\Form\Input\ElementAbstract
{
	protected $class = "checkboxes";
	protected $possibleValues = [];

	/**
	 * Create a new array of checkboxes
	 * @param String $label
	 * @param String $name (optional)
	 * @param Array $value (optional)
	 * @param Array $possibleValues (optional) associative list. key=>LANG
	 */
	public function __construct($label=null, $name=null, $value=null, $possibleValues=null)
	{
		if($possibleValues){
			$this->possibleValues = $possibleValues;
		}

		parent::__construct($label, $name, $value);

		/**
		 * renderMode:
		 *  - inline  	- renders elements inline, floating, trying to not break things up when the row breaks and crosses to a new line
		 *  - block  	- each checkbox is displayed on its own line
		 *  - aligned 	- try to align the inputs vertically, for now its 45% wide, and floating
		 */
		$this->setStyle('renderMode', 'inline');
	}

	/**
	 * Set the element value - forces it to be an array.
	 * @param Mixed $value
	 */
	public function setValue($value)
	{
		$this->value = (Array)$value;
		return $this;
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\ElementAbstract::getTemplateParams()
	 */
	protected function getTemplateParams($templateClass = null, $parentElement = null)
	{
		$params = parent::getTemplateParams($templateClass, $parentElement );
		$params['possibleValues'] =  $this->getPossibleValues();

		return $params;
	}

	/**
	 * Get the list of possible values
	 * @return Array
	 */
	public function getPossibleValues()
	{
		if (!$this->possibleValues) {
			$this->possibleValues = $this->determinePossibleValues();
 		}
		return $this->possibleValues;
	}

	/**
	 * Set the list of possible values
	 * @param Array $possibleValues associative list. key=>LANG
	 */
	public function setPossibleValues($possibleValues)
	{
		$this->possibleValues = $possibleValues;
		return $this;
	}

	/**
 	 * Determine the possible values that this checkbox can display.
 	 */
 	protected function determinePossibleValues()
 	{
		return [];
 	}

 	public function getDefaultValue()
 	{
 		// js likes the values to be set as keys
 		return array_flip($this->defaultValue);
 	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\ElementAbstract::renderFilterOverview()
	 */
	public function renderFilterOverview()
	{
		$pvalues = $this->getPossibleValues();
		$values = $this->getValue();

		$ret = [];
		foreach ($values as $v) {
			$ret[] = $pvalues[$v];
		}
		if ($ret) {
			return sprintf("%s", implode(", ", $ret));
		}
		else {
			return sprintf("(none)");
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\ElementAbstract::shouldRenderOverview()
	 */
	public function shouldRenderOverview()
	{
		if ($this->getDefaultValue()===null) {
			throw new \Exception("You did not set any default value for this input");
		}
		$cv = array_values($this->getvalue());
		sort($cv);

		$dv = array_keys($this->getDefaultValue());
		sort($dv);

		return $cv != $dv;
	}
}
