#!/usr/bin/perl

use strict;
use Ernad::Presorting;
use Ernad::Chungju;
use Ernad::Erimp;
use Krichel::File;

my $impna = $ARGV[0] // '';

my $e=Ernad::Erimp->new({'impna'=>$impna});

my $adverts_fullfilled_dir=$e->{'conf'}->{'adverts_fullfilled_dir'} // '';

## to take away on meger
my $amf_ns=$e->{'const'}->{'amf_ns'};
my $allport_collection=$e->get_allport_collection();
my $allport_repcode = $e->get_allport_repcode();
my $allport_dir=$e->get_allport_dir();
my $us_dir=$allport_dir.'/source/us';
my $ps_dir=$allport_dir.'/source/ps';
my $today = Ernad::Dates::CurrentAsString( 3 );
##


my $rifs=&presort_allport();
&inject_advert($today,$rifs);

#######################################################################
#
#
# sub presort_allport {
#   my $in_rif=shift // '';
#   ## should not be, after merger
#   if(not $in_rif) {
#     ## Find last issue
#     my $iss;
#     $iss = Ernad::Common::GetOldestReport( $us_dir );
#     if( !$iss ) {
#       die( "Could not find any all issue.\n" );
#     }
#     else {
#       #print "found \$iss $iss\n";
#     }
#     my $path;
#     my $ts;
#     my $issue_to_do;
#     ## here only $issue_to_do is used further
#     &Ernad::Common::split_rif_name( $iss, \$path, \$issue_to_do, \$ts );
#     #print "we have to do $issue_to_do\n";
#     $in_rif="$us_dir/$iss";
#     if(not -f $in_rif) {
#       print "no such file: $in_rif\n";
#     }
#   }
#   my $issue_to_do=$today;
#   my $texts_by_handles;
#   my $xml= &Krichel::File::load( $in_rif );
#   my @collection_elements=$xml->getElementsByTagNameNS($amf_ns,'collection')->get_nodelist;
#   my $collection_element=$collection_elements[0];
#   foreach my $text_element ($xml->getElementsByTagNameNS($amf_ns,'text')->get_nodelist) {
#     my $handle=$text_element->getAttribute('ref');
#     #print $text_element->toString(1);
#     my @titles=$text_element->getElementsByTagName('title')->get_nodelist;
#     my $title=$titles[0]->textContent;
#     ## deal with double-quoted titles
#     $title=~s|^"||;
#     $title=~s|"$||;
#     my $haspart_element=$text_element->parentNode();
#     $texts_by_handles->{lc($title)}=$haspart_element->cloneNode(1);
#     $haspart_element->parentNode->removeChild($haspart_element);
#   }
#   foreach my $handle (sort keys %$texts_by_handles) {
#     $collection_element->appendChild($texts_by_handles->{$handle});
#   }
#
#   ## Delete previous presorted issues of that date
#   #unlink glob("$ps_dir/$issue_to_do*");
#   ## Generate new name
#   my $out_rif = $ps_dir.'/'.$e->{'f'}->by_date( $issue_to_do );
#   ## Save the result into presorted
#   $xml->toFile($out_rif,1);
#   ##print "Destination: $out_rif.\n";
#   ## deal with adverts
#   ## call script
#   ## fix me. This should be done in a libarry call
#   my $rifs;
#   $rifs->{'in'}=$in_rif;
#   $rifs->{'out'}=$out_rif;
#   return $rifs;
# }


sub inject_advert {
  my $issue_to_do=shift;
  my $rifs=shift;
  my $us_rif=$rifs->{'in'};
  my $ps_rif=$rifs->{'out'};
  my $order_file=&Ernad::Chungju::find_order_to_fullfill();
  if(not $order_file) {
    print "no order to fullfill\n";
    exit;
  }

  print "order file is $order_file\n";
  my $error=&Ernad::Chungju::fullfill_order($order_file,$issue_to_do);
  if($error) {
    print "problem when fullfilling order: $error\n";
    exit;
  }
  my $advert_file="$adverts_fullfilled_dir/$order_file";
  if(not -f $advert_file) {
    print "no such advert file: '$advert_file', this is a bug, write to Thomas!\n";
    exit;
  }

  my $advert_element=&Ernad::Chungju::transform_order_to_advert($advert_file,$allport_repcode);

  print "I inject advert from $advert_file into $us_rif\n";
  &Ernad::Chungju::inject_advert_into_rif($advert_element,$us_rif);

  print "I inject advert from $advert_file into $ps_rif\n";
  &Ernad::Chungju::inject_advert_into_rif($advert_element,$ps_rif);
}

exit;
