#! /usr/bin/perl

###############################################################################
#
# License
# =======
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see
#   <http://www.gnu.org/licenses/>.
#
#   Copyright (C) 2012  Jörg Sommrey
###############################################################################

use strict;
use warnings;

use CGI qw{:standard};

my $from = param('from');
my $to = param('to');
my $days = param('days');
my @field = param('field');
my $smooth = param('smooth');
my $aggr = param('aggr');
my $aggrtype = param('aggrtype');

@field = qw(outTemp heatindex windchill) unless @field;

my $smootharg;
$smootharg = "smooth" if $smooth;
$aggr ||= "auto";
$aggrtype ||= "avg";

my @plotargs;
push @plotargs, "from=$from" if $from;
push @plotargs, "to=$to" if $to;
push @plotargs, "days=$days" if $days;
push @plotargs, "smooth=$smootharg" if $smootharg;
push @plotargs, "aggr=$aggr" if $aggr;
push @plotargs, "aggrtype=$aggrtype" if $aggrtype;
push @plotargs, map("field=$_", @field);
my @allfield = split /\s+/, qx{weatherplot -list};

print header,
	start_html(-title=>'Weather Plot',-style=>{'src'=>'/bear.css'},
		-encoding=>'UTF-8'),
	h2('Weather Plot'),
	start_form,
	table(Tr([td({-valign=>'TOP'}, [
		table(
			Tr(td(label({-for=>'from'}, 'From')),
				td(textfield(-name=>'from', -value=>$from, -size=>21,
					-maxlength=>19))),
			Tr(td(label({-for=>'to'}, 'To')),
				td(textfield(-name=>'to', -value=>$to, -size=>21,
					-maxlength=>19))),
			Tr(td(label({-for=>'days'}, 'Days')),
				td(textfield(-name=>'days', -value=>$days, -size=>4,
					-maxlength=>4))),
			Tr(td({-colspan => "2"},
				"Aggregation")),
			Tr(td(
				radio_group(-name=>'aggr',
				-values=>['no', 'auto', 'hour', 'day'],
				-default=>$aggr, -linebreak=>'true', -rows=>4,
				-columns=>1)
			)),
			Tr(td(
				radio_group(-name=>'aggrtype',
				-values=>['avg', 'min', 'max'],
				-default=>$aggrtype, -linebreak=>'true', -rows=>3,
				-columns=>1)
			)),
			Tr({-valign=>'TOP'},
				td({-colspan=>"2"},
					checkbox(-name =>'smooth', -label=>'Smooth',
					-checked => $smooth))
			),
			Tr(td(submit('Plot')), td(defaults('Clear')))
		),
		table(Tr(
			td(label({-for=>'field'}, 'Fields'))),
			Tr(td(scrolling_list(-name=>'field',
			-values=>\@allfield,
			-default=>\@field,
			-size=>scalar @allfield,
			-multiple=>'true')))),
		img({src=>"/weatherplot/weatherplot?" . join(';', @plotargs)})
	])])),
	end_form,
	end_html;

# vi:ts=4:
