package Distribution::Packager::Snocap;

use strict;
use Data::Dumper;
use File::Basename;

use Common::Assert;

use lib '/app/tools/distribution/lib';
use Distribution::Util;

use base 'Distribution::PackagerNew';

sub _requireAllowed { 1 }

sub _templateFile { 'snocap.tt' }

sub _xsdFile { 'snocap1_4_3.xsd' }

sub _worldWideTerritory { 'WW' }

###
#   Manifest
###
sub _manifestFile { 'manifest.xls' }

sub _trackManifest {
    my $self   = shift;
    my $album  = shift;
    my $volume = shift;
    my $track  = shift;

    return {
        UPC    => $album->{upc_ean},
        Title  => $album->{title},
        Artist => $track->{'display-artist'},
        ISRC   => $track->{isrc},
        Track  => $track->{title},
    };
}

sub _manifest {
    my $self = shift;

    my $destFile = $self->_localManifestPath();
    my $output;

    assert($destFile);

    Common::Log::Debug("*** Processing manifest ***");
    Common::Log::Debug(" - Manifest file: $destFile");

    unless ( $self->_processedAlbumsCount() ) {
        Common::Log::Debug(" - No albums processed, not creating manifest");
        return 1;
    }

    if ( $self->{_manifest} ) {
        $output .= sprintf( "%s\t%s\t%s\t%s\t%s\n", 'upc', 'album title', 'artist name', 'isrc', 'track name' );

        foreach my $record ( @{ $self->{_manifest}->{Album} } ) {
            $output .=
              sprintf( "%s\t%s\t%s\t%s\t%s\n", $record->{UPC}, $record->{Title}, $record->{Artist}, $record->{ISRC}, $record->{Track} );
        }
        $self->_writeFile( $self->_localManifestPath(), $output );
    } else {
        Common::Log::Print("WARNING: No manifest data found");
    }

    return 1;
}

###
#   Album Art
###
sub _imageFormat {
    {
        geometry => '300x300',
        density  => 72,
    };
}

sub _albumImageName {
    shift->SUPER::_albumImageName( @_, format => "%s_300x300.%s" );
}

###
#   Album Audio
###
sub _audioConverter { 'MediaServe::Audio::Converter::MP3' }
sub _audioConverterOptions { { bitrate => 256 } }

sub _albumAudioFileName {
    shift->SUPER::_albumAudioFileName( @_, format => "%s_%02d_%03d.%s" );
}

###
#   Directory Structure
###

sub _createDistributionPackageName {
    my $self = shift;
    $self->SUPER::_createDistributionPackageName( format => $self->{_acctInfo}->Code() . "_%d%02d%02d_%02d%02d%02d" );
}

sub _localAudioFilePath {
    my $self   = shift;
    my $album  = shift;
    my $volume = shift;
    my $track  = shift;

    my $dir = $self->_localAlbumPath($album) . "/audio";
    mkdir_local($dir);

    return "$dir/" . $self->_albumAudioFileName( $album, $volume, $track );
}

sub _localMetadataPath {
    my $self  = shift;
    my $album = shift;

    my $dir = $self->_localAlbumPath($album) . "/metadata";
    mkdir_local($dir);

    return "$dir/" . $self->_albumMetadataName($album);
}

sub _localImagePath {
    my $self  = shift;
    my $album = shift;

    my $dir = $self->_localAlbumPath($album) . "/images";
    mkdir_local($dir);

    return "$dir/" . $self->_albumImageName($album);
}

1;
