#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Catalog::Product::Book::WithFormat;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use lib '/app/tools/mediaserve/lib';
use BookPub::DB::Item::BookProduct;
use BookPub::DB::Item::ProductType;
use BookPub::DB::Item::BookProductImageFile;
use BookPub::DB::Item::BookFormat;

#use BookPub::Catalog::BookFormat;
use Common::RSApp;
use Common::Util;
use Common::Log;
use Common::Assert;
use Common::FormObject;
use MediaServe::Image::ImageFile;
use BookPub::Catalog::ProductFormat;
use BookPub::Catalog::BookProductContributorList;
use BookPub::Catalog::BookProductContributorHash;
use BookPub::Catalog::ContributorRoleList;

use base 'BookPub::Catalog::Product::Book';

use constant kDefaultCoverArtThumbnail => "/production/images/no_book_cover_thumbnail.png";
use constant kDefaultCoverArtWeb       => "/production/images/no_book_cover_web.png";
use constant kDefaultCoverArt          => "/production/images/no_album_art.png";

sub _initProperties {
    my ( $self, $props ) = @_;

    # Invoke the inherited method first.
    #
    $self->SUPER::_initProperties($props);

    if ( $props->{product_id} ) {

        my $bookProduct = BookPub::DB::Item::BookProduct->Lookup( product_id => $props->{product_id} );

        # We want to display the format
        #
        $self->{ProductFormat} = BookPub::Catalog::ProductFormat->new( bookFormatID => $bookProduct->book_format_id, );

        # Going to stuff the cover art in here as well.
        #
        my ( $coverArt, $web, $thumbnail ) = $self->_getCoverArtURLs( $props->{product_id} );

        if ( kDefaultCoverArtThumbnail eq $thumbnail ) {

            # Let's see if the hardcover book_product has images.
            #
            # There may be more than 1 hardcover, so we'll take the first one that gives us an image.
            #
            my $hardcoverBookFormat = BookPub::DB::Item::BookFormat->FindByOnixCode( type_onix_code => 'BB', type_onix_code_list => 7 );
            if ($hardcoverBookFormat) {
                my $hardcoverProducts = BookPub::DB::Item::BookProduct->GetAllByBookIDBookFormatID( $bookProduct->book_id(),
                    $hardcoverBookFormat->book_format_id() );
                while ( my $hardcoverBookProduct = $hardcoverProducts->next() ) {
                    ( $coverArt, $web, $thumbnail ) = $self->_getCoverArtURLs( $hardcoverBookProduct->product_id() );
                    last if ( kDefaultCoverArtThumbnail ne $thumbnail );
                }
            }
        }

        $self->{CoverArtThumbnail} = $thumbnail;
        $self->{CoverArtWeb}       = $web;
        $self->{CoverArt}          = $coverArt;

    }

    $self->{ContributorHash} = BookPub::Catalog::BookProductContributorHash->new( productID => $props->{product_id} );
}

sub _getCoverArtURLs {
    my ( $self, $bookProductID ) = @_;

    my $thumbnail;
    my $web;
    my $cover;

    # !!! I'd like to put a 'trick' in here.
    # !!! If this product doesn't have images, then try and use the hardcover images.
    # !!! So the trick is to determine the correct default image set to use.
    # !!! The desire is to use the hardcover image, if there is one.
    # !!! Of course, there could be more than 1 hardcover product, in theory.

    if ($bookProductID) {

        # Lookup the record in the mapping table (book_product_image_file) to fetch the media_file_id.
        # Then use the MediaServe interface to fetch the ImageFile, which will yield the url.
        # Note that this is a 'temporary' url, tied to this session.
        #
        my $thumbFile = BookPub::DB::Item::BookProductImageFile->Lookup(
            book_product_id => $bookProductID,
            file_type       => 'thumbnail',
        );

        if ($thumbFile) {

            #Common::Log::Print("FOUND THUMBNAIL");
            my $image = MediaServe::Image::ImageFile->new( clientID => Common::RSApp::GetClientID(), id => $thumbFile->media_file_id );
            if ($image) {
                $thumbnail = $image->url();
            }
        }

        my $webFile = BookPub::DB::Item::BookProductImageFile->Lookup(
            book_product_id => $bookProductID,
            file_type       => 'large',
        );

        if ($webFile) {

            #Common::Log::Print("FOUND WEB");
            my $image = MediaServe::Image::ImageFile->new( clientID => Common::RSApp::GetClientID(), id => $webFile->media_file_id );
            if ($image) {
                $web = $image->url();
            }
        }

        my $fullImage = BookPub::DB::Item::BookProductImageFile->Lookup(
            book_product_id => $bookProductID,
            file_type       => 'large',
        );

        if ($fullImage) {
            my $image = MediaServe::Image::ImageFile->new( clientID => Common::RSApp::GetClientID(), id => $fullImage->media_file_id );
            if ($image) {
                $cover = $image->url();
            }
        }

        # If I don't have either image, let's try serving the original image.

        my $orig;
        my $origImage = BookPub::DB::Item::BookProductImageFile->Lookup(
            book_product_id => $bookProductID,
            file_type       => 'original',
        );

        if ($origImage) {
            my $image = MediaServe::Image::ImageFile->new( clientID => Common::RSApp::GetClientID(), id => $origImage->media_file_id );
            if ($image) {
                $orig = $image->url();
            }
        }

        $thumbnail = $orig unless $thumbnail;
        $web       = $orig unless $web;
        $cover     = $orig unless $cover;
    }

    $thumbnail = kDefaultCoverArtThumbnail unless $thumbnail;
    $web       = kDefaultCoverArtWeb       unless $web;
    $cover     = kDefaultCoverArt          unless $cover;

    return ( $cover, $web, $thumbnail );
}

###
1;    #
###
