<?php
include_once('interface.php');
include_once('madeleine_imap.php');

class gsla implements Product, Map, RegionType, Movie {
	private $path = 'STATE_daily';
	private $regiontype = 'SR';
	private $region_array = array(
		'SR' => 'STATE_daily',
		'AR' => 'STATE_daily'
	);
	private $subproduct = 'SLA';  // Bit of a hack

	public function getRegionType() {
		return $this->regiontype;
	}
	public function setRegionType($r) {
		if (array_key_exists($r, $this->region_array)) $this->path = $this->region_array[$r];
		else $this->path = 'null';
		$this->regiontype = $r;
	}
	public function getLongName() {
		return 'GSLA';
	}
	public function isRegionExists($region) {
		//$k = array_keys($this->data_array);
		//$k = $k[0];
		$k = $this->subproduct;
		$d = $this->path.'/'.$k.'/'.$region;
		return is_dir($d);
	}
	public function isDateValid($region, $date) {
		$dt = $this->getNearestDate($region, $date);
		if (is_null($dt)) return false;
		$cmp = DateIntervalCmp($dt->diff($date,true), new DateInterval('P10D'));
		return $cmp <= 0;
	}
	public function getDateString($date) {
		return $date->format('d M Y');
	}
	public function getNextDate($region, $dt) {
		$now = new DateTime('now', new DateTimeZone('UTC'));
		$last = clone $dt;
		$last->add(new DateInterval('P20D'));
		if ($now < $last) $last = $now;
		$date = clone $dt;
		while ($date <= $last) {
			$fmask = $date->format('Ymd');
			$files = glob($this->path.'/'.$this->subproduct.'/'.$region.'/'.$fmask.'*.gif');
			foreach ($files as $f) {
				$d = $this->file2date(basename($f));
				if ($d > $dt) return $d;
			}
			$date->add(new DateInterval('P1D'));
		}
		return NULL;
	}
	public function getPreviousDate($region, $dt) {
		// If we go back a bit and find nothing then there probably isn't
		// any older data
		$last = clone $dt;
		$last->sub(new DateInterval('P20D'));
		$date = clone $dt;
		while ($date >= $last) {
			$fmask = $date->format('Ymd');
			$files = glob($this->path.'/'.$this->subproduct.'/'.$region.'/'.$fmask.'*.gif');
			foreach ($files as $f) {
				$d = $this->file2date(basename($f));
				if ($d < $dt) return $d;
			}
			$date->sub(new DateInterval('P1D'));
		}
		return NULL;
	}
	public function getNearestDate($region, $date) {
		$f = $this->getFilename($region, $date);
		if (!is_null($f)) return $this->file2date($f);
		$ndate = $this->getNextDate($region, $date);
		$pdate = $this->getPreviousDate($region, $date);
		if (is_null($ndate)) return $pdate;
		elseif (is_null($pdate)) return $ndate;
		// else we find the closest
		$ndiff = $ndate->diff($date, true);
		$pdiff = $pdate->diff($date, true);
		if ($pdiff < $ndiff) return $pdate;
		else return $ndate;
	}
	private function file2date($filename) {
		$ymdh = substr(basename($filename),0,8);
		return DateTime::createFromFormat('Ymd', $ymdh);
	}
	private function date2file($date) {
		return $date->format('Ymd').'.gif';
	}
	public function getFilename($region, $date) {
		$f = $this->path.'/'.$this->subproduct.'/'.$region.'/'.$this->date2file($date);
		if (file_exists($f)) return $f;
		return NULL;
	}
    public function getMapList($region, $date) {
        $txtfile = $this->path.'/TAGS/'.$region.'/'.$date->format('Ymd').'.txt';
        return get_map_from_textfile($txtfile, $date);
    }
    public function isMovieValid($name, $date) {
		return file_exists($this->getMovieFilename($name, $date));
	}
    public function getMovieFilename($name, $date) {
		// Split name into region and subproduct
		$a = explode('.', $name);
		$subproduct = $a[0];
		$region = $a[1];
		$year = $date->format('Y');
		$m = intval($date->format('m'));
		$q = 1;
		if ($m>=4) $q = 2;
		if ($m>=7) $q = 3;
		if ($m>=10) $q = 4;
		$f = $region.'_'.$subproduct.'_'.$year.'_Q'.$q.'.mp4';
		$path = $this->path.'/'.$subproduct.'/'.$region.'/'.$f;
		return $path;
	}
	/*
	public function getDataLinks($region, $date) {
		// region and data agnostic for the moment
		return array(
			'SST L3S-6d ngt<br>(1992-2017)' => 'http://thredds.aodn.org.au/thredds/catalog/IMOS/SRS/SST/ghrsst/L3S-6d/ngt/catalog.html',
			'SST L3SM-6d ngt<br>(2018-now)' => 'http://thredds.aodn.org.au/thredds/catalog/IMOS/SRS/SST/ghrsst/L3SM-6d/ngt/catalog.html',
			'GSLA' => 'http://thredds.aodn.org.au/thredds/catalog/IMOS/OceanCurrent/GSLA/catalog.html',
			'SSTAARS' => 'https://portal.aodn.org.au/search?uuid=79c8eea2-4e86-4553-8237-4728e27abe10'
		);
	}
	public function getInfo() {
		return file_get_contents('product_info/sst_percentiles_about.html');
	}
	public function getInfoTitle() {
		return 'SST and Percentiles';
	}
	public function getLegend() {
		return array(
			'<span style="color:#FF00FF;">&#9675;</span>' => 'Argo',
			'<span style="color:#FF00FF;">&#9670;</span>' => 'Glider',
			'<span style="color:#FF0000;">&rarr;</span>' => 'Radar',
			'<span style="color:#FF00FF;">&gt;</span>' => 'Drifter',
			'<span>&#9675;</span>' => 'Ship'
		);
	}
	public function getLegendText() {
		return file_get_contents('product_info/sst_percentiles_legend.html');
	}
	*/
}
?>
