#!/usr/bin/env perl
use strict;
use warnings;
use 5.014;
use WebService::ForecastIo::Response;
use Path::Class;
use Template::Provider::Encoding;
use Template::Stash::ForceUTF8;
use Template;

my %icon_for=(
    'clear-day' => '2',
    'clear-night' => '3',
    rain => '18',
    snow => '23',
    sleet => '24',
    wind => '6',
    fog => '13',
    cloudy => '14',
    'partly-cloudy-day' => '8',
    'partly-cloudy-night' => '9',
);

sub icon_for {
    my ($status) = @_;

    return "icons/".($icon_for{$status}//'45').".svg";
}

my $res = WebService::ForecastIo::Response->new(
    file($ARGV[0])->slurp(iomode=>'<:raw')
);
my $tf = file(__FILE__)->parent->file('forecast.html.tt');
my $t = Template->new(
    LOAD_TEMPLATES => [ Template::Provider::Encoding->new(
        ABSOLUTE => 1,
        RELATIVE => 1,
    ) ],
    STASH => Template::Stash::ForceUTF8->new,
);
binmode STDOUT,':utf8';
$t->process($tf->stringify,{
    f => $res,
    icon_for => \&icon_for,
})
    or die $t->error;

__DATA__
[% MACRO dtspan BLOCK -%]
<[% x.start_time.strftime('%H:%M%z') %] - [%x.stop_time.strftime('%H:%M%z') %]> [% x.value %]
[%- END -%]
Currently: [% f.currently.summary %] [% f.currently.temperature %]°
[% f.hourly.summary %]

[% FOREACH x IN f.hourly.spans_by_string('summary'); dtspan(x=x); ' '; END %]
[% FOREACH x IN f.hourly.spans_by_number('temperature',2); dtspan(x=x); ' '; END %]

