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

class ntsl implements Product, Map, RegionType, Movie {
	private $path = 'STATE_daily';
	private $regiontype = 'SR';
	private $region_array = array(
		'SR' => 'STATE_daily',
		'AR' => 'STATE_daily'
	);
	private $subproduct = 'NTSLA';  // 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 'NTSL';
	}
	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;
	}
}
?>
