package XXV::MODULES::GRAB;
use strict;

use Tools;
use Event::File;
use Locale::gettext;
use GD;
use Template;
use File::Basename;
use File::Find;

# This module method must exist for XXV
# ------------------
sub module {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $args = {
        Name => 'GRAB',
        Description => gettext('This module grab a picture from livestream.'),
        Version => '0.01',
        Date => '06.09.2004',
        Author => 'xpix',
        Preferences => {
            xsize => {
                description => gettext('Image width'),
                default     => 320,
                type        => 'integer',
                required    => gettext('This is required!'),
            },
            ysize => {
                description => gettext('Image height'),
                default     => 240,
                type        => 'integer',
                required    => gettext('This is required!'),
            },
            file => {
                description => gettext('Location where the grabbed image file will be stored'),
                default     => '/tmp/live.jpg',
                type        => 'file',
            },
            imgtext => {
                description => gettext('Text to display in the grabbed picture.'),
                default     => "[?- i = channel.split(' ') -?][[? i.shift ?]] [? i.join(' ') ?]",
                type        => 'string',
            },
            vpos => {
                description => gettext('Vertical position of displayed text, in pixels.'),
                default     => 10,
                type        => 'integer',
            },
            font => {
                description => gettext('True type font to draw image text.'),
                default     => 'VeraIt.ttf',
                type        => 'list',
                choices     => $obj->findttf,
            },
            imgfontsize => {
                description => gettext('Font size to draw image text (only for ttf font!).'),
                default     => 10,
                type        => 'integer',
            },
            imgquality => {
                description => gettext('Quality from image in percent.'),
                default     => 80,
                type        => 'integer',
            },
        },
        Commands => {
            grab => {
                description => gettext('Grab a picture'),
                short       => 'gr',
                callback    => sub{ $obj->grab(@_) },
                Level       => 'user',
                DenyClass   => 'remote',
            },
            gdisplay => {
                description => gettext('Display the picture'),
                short       => 'gd',
                callback    => sub{ $obj->display(@_) },
                Level       => 'user',
                DenyClass   => 'remote',
            },
        },
    };
    return $args;
}

# ------------------
sub new {
# ------------------
	my($class, %attr) = @_;
	my $self = {};
	bless($self, $class);

    # paths
    $self->{paths} = delete $attr{'-paths'};

	# who am I
    $self->{MOD} = $self->module;

    # all configvalues to $self without parents (important for ConfigModule)
    map {
        $self->{$_} = $attr{'-config'}->{$self->{MOD}->{Name}}->{$_} || $self->{MOD}->{Preferences}->{$_}->{default}
    } keys %{$self->{MOD}->{Preferences}};

    # i need the svdrp module
    $self->{svdrp} = main::getModule('SVDRP');

    # create Template object
    $self->{tt} = Template->new(
      START_TAG    => '\[\?',		    # Tagstyle
      END_TAG      => '\?\]',		    # Tagstyle
      INTERPOLATE  => 1,                # expand "$var" in plain text
      PRE_CHOMP    => 1,                # cleanup whitespace
      EVAL_PERL    => 1,                # evaluate Perl code blocks
    );

	return $self;
}

# ------------------
sub grab {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $watcher = shift;
    my $console = shift;
    my $file    = $obj->{file};

    unlink($file) if(-e $file);

    # the svdrp module
    my $svdrp = $obj->{svdrp};

    # the command
    my $cmd = sprintf('grab %s jpeg %d %d %d',
            $obj->{file},
            $obj->{imgquality},
            $obj->{xsize},
            $obj->{ysize},
    );

    my $erg = $svdrp->command($cmd);

    # Make imgtext
    $file = $obj->makeImgText($file, $obj->{imgtext})
        if($obj->{imgtext});

    $console->msg($erg, $svdrp->err)
        if(ref $console);
    return $file;
}

# ------------------
sub display {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $watcher = shift || return error ('No Watcher!');
    my $console = shift || return error ('No Console');

    my $file = $obj->grab();

    return $console->image($file);
}

# ------------------
sub makeImgText {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $watcher = shift || return error ('No Watcher!');
    my $console = shift || return error ('No Console');
    my $file = shift || $obj->{file} || return error ('No File to display');
    my $text = shift || $obj->{imgtext} || return error ('No Text to display');

    my $im;
    if(int(${GD::VERSION}) >= 2.0) {
        $im = GD::Image->newFromJpeg($file, 1) || return error("Can't read $file $!");
    } else {
        $im = GD::Image->newFromJpeg($file) || return error("Can't read $file $!");
    }
    my $color   = $im->colorClosest(255,255,255);
    my $shadow  = $im->colorClosest(0,0,0);


    # XXX: Hier sollten noch mehr Informationen dazu kommen
    my $channeltext = main::getModule('REMOTE')->switch();
    my $channelpos = (split(' ', $channeltext))[0];
    my $vars = {
        channel => $channeltext,
        event => main::getModule('EPG')->NowOnChannel($channelpos),
    };

    my $output = '';
    $obj->{tt}->process(\$text, $vars, \$output)
          or return error($obj->{tt}->error());

    my $font = sprintf("%s/%s",$obj->{paths}->{FONTPATH},$obj->{font});
    if($obj->{paths}->{FONTPATH} and $obj->{font} and -r $font) {
        $im->stringFT($shadow,$font,$obj->{imgfontsize},0,11,($obj->{vpos}-1),$output);
        $im->stringFT($color,$font,$obj->{imgfontsize},0,10,($obj->{vpos}),$output);
    } else {
        # Schatten
        $im->string(gdGiantFont,11, ($obj->{vpos}-1),$output,$shadow);
        # Text
        $im->string(gdGiantFont,10, ($obj->{vpos}),$output,$color);
    }

    my $img_data = $im->jpeg($obj->{imgquality});
    my @f = split('\.', $file);
    my $newfile = ($file =~ 'text' ? $file : sprintf('%s_text.%s', @f));
    save_file($newfile, $img_data);
    return $newfile;
}

# ------------------
sub findttf
# ------------------
{
    my $obj = shift || return error ('No Object!' );
    my $found;
    find({ wanted => sub{
                if($File::Find::name =~ /\.ttf$/sig) {
                    my $l = basename($File::Find::name);
                    push(@{$found},[$l,$l]);
                }
           },
           follow => 1,
           follow_skip => 2,
        },
        $obj->{paths}->{FONTPATH}
    );
    warn "Can't find useful font at : ", $obj->{paths}->{FONTPATH}
        if(scalar $found == 0);
    return $found;
}

1;
