<?php

// require_once('json.php');
// require_once('xml2array.php');

class weathercom_feed {
	private $feed_url;	
	public $feed_contents;
	private $feed_struct;
	private $struct_tags;
	
	private $callback;
	
	function __construct($zip_code, $base_url='http://gatehouse.weather.com/weather/local/', $params=array('cc=*','dayf=7','prod=bd_select','par=gatehousexml')) {
		$this->feed_url = $base_url . $zip_code . '?' . implode('&',$params);
		$this->callback = $callback;
	}
	
	
	// **** PUBLIC ***

	//returns current weather in JSON
	public function parseCurrentWeather($arr=false) {
		$this->fetch_feed();
		$sxml = simplexml_load_string($this->feed_contents);
		
		// day1wDir
		// day1wSpdM 
		// day1pOP
		// day1rH
		
		$f["TemperatureF"] = $sxml->cc->tmp . "";
		$f["WindChillF"] = $sxml->cc->flik . "";
		$f["WeatherDescription"] = $sxml->cc->t . "";
		$f["Pressure"] = $sxml->cc->bar->r . "";
		$f["VisibilityM"] = $sxml->cc->vis . "";
		$f["today_icon"] = $sxml->cc->icon . "";
		$f["today_hi"] = $sxml->dayf->day->hi . "";
		$f["today_lo"] = $sxml->dayf->day->low . "";
		$f["today_dow"] = date('l');
		$f["today_wDir"] = $sxml->dayf->day->part->wind->t . "";
		$f["today_wSpd"] = $sxml->dayf->day->part->wind->s . "";
		$f["today_pOP"] = $sxml->dayf->day->part->ppcp;
		$f["today_h"] = $sxml->dayf->day->part->hmid;
		
		unset($sxml);
		
		foreach($f as $k=>$v) {
			$json[] = '"' . $k . '":"' . $v . '"';
		}
		
		return ($arr ? $f : '({' . implode(',',$json) . '})');
	}
	
	//returns current weather in JSON
	public function parseForecast() {
		return $this->parse_feed();
	}
	

	// *** PRIVATE ****
	
	private function fetch_feed(){
		if(!$this->feed_contents) 
			$this->feed_contents = implode("", file($this->feed_url));
    }
	
	private function parse_feed() {		
		
		// This is FAIL'ey		
		$cw = $this->parseCurrentWeather(true);

		$json['"day1hiTmpF"'] = '"day1hiTmpF":"' . $cw['today_hi'] . '"';
		$json['"day1loTmpF"'] = '"day1loTmpF":"' . $cw['today_lo'] . '"';
		$json['"day1sky24"'] = '"day1sky24":"' . $cw['today_icon'] . '"';
		$json['"day1dow"'] = '"day1dow":"' . $cw['today_dow'] . '"';
		$json['"day1wDir"'] = '"day1wDir":"' . $cw['today_wDir'] . '"';
		$json['"day1wSpdM"'] = '"day1wSpd":"' . $cw['today_wSpd'] . '"';
		$json['"day1pOP"'] = '"day1pOP":"' . $cw['today_pOP'] . '"';
		$json['"day1rH"'] = '"day1rH":"' . $cw['today_h'] . '"';
		// end FAIL
		
		$this->fetch_feed();
		
		$p = xml_parser_create('UTF-8');
		xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
		xml_parse_into_struct($p, $this->feed_contents, $this->feed_struct, $this->struct_tags);
		xml_parser_free($p);
		
		foreach($this->struct_tags as $tag => $val) {
			if($tag == 'day') {
				$r = $val;
				$count = 2;
				for($i=0; $i < count($r); $i+=2) {
					$off = $r[$i] + 1; 
					$len = $r[$i + 1] - $off;
					foreach(array_slice($this->feed_struct, $off, $len) as $k => $v) {
						
						# <stupid-dumb-bullshit>
						$skip_these = array('suns','sunr','part','gust','bt','n','wind');
						
						$t = $v['tag'];
						
						if(in_array($t, $skip_these)) continue;
						
						if($t == 'icon') 				$t = 'sky24';
						if($t == 'hi')					$t = 'hiTmpF';
						if($t == 'low')					$t = 'loTmpF';
						if($t == 's')					$t = 'wSpdM';
						if($t == 't')					$t = 'wDir';
						if($t == 'ppcp')				$t = 'pOP';
						if($t == 'hmid')				$t = 'rH';
						
						if($count > 1 && in_array($t, array('wSpdM','wDir','pOP','rH'))) continue;
						# </lame-garbage>

						$json['"day' . $count . $t . '"'] = '"day' . $count . $t . '":"' . $v['value'] . '"';
					}
					
					//IF THERE ARE DAY-RELATED PROBLEMS: Yea, this doesn't come from the feed, this is PHP date(). I hope all goes well! -pk
					if($count > 1) $json[] = '"day' . $count . 'dow' . '":"' . date('l', strtotime('+' . ($count-1) . 'day')) . '"';
					$count++;
				}
			} else { continue; }
		}
		return '({' . implode(',',$json) . '})';
	}
}