#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package MediaServe::DB::Item::BookProductImageFile;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::Assert;
use BookPub::DB::Item::Archive::BookProductImageFile;
use base 'Common::DB::Item';

use constant kFileTypeOriginal  => 'original';
use constant kFileTypeThumbnail => 'thumbnail';
use constant kFileTypeLarge     => 'large';

use constant kTable => 'book_product_image_file';
use constant kDB    => Common::DB::Item::kClientDB;

sub LookupByType {
    my $self = shift;
    my %args = @_;

    my $book_product_id = $args{book_product_id};
    my $filename        = $args{filename};
    my $type            = _getFileTypeID( $args{type} );

    my $dbo = Common::RSApp::GetClientDB();

    assert($type);

    return unless ( $book_product_id || $filename );

    my $sql =
        " SELECT "
      . kTable . ".* "
      . " FROM "
      . kTable
      . "     LEFT JOIN media_file USING (media_file_id) "
      . " WHERE "
      . kTable
      . ".file_type = "
      . $dbo->DBQuote($type) . " AND ";

    $sql .= " book_product_id = " . $dbo->DBQuote($book_product_id) if ($book_product_id);
    $sql .= " filename = " . $dbo->DBQuote($filename) if ($filename);

    Common::Log::Debug("QUERY: $sql");

    my $result = $self->SUPER::GetAll($sql);

    return $result->next;
}

sub _getFileTypeID {
    my $type = shift;

    assert( $type, "Type Required" );

    if ( $type eq 'thumbnail' ) {
        return kFileTypeThumbnail;
    } elsif ( $type eq 'large' ) {
        return kFileTypeLarge;
    } elsif ( $type eq 'original' ) {
        return kFileTypeOriginal;
    } else {
        assert( 0, "Invalid type: $type" );
    }
}

sub GetUnmatchedFiles {
    my $self = shift;
    my %args = @_;

    assert($self);

    my $sql = "SELECT * FROM " . kTable . " INNER JOIN media_file USING (media_file_id) " . " WHERE book_product_id IS NULL";

    Common::Log::Debug("QUERY: $sql");
    return $self->SUPER::GetAll($sql);
}

sub GetMatchIDs {
    my $self = shift;
    my %args = @_;
    my $dbo  = Common::RSApp::GetClientDB();

    assert($self);
    assert(%args);
    assert( $args{isbn10} || $args{isbn13} );

    my $sql = "SELECT DISTINCT(product_id) FROM book_product WHERE ";

    $sql .= 'isbn10 = ' . $dbo->DBQuote( $args{isbn10} );
    $sql .= ' OR isbn13 = ' . $dbo->DBQuote( $args{isbn13} );

    Common::Log::Debug("QUERY: $sql");
    my $sth = $dbo->DoCmd($sql);

    my @res;

    while ( my $id = $sth->fetchrow_array() ) {
        push( @res, $id );
    }

    return @res;
}

sub GetDuplicateAssets {
    my $class                      = shift;
    my $file_type                  = shift;
    my $book_product_id            = shift;
    my $book_product_image_file_id = shift;

    assert($class);
    assert($file_type);
    assert($book_product_id);
    assert($book_product_image_file_id);

    my $sql =
        "SELECT * " . "FROM "
      . kTable
      . " INNER JOIN media_file USING (media_file_id) "
      . "WHERE "
      . "file_type = \"$file_type\" "
      . "AND book_product_id = $book_product_id "
      . "AND book_product_image_file_id != $book_product_image_file_id";

    Common::Log::Debug("QUERY: $sql");
    return $class->SUPER::GetAll($sql);
}

sub OLDGetDuplicateAssets {
    my $class           = shift;
    my $filename        = shift;
    my $book_product_id = shift;

    assert($class);
    assert($filename);
    assert($book_product_id);

    my $sql =
        "SELECT * " . "FROM "
      . kTable
      . " INNER JOIN media_file USING (media_file_id) "
      . "WHERE "
      . "filename NOT LIKE '$filename%' "
      . "AND book_product_id = $book_product_id";

    Common::Log::Debug("QUERY: $sql");
    return $class->SUPER::GetAll($sql);
}

sub LookupByFilename {
    my $self = shift;
    my %args = @_;

    my $filename = $args{filename};
    my $dbo      = Common::RSApp::GetClientDB();

    assert($filename);

    my $sql = " SELECT " . kTable . ".* " . " FROM " . kTable . "     LEFT JOIN media_file USING (media_file_id) " . " WHERE ";

    $sql .= " filename = " . $dbo->DBQuote($filename) if ($filename);

    Common::Log::Debug("QUERY: $sql");

    my $result = $self->SUPER::GetAll($sql);

    return $result->next;
}

1;
