# +-----------------------------------------------------------------------+
# | pLoader - a Perl photo uploader for Piwigo                            |
# +-----------------------------------------------------------------------+
# | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
# +-----------------------------------------------------------------------+
# | 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                                          |
# |                                                                       |
# | 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, write to the Free Software           |
# | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
# | USA.                                                                  |
# +-----------------------------------------------------------------------+
package Uploader::Image;
use strict;
use base qw/Uploader::Object/;
use File::Spec;
use Image::ExifTool qw(:Public);
use Data::Dumper;

# we need a ref to data preferences to use with threads
my @properties = qw/
    file
    wx_thumb_file
    site_high_file
    site_name
    site_resized_file
    site_thumb_file
    site_author
    site_comment
    site_original_filename
    exif_metadata
    add_rank
    site_tags
    image_id
    width
    height
    privacy_level
    caption
    global_rank
/;

my $init_caption_from_pattern_cbk;

__PACKAGE__->mk_accessors(@properties);

sub Init {
    my ( $self ) = @_;


    my $exif = $self->read_exif_metadata(
        $self->file
    );

    $self->exif_metadata(
        $self->select_exif_data($exif)
    );

        

    $self->site_original_filename(
        $self->original_filename
    );

}


sub read_exif_metadata {
    my ( $self, $file ) = @_;
    
    # read exif metadata
    my $info;
    eval {
        $info = ImageInfo( $file );
    };
    $info = {} if($@);
    
    $info;
}


sub exif_tag {
    my ( $self, $tag ) = @_;

    my $exif = $self->exif_metadata ;
    
    $exif->{$tag};
}


sub create_date {
    my ( $self, $date ) = @_;
    
    my $exif = $self->exif_metadata ;
   
    $self->{-create_date} ||= $exif->{CreateDate};

    $self->{-create_date} = $date if defined $date;

    $self->{-create_date};
}


sub select_exif_data {
    my ( $self, $exif ) = @_;

    return {
        map {
            $_ => $exif->{$_},
        }
        qw/
            CreateDate
            ImageWidth
            ImageHeight
            Orientation
            DateTimeOriginal
            ISO
            ExposureTime
            ApertureValue
            FocalLength
            Lens
            Exposure
            Make
            Model
        /
    };    
}


sub original_filename {
    my ( $self ) = @_;

    my ( $vol, $dir, $filename ) = File::Spec->splitpath(
        $self->file
    );

    $filename;
}

sub get_data {
    my ( $self, $preferences, $destination_category, $add_rank ) = @_;

    my $data = {
        map { $_ => $self->$_ } @properties
    };
    $data->{preferences} = $preferences;
    $data->{categories}  = $destination_category;
    $data->{site_img_date_creation} = $self->create_date;
    $data->{add_rank} = $add_rank;

    return $data;
}

1;