#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package EmbedImage;
use strict;

use Image::Magick;

# The height and width are somewhat arbitrary, and were chosen to constrain
# the logo such that is positioned "nicely" on all PDF statements.
#
use constant kMaxHeight => 150;  # Anything over this height will get scaled to fit (~0.93" high at 72dpi)
use constant kMaxWidth  => 250;  # Anything over this width will get scaled to fit (~3.47" wide at 72dpi)

# This adds the label's logo to the top right corner of the first page.
#
sub labelLogo {
    my ( $pdf, $page, $logoName, $topMargin, $rightMargin ) = @_;

    my $photo = $page->gfx;

    my $imagePath = "/app/tools/common/production/images/payor_logos/";

    my $gifImage  = $imagePath . $logoName . ".gif";
    my $pngImage  = $imagePath . $logoName . ".png";
    my $jpgImage  = $imagePath . $logoName . ".jpg";
    my $jpegImage = $imagePath . $logoName . ".jpeg";


    my $imageFile;
    my $imageWidth;
    my $imageHeight;

    if ( -e $pngImage ) {
        ( $imageWidth, $imageHeight ) = pngDimensions($pngImage);
        $imageFile = $pdf->image_png($pngImage);

    } elsif ( -e $jpgImage ) {
        ( $imageWidth, $imageHeight ) = jpgDimensions($jpgImage);
        $imageFile = $pdf->image_jpeg($jpgImage);

    } elsif ( -e $jpegImage ) {
        ( $imageWidth, $imageHeight ) = jpgDimensions($jpegImage);
        $imageFile = $pdf->image_jpeg($jpegImage);

    } elsif ( -e $gifImage ) {
        ( $imageWidth, $imageHeight ) = gifDimensions($gifImage);
        $imageFile = $pdf->image_gif($gifImage);
    }

    if ( $imageFile ) {

        my $scaleFactor = 1;
        my $scaleHeight = $imageHeight;
        my $scaleWidth  = $imageWidth;

        if( $imageHeight > kMaxHeight ) {
            $scaleFactor = $imageHeight /  kMaxHeight;
            $scaleWidth = $imageWidth / $scaleFactor;
            $scaleHeight = kMaxHeight;
        }
        elsif( $imageWidth > kMaxWidth ) {
            $scaleFactor = $imageWidth /  kMaxWidth;
            $scaleWidth = kMaxWidth;
            $scaleHeight = $imageHeight/ $scaleFactor;
        }

        my $y = $topMargin - $scaleHeight;
        my $x = $rightMargin - $scaleWidth;

#        print "EmbedImage: x($x) y($y) --> height($scaleHeight) width($scaleWidth) scale($scaleFactor)\n";

        $photo->image( $imageFile, $x, $y, $scaleWidth, $scaleHeight );
    }

    return ( $pdf, $page );
}

sub jpgDimensions {
    my ($filename) = @_;
    my $image = Image::Magick->new;
    open( IMAGE, $filename );
    $image->Read( file => \*IMAGE );
    my ( $width, $height ) = $image->Get( 'columns', 'rows' );
    close( IMAGE );
    return ( $width, $height );
}

sub gifDimensions {
    my ($filename) = @_;

    open( GIF, $filename ) || return ( undef, undef );
    my $buf = '';
    my $n = read GIF, $buf, 10;
    close GIF;

    return ( undef, undef ) if $n < 10;
    my ( $head, $width, $height ) = unpack( "A6vv", $buf );
    return ( undef, undef ) unless $head =~ /^GIF8[79]a/;
    return ( $width, $height );
}

sub pngDimensions {
    my ($filename) = @_;
    my $dimensions;

    open( IMAGEFILE, $filename ) || return ( undef, undef );
    binmode IMAGEFILE;

    seek( IMAGEFILE, 16, 0 );
    if ( read( IMAGEFILE, $dimensions, 8 ) != 8 ) {
        close IMAGEFILE;
        return ( -1, -1 );
    }
    close IMAGEFILE;
    my ( $width, $height ) = unpack( "NN", $dimensions );
    return ( $width, $height );
}

sub addIconGIF {
    my %args = @_;

    my $page   = $args{page};
    my $pdf    = $args{pdf};
    my $source = $args{source};
    my $link   = $args{link};
    my $width  = $args{width};
    my $height = $args{height};
    my $x      = $args{x};
    my $y      = $args{y};

    # Let's make sure the image exists first.
    #
    if ( -e $source ) {
        my $images = $page->gfx;
        my $icon   = $pdf->image_gif($source);

        if ( !$width || !$height ) {
            ( $width, $height ) = gifDimensions($source);
        }

        $images->image( $icon, $x, $y, $width, $height );

        if ($link) {
            my $left   = $x;
            my $right  = $x + $width;
            my $bottom = $y;
            my $top    = $y + $height;

            my $clientNameClean = Common::Client::Current()->WebAlias();
            $clientNameClean = Common::Client::Current()->ClientNameClean() unless $clientNameClean;
            my $url = "https://" . $clientNameClean . ".royaltyshare.com" . $link;

            # And now we'll create the actual link
            my $annot = $page->annotation();
            $annot->url( $url, -rect => [ $left, $bottom, $right, $top ] );
        }

    }

}

1;
