<?php

require_once 'class.callforwardtargetproperty.php';

abstract class AbstractCallForwardTarget {
	private $mType = "";
	private $mTitle = "";
	private $mFwNumber = "";
	private $mFwVoicemailFile = "";
	
	private $mParent = null;
	
	private $mTargetProperties = array(); //key value Store
	private $mTargetPropertyTypes = array();
		
	public function __construct($parent){
		$this->mParent = $parent;
	}
	
	public function setParent($parent) {
		$this->mParent = $parent;
	}
	
	public function getParent() {
		$this->mParent;
	}
	
	public function setCallForwardType($type) {
		$this->mType = $type;
	}
	
	public function getCallForwardType() {
		return $this->mType;
	}
	
	public function setTitle($title) {
		$this->mTitle = $title;
	}
	
	public function getTitle() {
		return $this->mTitle;
	}
	
	public function setProperty($key, $value) {
		$this->mTargetProperties[$key] = $value;
	}
	
	public function getProperty($key, $default="") {
		if (array_key_exists($key,$this->mTargetProperties)) {
			return $this->mTargetProperties[$key];
		} else {
			return $default;
		}
	}
	
	protected function addPropertyType(CallForwardTargetProperty $property) {
		$this->mTargetPropertyTypes[] = $property;
	}
	
	abstract public function getHumanReadableDescription();
	
		
	public function getCallForwardNumber() {
		return $this->mFwNumber;
	}
	
	public function setCallForwardNumber($number) {
		$this->mFwNumber = $number;
	}
	
	public function getCallForwardVoiceMailFileName() {
		return $this->mFwVoicemailFile;
	}
	
	public function setCallForwardVoiceMailFileName($filename) {
		$this->mFwVoicemailFile = $filename;
	}
	
	public function descriptionToJSON() {
		$json_Data = array();
		$json_Data['type'] = $this->mType;
		$json_Data['title'] = $this->mTitle;
		$json_Data['propertyTypes'] = array();
		foreach ($this->mTargetPropertyTypes as $type) {
			$json_Data['propertyTypes'][] = json_decode($type->descriptionToJSON());
		}
		
		return json_encode($json_Data);
	}
	
	public function toJSON() {
		$json_Data = array();
		$json_Data['type'] = $this->mType;
		$json_Data['title'] = $this->mTitle;
		$json_Data['desc'] = $this->getHumanReadableDescription();
		$json_Data['properties'] = new stdclass(); //have to use an stdclass object cause an assoziative array will be conformed to an json object anyway
		foreach ($this->mTargetProperties as $key => $value) {
			$json_Data['properties']->$key = $value;
		}
		//$json_Data['properties']["test"] = "foo";
		
		return json_encode($json_Data);
	}
	
	//should return a dialable Target for the dialplan eg a number or vm* whatever
	abstract public function getDialplanTargetNumber();

	//This function have to return an assoziative array (key/value) for settings asterisk dialplan channel vars
	public function getExtraDialplanVars() {
		return array();
	}
	
	
}

