package Genex::ClickMaps;

use strict;

use ShakeConfig;

use Shake::Die;


use IO::File;
use Genex::Constants qw( :peakimagemap );

use vars qw(@ISA @EXPORT);

use Exporter ();
@ISA = qw( Exporter );
@EXPORT = qw( &pgapgv_imagemap &intensityscale_imagemap &imagemap_prep );


sub imagemap_prep {
  my $par  = shift;
  my($fh,@lines,$line,%stas,$sta,$x,$y,@evt,$status,$array,
     $height_ps,$height);

  my $geom = $par->{'geom'};

  $fh = new IO::File;
  $fh->open($STATION_LOC_FILE,'r') || Die;
  @lines = <$fh>;
  $fh->close;
  @lines = grep($_ !~ /^\#/,@lines);   # get rid of comment lines
  @lines = grep($_ !~ /^\s*$/,@lines); # get rid of blank lines
  chomp(@lines);

  # get some geometry info
  $height_ps = $geom->{'h'};
#  $height = $height_ps + $JPG_MARGIN{bottom} + $JPG_MARGIN{top};
  $height = $height_ps + $geom->{'yo'} + $JPG_MARGIN{top};
  
  foreach $line (@lines) {
    ($sta,$x,$y) = split(' ',$line);
    
    # add gif/jpg margins
#    $x += $JPG_MARGIN{'left'};
#    $y += $JPG_MARGIN{'bottom'};
    $x += $geom->{'xo'};
    $y += $geom->{'yo'};

    # flip $y (0 at top in gif/jpg)
    $y = $height - $y;

    # convert from inches to 72 dpi
    $x = int($x * 72 + 0.5);
    $y = int($y * 72 + 0.5);

    $stas{$sta} = [$x,$y];
  }


  $fh->open($EVENT_LOC_FILE,'r') || Die;
  @lines = <$fh>;
  $fh->close;
  @lines = grep($_ !~ /^\#/,@lines);   # get rid of comment lines
  @lines = grep($_ !~ /^\s*$/,@lines); # get rid of blank lines
  chomp(@lines);

  $line = shift(@lines);
  ($x,$y) = split(' ',$line);

  # add gif/jpg margins
#  $x += $JPG_MARGIN{'left'};
#  $y += $JPG_MARGIN{'bottom'};
    $x += $geom->{'xo'};
    $y += $geom->{'yo'};
  
  # flip $y (0 at top in gif/jpg)
  $y = $height - $y;
  
  # convert from inches to 72 dpi
  $x = int($x * 72 + 0.5);
  $y = int($y * 72 + 0.5);
  @evt = ($x,$y);


  &add_status_string($par->{'station-data'},\%stas,\@evt);

  return(\@evt,\%stas);
}

sub pgapgv_imagemap {
  my $tmpl = shift;
  my $par  = shift;
  my(@stations,$stas,$sta,$x,$y,$evt,$status);

  if (not (exists $par->{'clickmap'}{'stas'} and 
	   exists $par->{'clickmap'}{'evt'})) 
    {
      Die "in \&pgapgv_imagemap without having run \&imagemap_prep!";
    }

  $stas = $par->{'clickmap'}{'stas'};
  $evt  = $par->{'clickmap'}{'evt'};

  @stations = ();
  foreach $sta (keys %$stas) {
    ($x,$y,$status) = @{ $stas->{$sta} };

    my %station = ('sta'    => $sta,
		   'x'      => $x,
		   'y'      => $y,
		   'status' => $status);

    push(@stations,\%station);
  }
  $tmpl->param('StationListMapping' => \@stations);

  ($x,$y,$status) = @$evt;
  $tmpl->param('eq-x'      => $x,
	       'eq-y'      => $y,
	       'eq-status' => $status);
}

sub intensityscale_imagemap {
  my $tmpl = shift;

  my(@html,$l,$t,$r,$b);

  # coordinates of intensity scale bar in pixels from top left
  $tmpl->param('int-imap-l' => 45,
	       'int-imap-t' => 572,
	       'int-imap-r' => 548,
	       'int-imap-b' => 593);
}

sub add_status_string {
  my $data = shift;
  my $stas = shift;
  my $evt  = shift;
  
  my($status,$lat,$lon,$M,$ns,$ew,
     $code,$slat,$slon,$pga,$pgv,
     $sta_hash,$sta_ref);

  # Create event status string
  $lat = $data->source->lat();
  $lon = $data->source->lon();
  $M   = $data->source->mag();
  
  $ns = $lat > 0 ? "N" : "S";
  $ew = $lon > 0 ? "E" : "W";
  
  $status = sprintf("Epicenter: %s%.2f %s%.2f M=%.1f",
		    $ns,abs($lat),$ew,abs($lon),$M);
  
  $evt->[2] = $status;
  

  # Create station status strings
  $sta_hash = $data->station_hash();
  foreach $code (keys %$stas) {
    $sta_ref = $sta_hash->{$code};
    if (!defined($sta_ref)) {
      warn "Genex::ClickMap - unable to access info for sta $code\n";
      next;
    }
    $slat = $sta_ref->lat();
    $slon = $sta_ref->lon();
    $pga  = $sta_ref->peak('pga');
    $pgv  = $sta_ref->peak('pgv');
    
    $ns = $slat > 0 ? "N" : "S";
    $ew = $slon > 0 ? "E" : "W";
 
    $pga = defined($pga) ? sprintf("PGA: %.2f",$pga) : "PGA: unknown";
    $pgv = defined($pgv) ? sprintf("PGV: %.2f",$pgv) : "PGV: unknown";

    $status = sprintf("(%s) %s %s " .
		      "%s%.2f %s%.2f",
		      $code,$pga,$pgv,
		      $ns,abs($slat),
		      $ew,abs($slon));
    
    push(@{ $stas->{$code} },$status);
  }
}

1;
