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

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::FormObject;
use BookPub::DB::Item::WatchList;
use BookPub::DB::Item::WatchListItem;
use BookPub::DB::Item::Region;
use BookPub::DB::Item::Market;

use base 'Common::FormObject';

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

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

    my %properties;
    if ( $args{dbItem} ) {
        $self->_propertiesFromDBItem( \%properties, $args{dbItem} );
    } elsif ( $args{watchListID} ) {
        $self->_propertiesFromDB( \%properties, $args{watchListID} );
    } else {
        $self->_propertiesFromDefaults( \%properties );
    }

    $self->_initProperties( \%properties );

    return $self;
}

sub _propertiesFromDB {
    my ( $self, $hrProperties, $watchListID ) = @_;

    my $dbItem = BookPub::DB::Item::WatchList->Lookup( watch_list_id => $watchListID );

    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;
    assert($dbItem);

    $hrProperties->{watch_list_id}    = $dbItem->watch_list_id;
    $hrProperties->{name}             = $dbItem->name;
    $hrProperties->{description}      = $dbItem->description;
    $hrProperties->{global_id}        = $dbItem->global_id;
    $hrProperties->{public}           = $dbItem->public;
    $hrProperties->{dynamic}          = $dbItem->dynamic;
    $hrProperties->{publication_date} = $dbItem->publication_date;
    $hrProperties->{format}           = $dbItem->format;
    $hrProperties->{rank}             = $dbItem->rank;
    $hrProperties->{market}           = $dbItem->market;
    $hrProperties->{author}           = $dbItem->author;
    $hrProperties->{imprint_id}       = $dbItem->imprint_id;
    $hrProperties->{publisher_id}     = $dbItem->publisher_id;
    $hrProperties->{service_id}       = $dbItem->service_id;
    $hrProperties->{price}            = $dbItem->price;
    $hrProperties->{alert}            = $dbItem->alert;

    $hrProperties->{date_created}  = $dbItem->date_created;
    $hrProperties->{date_modified} = $dbItem->date_modified;
    $hrProperties->{created_by}    = $dbItem->created_by;
    $hrProperties->{modified_by}   = $dbItem->modified_by;
}

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

    $self->{WatchListID}     = $props->{watch_list_id};
    $self->{Name}            = Common::FormObject::Scalar::String->new( value => $props->{name} );
    $self->{Description}     = Common::FormObject::Scalar::String->new( value => $props->{description} );
    $self->{GlobalID}        = Common::FormObject::Scalar::String->new( value => $props->{global_id} );
    $self->{Public}          = Common::FormObject::Scalar::Boolean->new( value => $props->{public} );
    $self->{Dynamic}         = Common::FormObject::Scalar::Boolean->new( value => $props->{dynamic} );
    $self->{PublicationDate} = Common::FormObject::Scalar::Integer->new( value => $props->{publication_date} );
    $self->{Format}          = Common::FormObject::Scalar::Integer->new( value => $props->{format} );
    $self->{Rank}            = Common::FormObject::Scalar::Integer->new( value => $props->{rank} );
    $self->{Market}          = Common::FormObject::Scalar::Integer->new( value => $props->{market} );
    $self->{Author}          = Common::FormObject::Scalar::String->new( value => $props->{author} );
    $self->{ImprintID}       = Common::FormObject::Scalar::Integer->new( value => $props->{imprint_id} );
    $self->{PublisherID}     = Common::FormObject::Scalar::Integer->new( value => $props->{publisher_id} );
    $self->{ServiceID}       = Common::FormObject::Scalar::Integer->new( value => $props->{service_id} );
    $self->{Price}           = Common::FormObject::Scalar::Integer->new( value => $props->{price} );
    $self->{Alert}           = Common::FormObject::Scalar::Boolean->new( value => $props->{alert} );

    $self->{CreatedDate}  = Common::FormObject::Scalar::DateTime->new( value => $props->{date_created},  readOnly => 1 );
    $self->{ModifiedDate} = Common::FormObject::Scalar::DateTime->new( value => $props->{date_modified}, readOnly => 1 );
    $self->{CreatedBy}    = Common::FormObject::Scalar::UserName->new( value => $props->{created_by},    readOnly => 1 );
    $self->{ModifiedBy}   = Common::FormObject::Scalar::UserName->new( value => $props->{modified_by},   readOnly => 1 );

    # Owners/Creators of watch list have certain privileges.
    # Let's set a flag for use in the template.
    my $isOwner      = 0;
    my $activeUserID = Common::RSApp::GetActiveUserID();
    if ( $props->{created_by} == $activeUserID ) {
        $isOwner = 1;
    }
    $self->{IsOwner} = Common::FormObject::Scalar::Boolean->new( value => $isOwner );
}

sub _propertiesFromDefaults {
    my ( $self, $hrProperties ) = @_;
}

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

    my $valid = $self->SUPER::validate();

    # Lists that have global IDs cannot be edited by users.
    #
    if ( $self->GlobalID() ) {
        $valid = 0;
    }

    return $valid;
}

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

    my $watchListDBObj;
    if ( $self->WatchListID() ) {
        $watchListDBObj = BookPub::DB::Item::WatchList->Lookup( watch_list_id => $self->WatchListID() );

        # We want to delete saved watch list items when switching from simple to dynamic.
        #
        if ( $self->Dynamic() ) {
            BookPub::DB::Item::WatchListItem->DeleteByWatchListID( $self->WatchListID() );
        }
    } else {
        $watchListDBObj = BookPub::DB::Item::WatchList->Create();
    }

    $watchListDBObj->name( $self->Name() );
    $watchListDBObj->description( $self->Description() );
    $watchListDBObj->global_id( $self->GlobalID() );
    $watchListDBObj->public( $self->Public() );
    $watchListDBObj->dynamic( $self->Dynamic() );
    $watchListDBObj->publication_date( $self->PublicationDate() );
    $watchListDBObj->format( $self->Format() );
    $watchListDBObj->rank( $self->Rank() );
    $watchListDBObj->market( $self->Market() );
    $watchListDBObj->author( $self->Author() );
    $watchListDBObj->imprint_id( $self->ImprintID() );
    $watchListDBObj->price( $self->Price() );
    $watchListDBObj->publisher_id( $self->PublisherID() );
    $watchListDBObj->service_id( $self->ServiceID() );
    $watchListDBObj->alert( $self->Alert() );

    $watchListDBObj->save();

    $self->WatchListID( $watchListDBObj->watch_list_id );

    return $watchListDBObj;

}

sub delete {

    # !!! We should make sure this user is allowed to delete this watch list. !!!
    #

    my ($self) = @_;

    # Delete this list
    #
    my $list = BookPub::DB::Item::WatchList->Lookup( watch_list_id => $self->WatchListID() );
    $list->delete();

    # (Really) Delete watch list items
    #
    BookPub::DB::Item::WatchListItem->DeleteByWatchListID( $self->WatchListID() );

    return 1;
}

sub getMarketIDs {
    my $self = shift;
    my @marketIDs;
    my @regionIDs;

    my $regionCollection = BookPub::DB::Item::Region->GetAllRegions( country_code => 'US' );

    while ( my $region = $regionCollection->next() ) {
        push( @regionIDs, $region->region_id );
    }

    if (@regionIDs) {
        my $marketCollection = BookPub::DB::Item::Market->GetAllByRegionIDs(@regionIDs);
        while ( my $market = $marketCollection->next() ) {
            push( @marketIDs, $market->market_id() );
        }
    }

    return @marketIDs ? \@marketIDs : undef;
}

###
1;    #
###
