#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Region::Region;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Region;
#use RPS::DB::Item::ArtistContractRegion;
use RPS::Region::CountryList;
use RPS::DB::Item::NewArtistContractTerm;

use Common::FormObject;
use base 'Common::FormObject';

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

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

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

    $self->_initProperties(\%properties);
    $self->_initSubs(\%properties);

    return $self;
}


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

	my $dbItem = RPS::DB::Item::Region->Lookup(region_id => $regionID);
	$self->_propertiesFromDBItem($hrProperties, $dbItem);
}


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

	$hrProperties->{region_id}      = $dbItem->region_id;
	$hrProperties->{name}           = $dbItem->name;
	$hrProperties->{description}    = $dbItem->description;
	$hrProperties->{is_default_mechanical_license_region}    = $dbItem->is_default_mechanical_license_region;
}

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

	$self->{RegionID} =     Common::FormObject::Scalar->new(value => $props->{region_id}, readOnly => 1);
	$self->{Name} =    		Common::FormObject::Scalar::String->new(value => $props->{name}, required => 1, maxlength => 64);
	$self->{Description} =  Common::FormObject::Scalar::String->new(value => $props->{description});

    # This flag is _not_ editable in the usual region form.
    # We'll have a seperate command that sets this value across the whole table at once.
    #
	$self->{IsDefaultMechanicalLicenseRegion} = 
     Common::FormObject::Scalar->new(value => $props->{is_default_mechanical_license_region}, readOnly => 1);
}

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

	if($self->loadSubs())
    {
	    $self->{CountryList} = RPS::Region::CountryList->new(regionID => $props->{region_id});

	    # is this region used in any contracts ?
	    #
        my $linked;
        my $contractRegions;
        if ($props->{region_id})
        {
            $linked = RPS::DB::Item::NewArtistContractTerm->IsRegionUsed($props->{region_id});
        }
	    $self->{LinkedContracts} = Common::FormObject::Scalar->new(value => $linked, readOnly => 1);

    }
}


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


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

	my $dbObj;
	if($self->RegionID())
	{
		$dbObj = RPS::DB::Item::Region->Lookup(region_id => $self->RegionID());
	}
	else
	{
		$dbObj = RPS::DB::Item::Region->Create();
	}

	# set fields here
	#
	$dbObj->name($self->Name());
	$dbObj->description($self->Description());

    # !!! Do I _want_ this to be editable along with the rest of the fields?
    # !!! Actually, I think not.   We will be changing this value with an explicit
    # !!! command.
#	$dbObj->is_default_mechanical_license_region($self->IsDefaultMechanicalLicenseRegion());

	# save db item
	#
	$dbObj->save();

	$self->{CountryList}->save(region_id => $dbObj->region_id);

	# reload object
	#
	$self->_init(_dbItem => $dbObj);
}

sub delete
{
	my $self = shift;
	return undef if($self->LinkedContracts() > 0);

	# we need to delete the country maps
	#
	foreach my $map (@{$self->{CountryList}->getCountries()})
	{
		$map->delete();
	}

	# delete this region
	#
	my $region = RPS::DB::Item::Region->Lookup(region_id => $self->RegionID());
	$region->delete();

	return 1;
}


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

    # If this is _already_ the default region, do nothing.
    #
    if ($self->IsDefaultMechanicalLicenseRegion())
    {
        return 1;
    }

    # The database interface will have a magic method to do this for us.
    #
    RPS::DB::Item::Region->SetAsDefaultMechanicalLicenseRegion($self->RegionID());

    return 1;
}



sub getCountryCodes
{
	my $self = shift;

	return (defined $self->{CountryList}) ? $self->{CountryList}->CountryCodes() : [];
}

###
1;#
###

