#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::Catalog::Import::Process::Fetcher::MacmillanAU;
use strict;
use warnings;
use Data::Dumper;
use Net::FTP;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::RSApp;
use Common::Util;
use Common::Log;
use Common::Assert;
use Common::Process;
use Common::DB::Item::Language;
use BookPub::DB::Item::CatalogImport;
use BookPub::Config;

use base 'BookPub::Catalog::Import::Process::Fetcher::Base';

sub _clientFTPDirectory {
    my ($self) = @_;
    return 'macmillanau';
}

sub _sortTokenFromFilename {
    my ( $self, $file ) = @_;

    my $token;
    if ( $file =~ /(\d\d\d\d)-(\d\d)(\d\d)\.(zip|xml|xlsx)/i ) {
        my $y = $1;
        my $m = $2;
        my $d = $3;
        $token = sprintf( "%04d%02d%02d", $y, $m, $d );
    } elsif ( $file =~ /(\d\d\d\d)(\d\d)(\d\d)/ ) {
        my $y = $1;
        my $m = $2;
        my $d = $3;
        $token = sprintf( "%04d%02d%02d", $y, $m, $d );
    }

    return $token;
}

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

    $self->_connectToFTP();
    $self->_changeToClientDirectory();

    my $directoryListing = $self->_ftpGetDirectoryListing();

    my $filteredListing = $self->_filterNewestFiles($directoryListing);

    my @files;
    foreach my $filename (@$filteredListing) {
        $self->_report( "  looking at file: $filename", 3 );

        # ALWAYS skip files that start with '_'...
        #
        next if ( '_' eq substr( $filename, 0, 1 ) );

        push @files, $filename;
        last;    # Let's just deal with one file at a time.
    }

    return @files;
}

###
1;    #
###

