<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

class WV_Form_Repeat extends sly_Form_ElementBase implements sly_Form_IElement {
	protected $elements;
	protected $group_number = 0;
	protected $meta_type    = '';
	protected $meta_name    = '';

	/**
	 * Constructor
	 *
	 * @param string $name        element name
	 * @param string $label       the label
	 * @param string $meta_type
	 * @param string $meta_namel
	 * @param string $id          optional ID (if not given, the name is used)
	 */
	public function __construct($name, $label, $meta_type = '', $meta_name = '', $id = null) {
		parent::__construct($name, $label, '', $id);

		$this->meta_type = $meta_type;
		$this->meta_name = $meta_name;
		$this->elements  = array();

		$this->addOuterClass('wv-form-repeat-row');
	}

	public function add(sly_Form_IElement $element) {
		$this->elements[$this->group_number][] = $element;
		return $this;
	}

	public function clear() {
		$this->elements = array();
		return $this;
	}

	public function nextGroup() {
		$this->group_number = $this->group_number + 1;
	}

	public function setLabelStyle($label_style) {
		$this->elements[$this->group_number][] = array('label_style' => $label_style);
	}

	public function setLabelAttr($label_attr) {
		$this->elements[$this->group_number][] = array('label_attr' => $label_attr);
	}

	public function setNewline($newline) {
		$this->elements[$this->group_number][] = array('newline' => $newline);
	}

	public function hideStart() {
		$this->elements[$this->group_number][] = array('hide' => 'start');
	}

	public function hideEnd() {
		$this->elements[$this->group_number][] = array('hide' => 'end');
	}

	/**
	 * Renders the element
	 *
	 * This method renders the form element and returns its XHTML code.
	 *
	 * @return string  the XHTML code
	 */
	public function render() {
		$output = $this->renderGroup();
		$base   = sly_Util_AddOn::assetBaseUri('webvariants/developer-utils');

		sly_Core::getLayout()->addJavaScriptFile($base.'js/repeat-input.js');

		return sprintf('<div class="form-repeat-container" data-metaname="%s" data-metatype="%s" data-name="%s">%s</div>', $this->meta_name, $this->meta_type, sly_html($this->getName()), $output);
	}

	protected function renderRow($group) {
		$output      = '<div class="form-repeat-row">';
		$label_attr  = null;
		$label_style = null;
		$newline     = array();

		foreach ($group as $element) {
			/* @var $element sly_Form_ElementBase */
			if (is_array($element)) {
				if (array_key_exists('label_attr', $element)) {
					$label_attr = $element['label_attr'];
				}

				if (array_key_exists('label_style', $element)) {
					$label_style = $element['label_style'];
				}

				if (array_key_exists('newline', $element)) {
					$newline = (array) $element['newline'];
				}

				if (array_key_exists('hide', $element)) {
					switch ($element['hide']) {
						case 'start': $output .= '<span style="display:none">'; break;
						case 'end':   $output .= '</span>';                     break;
					}
				}
			}
			else {
				if (!is_array($label_attr)) {
					$label_attr = array();
				}

				$label_attr['for'] = $element->getID();
				$label_style_str = '';

				if (is_array($label_style)) {
					foreach ($label_style as $sel => $sty) {
						$label_style_str .= $sel.':'.$sty.';';
					}
				}

				if ($label_style_str) {
					$label_attr['style'] = $label_style_str;
				}

				if (in_array('before', $newline)) {
					$output .= '<br />';
				}

				if ($element->getLabel()) {
					$output .= sprintf('<label %s>%s</label>', sly_Util_HTML::buildAttributeString($label_attr), sly_html($element->getLabel()));

					if (in_array('label', $newline)) {
						$output .= '<br />';
					}
				}

				$output .= $element->render();

				if (in_array('widget', $newline)) {
					$output .= '<br />';
				}

				$label_attr  = null;
				$label_style = null;
				$newline     = array();
			}
		}

		return $output.'</div>';
	}

	public function renderGroup() {
		$output = '';

		foreach ($this->elements as $group) {
			$output .= $this->renderRow($group);
		}

		return $output;
	}

	public function getDisplayValue() {
		return '';
	}
}
