<?php
namespace ALPCAL\Remote;
/**
 * AlpineCalendar.ca Remote fetch class for Categories
 * NOTE: Does not follow model for events
 */

Class Categories extends \ALPCAL\Models\Remote
{
	public $buildUrl = "http://alpinecalendar.ca/api/?data=eventTypes&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 eventTypes in specified format
	 */
	public function returnEventTypes()
	{
		// build the URL
		if (!$this->stDt) { throw new \Exception("stDt not set in Remote\AlpineCalendar_ca"); }
		if (!$this->enDt) { throw new \Exception("enDt not set in Remote\AlpineCalendar_ca"); }
		$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 (object) $eventsDecoded;
		
	}
	
		
}
