<?php

require_once('GeoCoder.class.php');
require_once('ReferenceData.class.php');

require_once('RepositoryModel.interface.php');
class RepositoryModel implements iRepositoryModel
{
	private $addresses;
	private $radius;
	private $timePeriod;
	private $dayType;
	private $weatherType;
	private $minTrafficSeverity;
	private $geoCoder;
	private $referenceData;

	public function __construct() {
                $this->addresses = DEFAULT_ADDRESSES;
		$this->radius = DEFAULT_RADIUS;
		$this->timePeriod = DEFAULT_TIMEPERIOD;
		$this->dayType = DEFAULT_DAYTYPE;
		$this->weatherType = DEFAULT_WEATHERTYPE;
		$this->minTrafficSeverity = DEFAULT_MINTRAFFICSEVERITY;
		$this->geoCoder = new GeoCoder();
		$this->referenceData = new ReferenceData();
	}

	public function setLocalData($LocalData)
	{
		$error = "";
		if(!empty($LocalData['address1']))
		{
			if(!empty($LocalData['address2']))
			{
				$this->addresses = array();
				$this->addresses[0] = $this->sanitizeLocalData($LocalData['address1'], 'string');
				$this->addresses[1] = $this->sanitizeLocalData($LocalData['address2'], 'string');
			}
			else
			{
				$this->addresses = $this->sanitizeLocalData($LocalData['address1'], 'string');
			}
		}
		else
			$error .= "<b>Invalid Address!</b><br>";

		if(!empty($LocalData['radius']))
			$this->radius = $this->sanitizeLocalData($LocalData['radius'], 'int', '25');
		else if(is_array($this->addresses))
		{ }
		else
			$error .= "<b>Invalid Radius!</b><br>";

		if(!empty($LocalData['timePeriod']))
			$this->timePeriod = $this->sanitizeLocalData($LocalData['timePeriod'], 'int', '3');

		if(!empty($LocalData['dayType']))
			$this->dayType = $this->sanitizeLocalData($LocalData['dayType'], 'int', '3');

		if(!empty($LocalData['weatherType']))
			$this->weatherType = $this->sanitizeLocalData($LocalData['weatherType'], 'int', '4');

		if(!empty($LocalData['minTrafficSeverity']))
			$this->minTrafficSeverity = $this->sanitizeLocalData($LocalData['minTrafficSeverity'], 'int', '5');
		
		if(!empty($error))
			return $error;
		else
			return true;
	}

	private function sanitizeLocalData($data, $type, $max = 1)
	{
		$min = 1;

		switch($type)
		{
			case "int":
				filter_var($data, FILTER_VALIDATE_INT, 
					array("options" => array("min_range"=>$min, "max_range"=>$max)))
					or $data = $min;
				break;
			case "string":
				filter_var($data, FILTER_SANITIZE_STRING)
					or $data = "";
				break;
		}
		return $data;
	}

	public function processData()
	{
        	if($this->geoCoder->lookupCoordinates($this->addresses))
		{
			$this->referenceData->generateStatistics(
				$this->getCoordinates(),
				$this->getRadius(),
				$this->getTimePeriod(),
				$this->getDayType(),
				$this->getWeatherType(), 
				$this->getMinTrafficSeverity());
			return true;
		}
		else
			return "<b>Could not find address(es)!</b><br>";
	}

	public function getCoordinates()
	{
		return $this->geoCoder->getCoordinates();
	}

	public function getPolyLine()
	{
		return $this->geoCoder->getPolyLine();
	}

	public function getRange()
	{
		return $this->referenceData->getRange();
	}

	public function getStatistics()
	{
		return $this->referenceData->getStatistics();
	}

	public function getMean()
	{
		return $this->referenceData->getMean();
	}

	public function getVariance()
	{
		return $this->referenceData->getVariance();
	}

	public function getAddresses()
	{
		return $this->addresses;
	}

	public function getRadius()
	{
		return $this->radius;
	}

	public function getTimePeriod()
	{
		return $this->timePeriod;
	}

	public function getDayType()
	{
		return $this->dayType;
	}

	public function getWeatherType()
	{
		return $this->weatherType;
	}

	public function getMinTrafficSeverity()
	{
		return $this->minTrafficSeverity;
	}
}

?>
