package Distribution::Service::247Entertainment;

use strict;

use lib '/app/tools/distribution/lib';
use Distribution::Delivery::SFTP;
use base 'Distribution::Manager';

use constant TEMPLATE => '247entertainment.tt';

# directory structure:
# YYYYMMDD_#####/
#     UPCUPCUPCUPC/
#         UPCUPCUPCUPC_DD_TTT_FULL.FLAC
#         UPCUPCUPCUPC_COVER.JPG
#         UPCUPCUPCUPC.XML

sub build_package {
    my $self = shift;

    my $distDirName = $self->_get_dist_dir_name();
    my $baseDir     = $self->dist_dir($distDirName);
    $self->make_path($baseDir);

  ALBUM: foreach my $album ( @{ $self->albums() } ) {
        ## TODO: need their territory restriction info
        unless ( $self->_can_be_sold_in( $album, ["US"] ) ) {
            $self->_album_denied( $album, $self->{TerritoriesDeniedMsg} );
            next ALBUM;
        }

        my $upc = $album->{upc};
        my $albDir = join( '/', $baseDir, $upc );
        $self->make_path($albDir);

        $album->{'vendor-id'} = $self->{AcctInfo}{code};

        my $imgDest = sprintf( "%s/%s_COVER.JPG", $albDir, $upc );
        ## TODO: get image specs
        unless (
            $self->_convert_image(
                src  => $self->_windoze_path( $album->{'image-path-full'} ),
                dest => $imgDest,
                xy   => 600,
                dpi  => 300
            )
          ) {
            print STDERR "failed to convert image for $upc ($album->{'image-path-full'})\n";
            $self->_cleanup_dir($albDir);
            next;
        }
        $album->{'image-filename'} = $upc . '.jpg';
        $album->{'image-checksum'} = $self->_get_file_checksum($imgDest);

        foreach my $volume ( @{ $album->{volume} } ) {
            my %volAttrs  = $volume->getXMLParams();
            my $volumeNum = $volAttrs{id};

            foreach my $track ( @{ $volume->{track} } ) {
                my $trackSrc = $self->_windoze_path( $track->{'audio-path-full'} );
                my $trackSec = $self->_get_track_seconds($trackSrc);
                $track->{length}   = $self->_seconds_to_iso8601($trackSec);
                $track->{duration} = $trackSec;

                my $trackNum = $track->{'sequence'};
                my $trackName = sprintf( "%s_%02d_%03d_FULL.FLAC", $upc, $volumeNum, $trackNum );
                unless ( File::Copy::copy( $trackSrc, "$albDir/$trackName" ) ) {
                    print STDERR "failed to copy $upc disc $volumeNum, track $trackNum: $!\n";
                    $self->_cleanup_dir($albDir);
                    next ALBUM;
                }
                $track->{'audio-filename'} = $trackName;
                $track->{'audio-checksum'} = $self->_get_file_checksum("$albDir/$trackName");
            }

        }

        ## TODO: get WW code
        unless ( $self->_generate_metadata( dataSrc => $album, xmlDest => "$albDir/$upc.xml", wwCode => "WW" ) ) {
            print STDERR "metadata conversion failed on $upc\n";
            $self->_cleanup_dir($albDir);
            next;
        }

        $self->_complete_albums($album);
    }

    return 1;
}

sub deliver_package {
    my $self = shift;

    # send the big blob
    my $retval = $self->SUPER::deliver_package();

    return $retval if ( $retval == 0 );

    my $confirmation_file = join( '/', $self->{TempDir}, 'delivery.complete' );
    open( F, '>', $confirmation_file ) or die "can't open $confirmation_file: $!";
    close(F) or warn "can't close $confirmation_file: $!";

    return $self->SUPER::deliver_package( src => 'delivery.complete', dest => $self->{DistDirName}, dont_retry => 1 );
}

sub _get_dist_dir_name {
    my $self = shift;
    my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime( $self->{Timestamp} );

    ## TODO, actually they want a 5-digit counter at the end, rather than the date

    return sprintf( '%d%02d%02d_%02d%02d%02d', $year + 1900, $mon + 1, $mday, $hour, $min, $sec );
}

1;
