#!/usr/bin/perl
use strict;
use warnings;
use lib 'eg';
use Games::Chess::Coverage;
use Games::Chess::Coverage::GD;
use Graphics::ColorNames 'hex2tuple';

my $v = shift || 0;

my %colors;
tie %colors, 'Graphics::ColorNames';

my %fen = (
    move   => '7r/8/RQR5/8/8/8/8/8',
    threat => '7r/8/RQR5/8/8/8/8/8',
    arrows => '7r/8/RQR5/8/8/8/8/8',
);

my %apply = (  # {{{
    board => sub {
        my $drawing = shift;
        $drawing->add_rule( 'Pieces' );
    },
    draw => sub {
        my $drawing = shift;
        $drawing->add_rule( 'Games::Chess::Coverage::GD::Move',
            white_move_color => [ hex2tuple( $colors{lightblue} ) ],
            black_move_color => [ hex2tuple( $colors{palegreen} ) ],
            both_move_color  => [ hex2tuple( $colors{pink} ) ],
        );
        $drawing->add_rule( 'Games::Chess::Coverage::GD::Threat',
            white_threat_color => [ hex2tuple( $colors{darkblue} ) ],
            black_threat_color => [ hex2tuple( $colors{darkgreen} ) ],
        );
    },
    move => sub {
        my $drawing = shift;
        $drawing->add_rule( 'Games::Chess::Coverage::GD::Move',
            white_move_color => [ hex2tuple( $colors{lightblue} ) ],
            black_move_color => [ hex2tuple( $colors{palegreen} ) ],
            both_move_color  => [ hex2tuple( $colors{pink} ) ],
        );
    },
    threat => sub {
        my $drawing = shift;
        $drawing->add_rule( 'Games::Chess::Coverage::GD::Threat',
            white_threat_color => [ hex2tuple( $colors{blue} ) ],
            black_threat_color => [ hex2tuple( $colors{red} ) ],
        );
    },
    arrows => sub {
        my $drawing = shift;
        $drawing->add_rule( 'Games::Chess::Coverage::GD::Arrows',
            white_arrow_color => [ hex2tuple( $colors{blue} ) ],
            black_arrow_color => [ hex2tuple( $colors{red} ) ],
        );  # Grrr!
    },
);  # }}}

for my $action ( keys %apply ) {
    $fen{$action} ||= 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR';

    my $cover = Games::Chess::Coverage->new(
#        verbose => 1,
        fen => $fen{$action},
    );
    if( $cover->{verbose} ) {
        warn $cover->whoami($_), "\n"
            for sort keys %{ $cover->pieces };
        warn "----\n";
    }

    my $drawing = Games::Chess::Coverage::GD->new(
        verbose => $v,
        coverage => $cover,
        out_file => "eg/$action",
        image_type => 'gif',
    );
    warn "Building $action.$drawing->{image_type} for $fen{$action}\n",
        $cover->game->to_text, "\n"
        if $drawing->{verbose};
#use Data::Dumper;warn Dumper(\$drawing);

    $drawing->add_rule( 'Games::Chess::Coverage::GD::Board',
        board_color  => [ hex2tuple( $colors{white} ) ], #[00,00,00]
        border       => 2,
        border_color => [ hex2tuple( $colors{black} ) ], #[255,255,255]
        letters      => 1,
        letter_color => [ hex2tuple( $colors{blue} ) ],  #[0,0,255]
        grid         => 1,
        grid_color   => [ hex2tuple( $colors{grey} ) ],  #[127,127,127]
    );

    $apply{$action}->( $drawing );
#use Data::Dumper;warn Dumper($drawing->{rules});

    $drawing->draw;
    $drawing->write;
}
