#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Catalog::Import::Process::Fetcher::Wiley;
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 BookPub::Catalog::Import::Process::Fetcher::FileHandler::CopyThenIgnore;
use BookPub::Catalog::Import::Process::Fetcher::FileHandler::ONIX;
use BookPub::Catalog::Import::Process::Fetcher::FileHandler::ZipArchive::ONIX;

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

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

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

    # Wiley has two different naming schemes:  One for incremental files, and
    # one for their full files.   We get the full files once a month, apparently.
    #
    # The full files have excellent file names, such as: 'wiley.2.20110516.zip'.  Although they do
    # seem to play fast and loose with the left-hand side of the filename, we do end up with a
    # date on the right side of the name,
    #
    # The incremental files use a 'MONxx' format, like 'wiley_incremental_may11-may17.zip'.
    # These sometimes have two dates, sometimes one.
    #
    # The internal date within these incremental files (i.e. the date_effective) matches the last date
    # in the filename.  So I can work with that.
    #
    # Note that these incremental files don't have years, so we need to be careful we don't mess up when
    # we're processing files from the end of December in January.

    # Make a mapping of file date to file name.
    # We'll need to translate the '-may17.zip'-style names into yyyymmdd dates, so
    # we can sort properly.
    #
    # We want the current month and year, so we can figure out what year to use for the incremental files.
    #
    my %monthMap = (
        'jan' => 1,
        'feb' => 2,
        'mar' => 3,
        'apr' => 4,
        'may' => 5,
        'jun' => 6,
        'jul' => 7,
        'aug' => 8,
        'sep' => 9,
        'oct' => 10,
        'nov' => 11,
        'dec' => 12,
    );

    my $token;
    if ( $file =~ /(\d\d\d\d\d\d\d\d)\.zip$/i ) {
        $token = $1;
    } elsif ( $file =~ /_(\d\d\d\d\d\d\d\d)\d*_RSCAT\.xlsx$/i ) {
        $token = $1;
    } elsif ( $file =~ /(\D\D\D)(\d\d)\.zip$/i ) {
        my $monthChunk = $1;
        my $dayChunk   = $2;
        my @timeBits   = localtime(time);
        my $year       = $timeBits[5] + 1900;
        my $month      = $timeBits[4] + 1;

        # Make sure the month chunk makes some sort of sense.
        #
        my $monthNum = $monthMap{$monthChunk};
        if ($monthNum) {

            # Set the year to the current year, unless this is a december file and we're running this in january.
            #
            my $yearNum = $year;
            if ( 12 == $monthNum && $month == 1 ) {
                $yearNum--;
            }

            $token = sprintf( "%04d%02d%02d", $yearNum, $monthNum, $dayChunk );
        }
    } elsif ( $file =~ /(\D\D\D)(\d\d)_(\d{4})\.zip$/i ) {
        my $monthChunk = $1;
        my $dayChunk   = $2;
        my $yearNum    = $3;

        # Make sure the month chunk makes some sort of sense.
        #
        my $monthNum = $monthMap{$monthChunk};
        if ($monthNum) {
            $token = sprintf( "%04d%02d%02d", $yearNum, $monthNum, $dayChunk );
        }
    }

    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;    #
###

