#!/usr/bin/perl
use GD::Simple;
use Math::Round;
require "../cgi-lib.pl";
ReadParse(*params);
$debug_line = 1;
# width - image width
# height - image height
# bgcolor - background color

# sXcolor - set X color
# sXval - numeric value

my($width,$height) = ($params{width},$params{height});
$width ||= 100;
$height ||= 100;
$borderflag = 0;

%valid_borders = (
    200 => { border => 200, modwidth => 0, img => 'ring.png', },
    500 => { border => 499, modwidth => -1, img => '500circle.png', },
    131 => { border => 131, modwidth => 0, img => '132circle.png', },
    132 => { border => 131, modwidth => -1, img => '132circle.png', },
    276 => { border => 276, modwidth => -1, img => '276circle.png', },
    396 => { border => 396, modwidth => -1, img => '396circle.png', },
    );

#print STDERR "Starting!\n";
if($params{border} && $width == $height && $valid_borders{$width}) {
    $borderflag = $valid_borders{$width}{border};
    $ring = "/home/dave/public_html/cgi/$valid_borders{$width}{img}";
    $height = $height + $valid_borders{$width}{modwidth};
    $img = GD::Simple->new($height+1,$height+1,1);
    $width = $width + $valid_borders{$width}{modwidth};
} elsif($width == 200 && $height == 200 && $params{border}) {
    $borderflag = 200;
    $img = GD::Simple->new($width+1,$height+1,1);
} elsif($width == 500 && $height == 500 && $params{border}) {
    $borderflag = 499;
    $width = 499;
    $height = 499;
    $img = GD::Simple->new($width+1,$height+1,1);
} elsif($width == 131 && $height == 131 && $params{border}) {
    $borderflag = 131;
    $img = GD::Simple->new($width+1,$height+1,1);
} else {
    $img = GD::Simple->new($width,$height,1);
}

$radius = ($width > $height) ? $height / 2 : $width / 2;

#print STDERR "Filling\n";
$img->fill(10,5,parse_color($params{bgcolor}));

#print STDERR "Parsing\n";
$data = parse_sets();

#$angle = 0;
#print STDERR "Move to!\n";
$done = 0;
$dithered = 0;
while(!$done) {
    $done = 1;
    $img->moveTo($radius,$radius);
    $angle = 0;
    $si = 0;
    foreach my $set (@{$data}) {
	#print STDERR "Drawing set\n";
	if($params{antialias}) {
	    my($c) = parse_color($set->{color});
	    $img->setAntiAliased($c);
	    $img->bgcolor(gdAntiAliased);
	    $img->fgcolor(gdAntiAliased);
	} else {
	    $img->bgcolor(parse_color($set->{color}));
	    $img->fgcolor(parse_color($set->{color}));	
	}
	my($inc) = 360*$set->{perc};
	if(!$set->{dither} || !$dithered) {
	    if(int($angle) == int($angle+$inc)) {
		#drawing an arc from "64.1 degrees to 64.4 degrees" causes a
		#full circle to be drawn.
		#debug("Trunc $angle,$inc");
	    } else {
		$img->arc($radius*2,$radius*2,$angle,$angle+$inc,gdEdged);
	    }
	}
	$angle+=$inc;
	if($set->{dither} && !$dithered) { $done = 0; }
	$si++;
    }
    if(!$done) {
	#dither!
	for(my $x = 0;$x<$width;$x++) {
	    for(my $y = 0;$y<$height;$y++) {
		if(($y % 2) + ($x % 2) == 1) {
		    if(sqrt(($radius-$x)*($radius-$x)+($radius-$y)*($radius-$y)) <= $radius) {
			#$img->setPixel($x,$y,color(255,255,255));
			$img->setPixel($x,$y,color(0,0,0));
		    }
		}
	    }
	}
	$dithered = 1;
    }
}
#debug($si);

#$img->bgcolor(color(255,0,255));

#$img->arc(30,30,0,330,gdEdged);

#$img->arc(50,50,30,30,0,360,color(255,0,255));

if($borderflag) {
    if($ring) {
	$ringimg = GD::Image->newFromPng($ring,1);
	$img->copy($ringimg,0,0,0,0,$width+1,$height+1);
    } elsif($borderflag == 200) {
	$ringimg = GD::Image->newFromPng("/home/dave/public_html/cgi/ring.png",1);
	$img->copy($ringimg,0,0,0,0,201,201);
    } elsif($borderflag == 499) {
	$ringimg = GD::Image->newFromPng("/home/dave/public_html/cgi/500circle.png",1);
	$img->copy($ringimg,0,0,0,0,500,500);
    } elsif($borderflag == 131) {
	$ringimg = GD::Image->newFromPng("/home/dave/public_html/cgi/132circle.png",1);
	$img->copy($ringimg,0,0,0,0,132,132);
    }
}

print "Content-Type: image/png\n\n";
binmode STDOUT;

#my $o = rand();
my $o = 0; #stop for now
my $rotimg;
if($o < 0.25) {
    $rotimg = $img; #nothing
} elsif($o < 0.5) {
    $rotimg = $img->copyRotate90();
} elsif($o < 0.75) {
    $rotimg = $img->copyRotate180();
} else {
    $rotimg = $img->copyRotate270();
}

print $rotimg->png;

#if($params{saveme} && $params{name}) {
#    $filename = "/home/dave/public_html/cgi/dyn/$params{name}_";
#    for(my $i=1;$i<10;$i++) {
#	next if(-e "$filename$i.png");
#	if(open($PNG,">","$filename$i.png")) {
#	    print $PNG $rotimg->png;
#	    close $PNG;
#	}
#	$i=10;
#    }
#}

sub parse_sets {
    my($total) = 0;
    my @data = ();
    my($flag) = 0;
    foreach my $k (keys %params) {
	if($k =~ /^s(\d+)color$/) {
	    $data[$1]{color} = $params{$k};
	} elsif($k =~ /^s(\d+)val$/) {
	    $data[$1]{val} = $params{$k};
	    $total+= $params{$k};
	    if($params{$k} < 0) {
		$flag = 1; #ACK!
	    }
	} elsif($k =~ /^s(\d+)dither$/) {
	    $data[$1]{dither} = 1;
	}
    }
    if(!@data || $total <= 0 || $flag) {
	@data = (
	    {
		'color' => "#0000FF",
		'val' => 20,
	    },
	    {
		'color' => "#00FF00",
		'val' => 35,
	    },
	    {
		'color' => "#FF0000",
		'val' => 45,
	    },
	    );
	$total = 100;
    }
    foreach my $set (@data) {
	$set->{perc} = $set->{val} / $total;
	$set->{color} ||= "#FFFFFF";
    }
    return \@data;
}

sub parse_color {
    my($str) = @_;
    #print STDERR "Trying to resolve $str\n";
    if($str =~ /([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})/) {
	#print STDERR "Got it!\n";
	return color(hex($1),hex($2),hex($3));
    } else {
	#print STDERR "Hm, going with white\n";
	return color(255,255,255);
    }
}

sub color {
    my($r,$g,$b) = @_;
    #print STDERR "Calling for $r,$g,$b\n";
    if(!defined($pal{"$r\_$g\_$b"})) {
	#print STDERR "Not defined, defining\n";
        $pal{"$r\_$g\_$b"} = $img->colorAllocate($r,$g,$b);
	#$img->setAntiAliased($pal{"$r\_$g\_$b"});
    }
    return $pal{"$r\_$g\_$b"};
}

sub debug {
    $img->string(gdTinyFont,0,$debug_line*10,$_[0],color(0,0,0));
    $debug_line++;
}
