<?php
namespace ALPCAL\Remote;
/**
 * AlpineCalendar.ca Remote fetch class
 */

Class RegistrationAlpineontarioCa_xml_aoaevents extends \ALPCAL\Models\Remote implements \ALPCAL\Interfaces\Remote
{
	public $buildUrl = "http://alpinecalendar.ca/api/?data=events&which_cal=[which_cal]&date_start=[date_start]&date_end=[date_end]";

	function __construct($alpcal_options)
	{
		$this->alpcal_options = $alpcal_options;
		$this->renderTime = time();
  	}
	
	/**
	 * Returns array of events in specified format
	 */
	public function returnEvents()
	{
		// build the URL
		if (!$this->stDt) { throw new \Exception("stDt not set in Remote\RegistrationAlpineontarioCa_xml_aoaevents"); }
		if (!$this->enDt) { throw new \Exception("enDt not set in Remote\RegistrationAlpineontarioCa_xml_aoaevents"); }
		$this->buildUrl = str_replace("[which_cal]",$this->alpcal_options['which_cal'],$this->buildUrl);
		$this->buildUrl = str_replace("[date_start]",$this->stDt->format('Y/m/d'),$this->buildUrl);
		$this->buildUrl = str_replace("[date_end]",$this->enDt->format('Y/m/d'),$this->buildUrl);
		
		try {
			// fetch via curl
			$GetRemoteFile = new \ALPCAL\Util\GetRemoteFile;
			$GetRemoteFile->set_url($this->buildUrl);
			$getEvents = $GetRemoteFile->get('curl');
			}
		catch(\Exception $e)
			{
			if ($e->getCode() == 22)
				{
				// there was an error thrown receiving
				die("Error: failed to retrieve file - ".$e->getMessage());
				}
			throw new \Exception($e->getMessage());
			}
		$eventsDecoded = json_decode($getEvents);
		
		return $eventsDecoded;
		
	}
	
	
	/**
	 * Returns array of categories in specified format
	 */
	public function returnCategories($inSpecifiedDateRange=false)
	{
		
	}
	
}
