<?php
require_once(dirname(__FILE__).'/../constants.inc.php');
require_once(dirname(__FILE__).'/utils.class.php');
require_once(dirname(__FILE__).'/GeoPoint.class.php');
require_once(dirname(__FILE__).'/Tile.class.php');

//
class PanoramaFormatException extends Exception {
	/** If the files organization is not correct for a panorama, we can't let it go...
	 */
}

class site_point extends GeoPoint {
  /** Defines a point, with a panorama
  */
  private $base_dir;        // dir of tiles for that panorama
  private $name = false;
  private $prefix = false;
  private $params = false;
  private $zooms;

  public static $REF_KEY = 'reference';

  public function __construct($dir) {
    $this->base_dir = $dir;
    $this->prefix = basename($dir);
  }

  public function params_path() {
	  return $this->base_dir.'/site.params';
  }

  /** Look for a *.params file in the base_dir
   *
   *  Tries first the site.params, then a globbing *.params.
   *
   *  May be deprecated at a certain time (if we consider all files should be
   *  named site.params)
   *
   *  @returns false if not found, an abs path else.
   */
  private function look_for_params() {
	if (is_file($this->params_path())) {
	  return $this->params_path();
	}
	$matches = glob($this->base_dir.'/*.params');
	if ($matches and count($matches) > 0) {
	  return $matches[0];
	} else {
	  return false;
	}
  }

  public function tiles_url_prefix() {
	  return PANORAMA_FOLDER;
  }

  public function tiles_path() {
	  return $this->base_dir;
  }

  private function parse_and_cache_params() {
	$params_path = $this->look_for_params();

    if ($params_path) {
	    $params = parse_ini_file($params_path);
	    if ($params) {
		    $this->params = $params;
		    if (isset($params[self::$REF_KEY])) {
			    foreach ($params[self::$REF_KEY] as $ref => $vals) {
				    $bits = explode(',',$vals);
				    $this->params[self::$REF_KEY][$ref] = array(floatval($bits[0]),
				                                                floatval($bits[1]));
			    }
		    }
		    if (isset($params['image_loop'])) {
			    $this->params['image_loop'] = (bool)($params['image_loop']);
		    }
		    return $this->params;
	    }
    }
    return array();
  }

  public function get_params() {
	  // the params are cached
	  if (isset($this->params) && $this->params) {
		  return $this->params;
	  } else {
		  return $this->parse_and_cache_params();
	  }
  }

  public function save_params() {
	  $o = '';
	  $p = $this->get_params();
	  foreach ($this->get_params() as $k => $v) {
		  if ($k == self::$REF_KEY) {
			  foreach ($v as $refk => $refv) {
				  $o.= sprintf("%s[\"%s\"] = %.5f,%.5f\n",
				               self::$REF_KEY, $refk,
				               $refv[0], $refv[1]);
			  }
		  } else {
			  $o.= "$k = ".utils::php2ini($v)."\n";
		  }
	  }
	  file_put_contents($this->params_path(), $o);
  }

  public function set_param($key, $value) {
	  $p = $this->get_params();
	  $this->params[$key] = $value;
	  if ($key == 'titre') {
		  $this->name = $value;
	  }
  }

  public function has_params(){
	  $p = $this->get_params();
	  return (isset($p['latitude'], $p['longitude'],
	                $p['altitude'], $p['titre']));
  }

  public function has_tiles(){
	  if (file_exists($this->tiles_path())) {
         $pano_files = scandir($this->tiles_path());
        foreach($pano_files as $filename) {
          if (preg_match('/.*\.jpg/', $filename)) {
	          return true;
          }
        }
      } else {
	      return false;
      }
  }

  public function get_name() {
    return basename($this->base_dir);
  }

  public function get_prefix() {
    return $this->prefix;
  }

  public function get_lat() {
	$p = $this->get_params();
	return $p['latitude'];
  }

  public function get_lon() {
	$p = $this->get_params();
	return $p['longitude'];
  }

  public function get_magnifications() {
    //$dir_fd = opendir($this->base_dir);
    $zoom_array = array();

	// extraction des paramètres de grossissement par le serveur
	$stop = false;
	$zoom_level = 0;

	while (! $stop) {
	  $files = glob(sprintf('%s/%03d_*.jpg', $this->base_dir, $zoom_level));
	  sort($files);
	  $last_file = end($files);
	  if ($last_file) {
		$last_tile = Tile::from_file($last_file, $this);
		$zoom_array[$zoom_level] = array('nx' => $last_tile->x + 1,
										 'ny' => $last_tile->y + 1);
		$zoom_level++;
	  } else {
		$stop = true;
	  }
	}
    $this->zooms = $zoom_array;
    return $this->zooms;
  }

