<?php
require_once 'class.abstractcallforwardcriteria.php';

final class CallForwardCriteriaPhoneStatusType {
	const None         = 0x00000000;
	const Busy         = 0x00000001;
	const Offline      = 0x00000010;
	const NoAnswer     = 0x00000100;
	private function __construct(){
	}
	
	public static function toMySQLEnum($type) {
		if($type == CallForwardCriteriaPhoneStatusType::Busy) {
			return "busy";
		} else if ($type == CallForwardCriteriaPhoneStatusType::Offline) {
			return "offline";
		} else if ($type == CallForwardCriteriaPhoneStatusType::NoAnswer) {
			return "noanswer";
		} else {
			return "none";
		}
	}
	
	public static function fromMySQLEnum($type) {
		if($type == "busy" ) {
			return CallForwardCriteriaPhoneStatusType::Busy;
		} else if ($type == "offline") {
			return CallForwardCriteriaPhoneStatusType::Offline;
		} else if ($type == "noanswer") {
			return CallForwardCriteriaPhoneStatusType::NoAnswer;
		} else {
			return CallForwardCriteriaPhoneStatusType::None;
		}
	}
	
}

class CallForwardCriteriaPhoneStatus extends AbstractCallForwardCriteria {
	
	private $mCallForwardCriteriaPhoneStatusType = CallForwardCriteriaPhoneStatusType::Busy;
	private $mCallTimeout = 15;
	
	public function __construct($parent){
		$this->setCriteriaName("PhoneStatus");
		$this->setCriteriaTitle(__("PHONESETTINGSCALLFORWARD_PHONESTATE_GENERAL"));
		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("phonestatus");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_PHONESTATE_IS"));
		$property->setPropertyTypeName("select");
		
		$cap = new CallForwardCriteriaPropertyCapability();
		$cap->setName(CallForwardCriteriaPhoneStatusType::toMySQLEnum(CallForwardCriteriaPhoneStatusType::NoAnswer));
		$cap->setTitle(__("PHONESETTINGSCALLFORWARD_PHONESTATE_CALLTIMEOUT"));
		$calltimeoutproperty = new CallForwardCriteriaProperty();
		$calltimeoutproperty->setPropertyName("calltimeout");
		$calltimeoutproperty->setPropertyTypeName("timeout");
		$cap->addSubCallForwardCriteriaProperty($calltimeoutproperty);
		$property->addPropertyCapability($cap);
		
		$cap = new CallForwardCriteriaPropertyCapability();
		$cap->setName(CallForwardCriteriaPhoneStatusType::toMySQLEnum(CallForwardCriteriaPhoneStatusType::Busy));
		$cap->setTitle(__("PHONESETTINGSCALLFORWARD_PHONESTATE_BUSY"));
		$property->addPropertyCapability($cap);
		
		$cap = new CallForwardCriteriaPropertyCapability();
		$cap->setName(CallForwardCriteriaPhoneStatusType::toMySQLEnum(CallForwardCriteriaPhoneStatusType::Offline));
		$cap->setTitle(__("PHONESETTINGSCALLFORWARD_PHONESTATE_SIGNED_OUT"));
		$property->addPropertyCapability($cap);
		
		
		//$property->addPropertyCapability(CallForwardCriteriaPhoneStatusType::toMySQLEnum(CallForwardCriteriaPhoneStatusType::Busy));
		//$property->addPropertyCapability(CallForwardCriteriaPhoneStatusType::toMySQLEnum(CallForwardCriteriaPhoneStatusType::Offline));
		
		
		
		$this->addPropertyType($property);
		
		
		
	}
	
	public function setMatchedPhoneStatus($type) {
		$this->setProperty("phonestatus", CallForwardCriteriaPhoneStatusType::toMySQLEnum($type));
		
		if ($type == CallForwardCriteriaPhoneStatusType::NoAnswer && $this->getCallTimeOutForNoAnswer() == "") {
			$this->setCallTimeOutForNoAnswer("15");
		}
	}
		
	public function getMatchedPhoneStatus() {
		return CallForwardCriteriaPhoneStatusType::fromMySQLEnum($this->getProperty("phonestatus"));
	}
	
	public function setCallTimeOutForNoAnswer($timeout) {
		$this->setProperty("calltimeout", $timeout);
	}
	
	public function getCallTimeOutForNoAnswer() {
		return $this->getProperty("calltimeout");
	}
	
	public function getCriteriaDescription() {
		
		if ($this->isInverse()) {
			return __("Invers.");
		} else {
			$type = $this->getMatchedPhoneStatus();
			switch ($type) {
				case CallForwardCriteriaPhoneStatusType::Busy :
					return __('PHONESETTINGSCALLFORWARD_PHONESTATE_GENERAL').": ".__('PHONESETTINGSCALLFORWARD_PHONESTATE_BUSY'); 
					break;
				case CallForwardCriteriaPhoneStatusType::Offline :
				    return __('PHONESETTINGSCALLFORWARD_PHONESTATE_GENERAL').": ".__('PHONESETTINGSCALLFORWARD_PHONESTATE_SIGNED_OUT'); 
					break;
				case CallForwardCriteriaPhoneStatusType::NoAnswer :
				    return __('PHONESETTINGSCALLFORWARD_PHONESTATE_GENERAL').": ".__('PHONESETTINGSCALLFORWARD_PHONESTATE_CALLTIMEOUT')." ".CHtml::encode($this->getCallTimeOutForNoAnswer())." ".__("PHONESETTINGSCALLFORWARD_TIMEOUT_SECONDS"); 
					break;
			}
		}
	}

	public function checkIfMatched($variables) {
		
		//is the phonestatus set?
		$currentState = @$variables['phonestatus'];
		if ($currentState == "") {		
			return false;
		}
		//transform into enum
		$currentState = CallForwardCriteriaPhoneStatusType::fromMySQLEnum($currentState);
		
		if ($this->getMatchedPhoneStatus() == $currentState) {
			//current state matches.
			return true;
		}
		return false;
	}

	
}