<?php

/**
 *
 * @author Peter Csubak (chuby)
 * @copyright Peter Csubak (chuby)
 */
class PEntityView extends PComponent{
	
	/** @CProperty(setter="setEntity") */
	protected $entity;
	/** @CProperty */
	protected $fields;
	/** @CProperty */ 
	protected $editableFields;
	/** @CProperty */
	protected $saveBtnText;
	/** @CProperty */
	protected $width;
	/** @CProperty */
	protected $style;
	/** @CProperty(setter="setSessionPH") */
	protected $sessionPH;
	
	public static $evtOnEditField = 1;
	public static $evtOnSaveChanges = 2;
	public static $evzOnDiscardChanges = 3;
	
	private static function generateRandomString($length = 10, $letters = '1234567890qwertyuiopasdfghjklzxcvbnm'){
		$s = '';
		$lettersLength = strlen($letters)-1;

		for ($i = 0 ; $i < $length ; $i++){
			$s .= $letters[rand(0, $lettersLength)];
		}

		return $s;
	}
	
	public function __construct($entity = null, $fields = null, $editableFields = null, $saveBtnText = "", $width = "100%", $id = "", $cssclass = "", $style = "", $evtslock = ""){
		$this->setEntity($entity);
		$this->editableFields = $editableFields;
		$this->fields = $fields;
		$this->width = $width;
		$this->id = ($id == "") ? self::generateRandomString() : $id;
		$this->cssclazz = $cssclass;
		$this->style = $style;
		$this->eventsLock = $evtslock;
		$this->saveBtnText = $saveBtnText;
	}
	
	public function setEntity($ent){
		if ($ent instanceof Entity){
			$this->entity = $ent;
		}
	}
	
	public function setSessionPH($sph){
		if ($sph instanceof SessionPeaHandler){
			$this->sessionPH = $sph;
		}
	}
	
	private static function getEntityFields($entity){
		$res = array();
		$reflect = new ReflectionClass(get_class($entity));
		$props = $reflect->getProperties();
		for ($i = 0; $i < count($props); $i++){
			$property = $props[$i];
			$name = $property->getName();
			$res[$name] = $entity->__get($name);
		}
		return $res;
	}
	
	private static function isId($entity, $fieldName){
		$ref = new ReflectionAnnotatedProperty(get_class($entity), $fieldName);
		return $ref->hasAnnotation("Id");
	}
	
	private static function getEntityId($entity){
		foreach (self::getEntityFields($entity) as $key => $v){
			if (self::isId($entity, $key)){
				return $v;
			}
		}
		throw new ComponentException("Az egyik megjeleníteni kívánt entitás nem tartalmaz elsődleges kulcsot.");
	}
	
	private static function getEntityIdName($entity){
		foreach (self::getEntityFields($entity) as $key => $v){
			if (self::isId($entity, $key)){
				return $key;
			}
		}
		throw new ComponentException("Az egyik megjeleníteni kívánt entitás nem tartalmaz elsődleges kulcsot.");
	}
	
	public function render(){
		if ($this->id == ""){
			$this->id = self::generateRandomString();
		}
		
		if ($this->entity === null){
			return "";
		}
		
		if (!is_array($this->fields)){
			return ""; 
		}
		
		$table = new PTable(0, 0, $this->width, $this->id, $this->cssclazz, $this->style, $this->id);
		
		$i = 0;
		foreach ($this->fields as $key => $value){
			$row = $table->addRow();
			$cell1 = $row->addCell();
			$cell1->content = $key;
			$cell2 = $row->addCell();
			$cell2->id = $this->id."_field".$i++;
			$cell2->content = $this->entity->$value;
			$cell2->activateEvents(PCell::$evtOnDoubleClick);
		}
		
		$div = new PDiv($table->render().Translator::Translate("A szerkeszthető mezők szerkesztéséhez kattints duplán a mezőre."));
		
		return $div->render();
	}
	
	public function getEventKey(){
		return $this->id;
	}
	
	/** @Trigger(eventType="AjaxOnClick", senderType="PCell") */
	public function onCellClick($event){
		$sf = $event->getSendedFrom();
		$regs = array();
		if (ereg("^".$this->id."_(.+)_field([0-9]+)", $sf, $regs)){
			$id = $regs[1];
			$num = $regs[2];
			
			$i = 0;
			$fieldn = "";
			foreach ($this->fields as $fieldName){
				if ($i++ == ((int) $num)){
					$fieldn = $fieldName;
					break;
				}
			}
			
			// TODO Ezt mág át kell írni, csak az események sincsenek még kész.
			$evt2 = new OnEntityListFieldClick();
			$evt2->fieldName = $fieldn;
			$evt2->entity = $ent;
			$evt2->setSender($this);
			if ($this->eventsLock != ""){
				$evt2->setLock($this->eventsLock);
			}
			
			EventHandler::sendEvent($evt2);
		}else{
			throw new ComponentException("Nem sikerült felismerni a cellát.", null, 702);
		}
	}

}
?>