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 RPS::DB::Item::Album;
use RPS::DB::Item::Track;
use RPS::DB::Item::Artist;
use RPS::DB::Item::Song;
use RPS::DB::Item::Master;


use base 'Common::Script';

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


sub _process {
    my $self = shift;

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

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

    my $all = RPS::DB::Item::Album->GetAll();

    while (my $album = $all->next())
    {
        my $title = $album->title();
        $title =~ s/^\s+//;
        $title =~ s/\s+$//;

        if ($title ne $album->title())
        {
            Log->warn("album id: " . $album->album_id() . " : translating '".$album->title()."' to '$title'");
            $album->title($title);
            $album->title_clean(Common::Util::clean_name($title));
            $album->save();
        }
    }
}

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

    my $all = RPS::DB::Item::Track->GetAll();
    while (my $track = $all->next())
    {
        my $title = $track->title();
        $title =~ s/^\s+//;
        $title =~ s/\s+$//;

        if ($title ne $track->title())
        {
            Log->warn("track id: " . $track->track_id() . " : translating '".$track->title()."' to '$title'");
            $track->title($title);
            $track->title_clean(Common::Util::clean_name($title));
            $track->save();
        }
    }
}

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

    my $all = RPS::DB::Item::Song->GetAll();
    while (my $song = $all->next())
    {
        my $title = $song->title();
        $title =~ s/^\s+//;
        $title =~ s/\s+$//;

        if ($title ne $song->title())
        {
            Log->warn("song id: " . $song->song_id() . " : translating '".$song->title()."' to '$title'");
            $song->title($title);
            $song->save();
        }
    }
}

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

    my $all = RPS::DB::Item::Master->GetAll();
    while (my $master = $all->next())
    {
        my $title = $master->title();
        $title =~ s/^\s+//;
        $title =~ s/\s+$//;

        if ($title ne $master->title())
        {
            Log->warn("song id: " . $master->song_id() . " : translating '".$master->title()."' to '$title'");
            $master->title($title);
            $master->save();
        }
    }
}

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

    my $all = RPS::DB::Item::Artist->GetAll();
    while (my $artist = $all->next())
    {
        my $name = $artist->name();
        $name =~ s/^\s+//;
        $name =~ s/\s+$//;

        if ($name ne $artist->name())
        {
            Log->warn("artist id: " . $artist->artist_id() . " : translating '".$artist->name()."' to '$name'");
            $artist->name($name);
            $artist->name_clean(Common::Util::clean_name($name));
            $artist->save();
        }
    }
}

1;
