use strict;

ReClean->start();

package ReClean;
use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';

use Common::Log;
use Common::Util;
use Common::RSDB;
use Common::Client;
use RPS::DB::Item::Album;
use RPS::DB::Item::Track;
use RPS::DB::Item::Artist;

use base 'Common::Script';

#sub _options {{
#  client_id => {
#      short        => 'c',
#      required     => 1,
#      description  => 'Limit to this client id',
#      parameter    => 'i'
#  },
#}}

# We only want to run this for RPS clients.
#
sub _get_client_list {
    my $self = shift;

    my @list = $self->SUPER::_get_client_list();
    my @filteredList;
    foreach my $clientID (@list) {
        push @filteredList, $clientID if Common::RSDB::_isRPSClient($clientID);
    }

    return @filteredList;
}

sub _process {
    my $self = shift;

    # !!! debugging
    #
    my $client = Common::Client::Current();
    Common::Log::Print( "Processing: " . $client->ClientName() . "\t" . $client->ClientID() );

    $self->_reCleanAlbums();
    $self->_reCleanTracks();
    $self->_reCleanArtists();
}

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

    my $all = RPS::DB::Item::Album->GetAll();
    while ( my $album = $all->next() ) {
        $album->title_clean( Common::Util::clean_name_matching( $album->title() ) );
        $album->save();
    }
}

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

    my $all = RPS::DB::Item::Track->GetAll();
    while ( my $track = $all->next() ) {
        $track->title_clean( Common::Util::clean_name_matching( $track->title() ) );
        $track->save();
    }
}

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

    my $all = RPS::DB::Item::Artist->GetAll();
    while ( my $artist = $all->next() ) {
        $artist->name_clean( Common::Util::clean_name_matching( $artist->name() ) );
        $artist->save();
    }
}

1;
