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

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::OnixCode;
use BookPub::DB::Item::BookFormat;
use BookPub::DB::Item::BookFormatType;
use BookPub::DB::Item::BookFormatSubtype;
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::FormObject;

use base 'Common::FormObject';

use constant kProductFormCodeList       => 7;
use constant kProductFormDetailCodeList => 78;
use constant kEpubTypeCodeList          => 10;

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    # If we don't have a productFormCode, we don't have anything else either.
    #
    if ( defined $args{bookFormatID} ) {

        my $bookFormat = BookPub::DB::Item::BookFormat->Lookup( book_format_id => $args{bookFormatID} );
        my $bookFormatType = BookPub::DB::Item::BookFormatType->Lookup( book_format_type_id => $bookFormat->book_format_type_id );
        my $codeItem = BookPub::DB::Item::OnixCode->Lookup( onix_code_id => $bookFormatType->onix_code_id );

        $self->{BookFormatType}{Code}        = $codeItem->code_value();
        $self->{BookFormatType}{Description} = $codeItem->description();
        $self->{BookFormatType}{Notes}       = $codeItem->code_value();
        $self->{BookFormatType}{OnixCodeID}  = $bookFormatType->onix_code_id;

        if ( $bookFormat->book_format_subtype_id ) {
            my $bookFormatSubtype =
              BookPub::DB::Item::BookFormatSubtype->Lookup( book_format_subtype_id => $bookFormat->book_format_subtype_id );
            my $subtypeItem = BookPub::DB::Item::OnixCode->Lookup( onix_code_id => $bookFormatSubtype->onix_code_id );

            $self->{BookFormatSubtype}{Code}        = $subtypeItem->code_value();
            $self->{BookFormatSubtype}{Description} = $subtypeItem->description();
            $self->{BookFormatSubtype}{Notes}       = $subtypeItem->code_value();
            $self->{BookFormatSubtype}{OnixCodeID}  = $bookFormatSubtype->onix_code_id;
        }

    }

    return $self;
}

sub asString {
    my ($self) = @_;
    my $description = $self->{BookFormatType}{Description};
    if ( $self->{BookFormatSubtype} ) {
        $description .= ' ' . $self->{BookFormatSubtype}{Description};
    }

    return $description;
}

sub isSubFormat {
    my ( $self, $onixcode ) = @_;

    assert($onixcode);

    return unless ( $self->{BookFormatSubtype} );

    Log->debug(" subformat match, '$onixcode' eq '$self->{BookFormatSubtype}{Code}' ($self->{BookFormatSubtype}{Description})");

    return $self->{BookFormatSubtype}{Code} && lc( $self->{BookFormatSubtype}{Code} ) eq lc($onixcode);
}

###
1;    #
###