  public function coordsToCap($lat, $lon, $alt) {
    $params = $this->get_params();
    if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
    $rt = 6371;  // Rayon de la terre
    $alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
    $lat1 = $params['latitude']*M_PI/180;
    $lon1 = $params['longitude']*M_PI/180;
    $alt2 = $alt;
    $lat2 = $lat * M_PI/180;
    $lon2 = $lon * M_PI/180;

    $dLat = $lat2-$lat1;
    $dLon = $lon2-$lon1;

    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
    $d = $angle * $rt;                    // distance du point en Kms

    $y = sin($dLon)*cos($lat2);
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians

    $e = atan2(($alt2 - $alt1)/1000 - $d*$d/(2*$rt), $d);  // angle de l'élévation en radians
    //    printf("%s, %s, %s, %s\n",$lat1, $params['latitude'], $lat, $dLat);
    return array($d, $cap*180/M_PI, $e*180/M_PI);   // les résultats sont en degrés
  }

  public function get_generate_url($source_file) {
	  /**
	   * @param $source_file : the name of the source file within the upload dir.
	   */
	  return sprintf('genererPano.php?wizard=1&name='.$source_file);
  }

  public function src_path(){
	  /** @returns the basename of the src image, or false if it's no longer available
	   */
	  $extensions = array('jpg', 'tif', 'png', 'bmp', 'jpeg', 'pnm',
	                      'JPG', 'TIF', 'PNG', 'BMP', 'JPEG', 'PNM');

	  foreach ($extensions as $ext) {
		  $tried_name = sprintf('%s/%s.%s', UPLOAD_PATH, $this->get_name(),$ext);
		  if (file_exists($tried_name)) {
			  return $tried_name;
		  }
	  }
	  return false;
  }

  public function get_url($cap=false, $ele=false) {
	  $o = sprintf('panorama.php?dir=%s&panorama=%s',
	                  PANORAMA_FOLDER, $this->get_name());
	  if ($cap && $ele) {
		  $o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
	  }
	  return $o;
  }

  public function get_map_url($cap=0) {
	  $encoded_title = base64_encode($this->get_name());
	  $script_name = 'show_capline.php';
	  $lat = $this->get_params()['latitude'];
	  $lon = $this->get_params()['longitude'];

	  $o = sprintf('%s?title=%s&cap=%s&org_lat=%.5f&org_lon=%.5f&dist=120000',
	               $script_name, $encoded_title, $cap, $lat, $lon);
	  return $o;
  }

  public function set_reference($ref_point, $x, $y) {
	  /**
	   * Registers (for saving) the position of a reference point within a
	   * panorama. It sets or overwrite a reference.
	   *
	   * @param $ref_point a RefPoint instance
	   * @param $x the relative x position of the RefPoint
	   * @param $x the relative y position of the RefPoint
	   */
	  $p = $this->get_params();

	  if (!isset($this->params[self::$REF_KEY]) ||
	      !is_array($this->params[self::$REF_KEY])) {
		  $this->params[self::$REF_KEY] = array();
	  }
	  $ref_name = $ref_point->name;
	  $dict = $this->params[self::$REF_KEY];
	  //$dddd = $this->params[self::$REF_KEY][$ref_name];
	  $this->params[self::$REF_KEY][$ref_name] = array($x, $y);
  }

  public function unset_reference($ref_point) {
	  /**
	   * Unregisters a reference, within a panorama.
	   * does nothing if the RefPoint is not registered.
	   *
	   * @param $ref_point a RefPoint instance
	   */
	  $p = $this->get_params();
	  $ref_name = $ref_point->name;
	  if (isset($p[self::$REF_KEY]) &&
	      isset($p[self::$REF_KEY][$ref_name])) {
		  unset($this->params[self::$REF_KEY][$ref_name]);
	  }
  }



  public static function get($name) {
	  /** Instantiate a site_point, given its name
	   */
	  $pano_dir = PANORAMA_PATH.'/'.$name;
	  return new site_point($pano_dir);
  }

  public static function create($filepath) {
	  /** creates a new panorama, given its name, from an uploaded file.
	   */
	  $name = utils::strip_extension(basename($filepath));
	  $pano_dir = PANORAMA_PATH.'/'.$name;
	  $pano = new site_point($pano_dir);
	  if (!mkdir($pano->tiles_path())) {
		  return false;
	  } else {
		  return $pano;
	  }
  }

  public function to_geoJSON() {
	  $prm = $this->get_params();
		$name = $this->get_name();
		$lat = floatval($prm['latitude']);
		$lon = floatval($prm['longitude']);
		//$alt = $prm['altitude'];
		//$title = $prm['titre'];

		return array("type" => "Feature",
		             "geometry" => array(
		                                 "type" => "Point",
		                                 "coordinates" => array($lon, $lat)
		                                 ),
		             "properties" => array("name" => $name,
		                                   "type" => 'pano_point',
		                                   "view_url"  => $this->get_url())
		             );
  }


  public static function get_all($only_with_params=true) {
	  /**
	   * @param $only_with_params : filters out the panoramas which
	   *        are not parametrized
	   */
	  $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
	  $pano_instances = array();

	  foreach ($panos as $pano_name) {
		  $pano =  site_point::get($pano_name);
		  if (! $only_with_params || $pano->has_params() ) {
			  $pano_instances[] = $pano;
		  }
	  }
	  return $pano_instances;
  }

}
