#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Track::Command::PreSubmitXML;
use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::Master::Master;
use RPS::DB::Item::Master;
use RPS::Song::Song;
use RPS::DB::Item::Song;

use lib '/app/tools/common/lib';
use RSApache::Response::XML;
use Common::FormObject;

use RPS::XMLCommand;
use base 'RPS::XMLCommand';

sub cmd  { return 'presubmit_xml'; }
sub area { return 'track'; }

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

	my $response = $self->SUPER::execute();
	return $response if $response;

	$self->checkForDuplicateMaster();
	$self->checkForDuplicateSong();

	$response = RSApache::Response::XML->new($self->{xml});

	return $response;
}





# ------------------------------------------
# Help functions to check for duplicates
# ------------------------------------------


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

	my $newISWC = $self->getParam("SongISWC");
	my $newTitle = $self->getParam("SongTitle");
	return 0 if(!$newISWC && !$newTitle);

	my $dupsFound = 0;
	if(my $songID = $self->getParam("SongID"))
	{
		my $songObj = RPS::DB::Item::Song->Lookup(song_id => $songID);

		# was song title submitted?
		#
		if(defined $songObj)
		{
			# is this title different from the db?
			#
			if(lc $newTitle ne lc $songObj->title())
			{
				# ok, now we know we should check for duplicate
				my $songs = RPS::DB::Item::Song->Match($newTitle);
				if(defined $songs && $songs->hasNext())
				{
					my $data = $songs->next();
					push @{$self->{xml}{DuplicateSongs}}, new RPS::Song::Song(_dbItem => $data);
					$dupsFound++;
				}
			}

			# is this ISWC different from the db?
			#
			if(lc $newISWC ne lc $songObj->iswc())
			{
				# ok, now we know we should check for duplicate
				my $songs = RPS::DB::Item::Song->Match($newISWC);
				if(defined $songs && $songs->hasNext())
				{
					my $data = $songs->next();
					push @{$self->{xml}{DuplicateSongs}}, new RPS::Song::Song(_dbItem => $data);
					$dupsFound++;
				}
			}
		}
	}
	else
	{
		my $songs = RPS::DB::Item::Song->Match($newTitle);
		if(defined $songs && $songs->hasNext())
		{
			my $data = $songs->next();
			push @{$self->{xml}{DuplicateSongs}}, new RPS::Song::Song(_dbItem => $data);
			$dupsFound++;
		}

		$songs = RPS::DB::Item::Song->Match($newISWC);
		if(defined $songs && $songs->hasNext())
		{
			my $data = $songs->next();
			push @{$self->{xml}{DuplicateSongs}}, new RPS::Song::Song(_dbItem => $data);
			$dupsFound++;
		}
	}

	return $dupsFound;
}


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

	my $newISRC = $self->getParam("MasterISRC");
	my $newTitle = $self->getParam("MasterTitle");
	return 0 if(!$newISRC && !$newTitle);

	my $dupsFound = 0;
	if(my $masterID = $self->getParam("MasterID"))
	{
		my $masterObj = RPS::DB::Item::Master->Lookup(master_id => $masterID);

		if(defined $masterObj)
		{
			my $masters;

			# is this title different from the db?
			#
			if($newTitle && lc $newTitle ne lc $masterObj->title())
			{
				$masters = RPS::DB::Item::Master->Match($newTitle);
				if(defined $masters && $masters->hasNext())
				{
					my $data = $masters->next();
					push @{$self->{xml}{DuplicateMasters}}, new RPS::Master::Master(_dbItem => $data);
					$dupsFound++;
				}
			}

			# is this isrc different from the db?
			#
			if($newISRC && lc $newISRC ne lc $masterObj->isrc())
			{
				$masters = RPS::DB::Item::Master->Match($newISRC);
				if(defined $masters && $masters->hasNext())
				{
					my $data = $masters->next();
					push @{$self->{xml}{DuplicateMasters}}, new RPS::Master::Master(_dbItem => $data);
					$dupsFound++;
				}
			}
		}
	}
	else
	{
		my $masters = RPS::DB::Item::Master->Match($newTitle);
		if(defined $masters && $masters->hasNext())
		{
			my $data = $masters->next();
			push @{$self->{xml}{DuplicateMasters}}, new RPS::Master::Master(_dbItem => $data);
			$dupsFound++;
		}

		$masters = RPS::DB::Item::Master->Match($newISRC);
		if(defined $masters && $masters->hasNext())
		{
			my $data = $masters->next();
			push @{$self->{xml}{DuplicateMasters}}, new RPS::Master::Master(_dbItem => $data);
			$dupsFound++;
		}
	}

	return $dupsFound;
}


###
1;# Play nicely.
###
