#!/usr/bin/perl 
#use strict;

use GD;

my $cgi = new CGI;
my $vars = $cgi->Vars;
print $cgi->header('text/html');

my $rando_str_hex = '';
my @things = ('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
for (my $x = 0; $x < 3; $x++) { $rando_str_hex .= $things[int(rand(16))]; }
my $rando_str = time . "_$rando_str_hex";

if ($vars->{input})
  {
  open WHITS, ">hits/hits_$rando_str.txt";
  print WHITS $vars->{input}, "\n";
  close WHITS;

use DBI;
use CGI;

require "mapplot/mapplot.pl";

my $dbh = DBI->connect('dbi:mysql:zipcode', 'root', 'bvlkuyre');

open(PNG,"usmap-blank.png") || die "Couldn't open usmap.png : $!";

print STDERR "Reading in map image (this may take a few seconds)...\n";
binmode(PNG);
my $img = newFromPng GD::Image(PNG) || die;
close(PNG);


# allocate some colors
my $red = $img->colorAllocate(255,0,0);
my $blue = $img->colorAllocate(0,0,255);


#Get a list of bidders in the USA
open HITS, "hits/hits_$rando_str.txt";

while (my $line = <HITS>)
  {
  chomp $line;
  next if ($line =~ /^\#/);
  next unless $line =~ /(\d{5})\D*(\d*)/;
  #print "$1 $2\n";

  my $count = $2;
  if ($vars->{no_rings}) { $count = 1; }

  my $sql = "select lat as latitude, lon as longitude from zips where zip = '$1'";
  my $query = $dbh->prepare($sql);
  $query->execute;
  my ($lat, $lon) = $query->fetchrow_array; 
  next if (!$lat) or (!$lon);



#while ( my ($lat, $lon, $count) = $query->fetchrow_array ) {

	my ($x,$y) = &GetXY($lat,$lon);

#print STDERR "Here's our Cord ($x, $y)<br>\n";
	my $width = 5 + $count;

	$img->arc($x,$y,$width-6,$width-6,0,360,$red);
	$img->arc($x,$y,$width-5,$width-5,0,360,$red);
	$img->arc($x,$y,$width-4,$width-4,0,360,$red);
	$img->arc($x,$y,$width-3,$width-3,0,360,$red);
	$img->arc($x,$y,$width-2,$width-2,0,360,$red);
	$img->arc($x,$y,$width-1,$width-1,0,360,$red);
	$img->arc($x,$y,$width,$width,0,360,$red);
	$img->arc($x,$y,$width+1,$width+1,0,360,$red);
	$img->arc($x,$y,$width+2,$width+2,0,360,$red);
	$img->arc($x,$y,$width+3,$width+3,0,360,$red);
	$img->arc($x,$y,$width+4,$width+4,0,360,$red);
	$img->arc($x,$y,$width+5,$width+5,0,360,$red);
	$img->arc($x,$y,$width+6,$width+6,0,360,$red);

}

#write credit here
my $black = $img->colorAllocate(255,0,0);
my $basey = 2000;

$img->string(gdGiantFont, 100, $basey, "http://www.wheresgeorge.com Map Maker by frank.", $black);
$img->string(gdGiantFont, 100, ($basey + 13), "http://blurbco.com/wg/map_hits.pl", $black);


#print "Writing output file...\n";
#print STDERR "Writing output file...\n";
my $output_filename = "maps/hitmap_$rando_str.png";
open(OUT,"> $output_filename") || die "Couldn't open example.png for writing : $!";
binmode(OUT);
print OUT $img->png;
close(OUT);

print "<img src='$output_filename'>\n";

  }
elsif ($vars->{show})
  {
  print "<font size=2 face=arial color='000000'>Here are the maps that have been made in the past, most recent ones first. The three numbers indicate zip count, hit count, and line count from the input. These images may be deleted from this webpage at any time.</font><p>\n";
  opendir DIR, "/var/www/blurbco.com/htdocs/wg/maps/";
  my @files = readdir DIR;
  closedir DIR;
  foreach my $file (sort {$b <=> $a} (@files))
    {
    next unless $file =~ /(hitmap_.*.png)/i;
    $file =~ /hitmap_(.*)\.png/i;
    my $code = $1;
    my $txt = 'hits/hits_' . $code . '.txt';
    open HITS, $txt;
    my $hit_count = 0;
    my $zip_count = 0;
    my $line_count = 0;
    while (my $line = <HITS>)
      {
      $line_count++;
      next unless $line =~ /(\d{5})\D*(\d*)/;
      $zip_count++;
      $hit_count += $2;
      }
    close HITS;
    print "<a href='maps/$file'><font size=3 face=arial>$file</font></a> &nbsp; <font size=3 face=arial>$zip_count / $hit_count / $line_count</font><br>\n";
    }
  }
else
  {
  print "<font size=3 face=arial>map maker by frank</font><br><font size=2 color='ff0000'>instructions below.</font><br>\n";
  print "<a href='map_hits.pl?show=1'><font size=2 face=arial>past maps here</font></a><br>\n";
  print "<form action='map_hits.pl' method='post'>\n";
  print "<input type=checkbox name='no_rings'>No Rings Style<br>";
  print "<textarea name='input' rows=50 cols=100>";
  print "</textarea>\n";
  print "<br>\n";
 
  print "<input type=submit value='submit here'>\n";
  print "</form>";
  print "1) Go to your bills.<br>\n";
  print "2) Go to Hits by State.<br>\n";
  print "3) Go to Hits by Zip Code for a state.<br>\n";
  print "4) Highlight as shown below.<br>\n";
  print "5) Hit CRTL-C and then go the the input box above and CRTL-V.<br>\n";
  print "6) Check to see that the input looks like this:<br><pre>\n";
  print "1. 79109 Amarillo Randall 41 
2. 79106 Amarillo Potter 7 
3. 79110 Amarillo Randall 6 
4. 79124 Amarillo Potter 4 
5. 79119 Amarillo Randall 3 
6. 79121 Amarillo Randall 3 
7. 79103 Amarillo Potter 3 
8. 79092 Vega Oldham 2 
9. 79101 Amarillo Potter 2 
10. 76014 Arlington Tarrant 1 
11. 75206 Dallas Dallas 1";
  print "</pre><br>\n";
  print "7) Repeat for as many states as you want.<br>\n";
  print "8) Hit Submit for your map.<br>\n";
  print "<img src='/~frank/how_to_highlight.gif'>\n";

  }
