package MpmasCropYields;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use CtUtilities qw(queryhash extqueryhash);
use MpmasInputfile;
use MpmasInputBlock;
our @ISA = qw(MpmasInputfile);

sub filename {
	my $self = shift;
	return "CropYields".$self->file_suffix;
}


sub prepare_special {
		my $self = shift;
		my $y = $self->argument()->[0];
		
		my $xnychead = MpmasInputBlock->new(2,10);	
		$xnychead->add_comment_row("CROP COEFFICIENTS");
		$xnychead->add_comment_row("","This is a yield look-up table for year 1 that can be used in place of an external crop growth model");	
		$xnychead->add_comment_row("", "Sheet for a particular year is only required when crop yields have changed");
		$xnychead->add_comment_row("", "Note: Counting of years is as follows: 01,  02, 03, …., 10, 11");	
		$xnychead->add_comment_row(1,"CROP ACTIVITIES")	;
		$xnychead->add_comment_row("1.1",	"CROP YIELDS (ONLY THOSE THAT HAVE CHANGED)");
		$xnychead->add_comment_row("","", "Activity Link ID",  "Yield", "Stover Yield");
		
		my $c = 0;
		if ($self->config_entry("STATICNRU") == 1 and $self->config_entry("EXTERNALCGFORMAT0")) {
			for my $o (@ {$self->get_global("Sorted_Xn_Activity_Objects")}) {
				my $yield = $o->subelement_field("external_crop_growth", "yield_product1_$y");
				my $stoveryield = $o->subelement_field("external_crop_growth", "yield_product2_$y");
				
				my @externalyieldexp = ();
				if ($self->config_entry("EXO_YIELD_EXPECTATIONS"))
				{
					unless (defined $o->subelement_field("external_crop_growth", "expected_yield_product1_$y") ) {
					  print STDERR "Error: You turned on EXO_YIELD_EXPECTATIONS, but did not provide an expected yield (product 1) for ".$o->id."\n";
					  exit(1);
					}
					$externalyieldexp[0] = $o->subelement_field("external_crop_growth", "expected_yield_product1_$y") ;

					unless (defined $o->subelement_field("external_crop_growth", "expected_yield_product2_$y") ) {
					  print STDERR "Error: You turned on EXO_YIELD_EXPECTATIONS, but did not provide an expected yield (product 2) for ".$o->id."\n";
					  exit(1);
					}
					$externalyieldexp[1] = $o->subelement_field("external_crop_growth", "expected_yield_product2_$y") ;
				}

				if (defined $yield or defined $stoveryield) {
					$xnychead->add_row("",$o->id,$o->subelement_field("external_crop_growth", "link"), $yield ,$stoveryield || 0, @externalyieldexp)  ;	
					$c++;
				}
			}
		}
		else {
			my $xnyieldtab0 = $self->get_global("ExternalYields");
			my $xnyieldexp0;
			if ($self->config_entry("EXO_YIELD_EXPECTATIONS")) {
				$xnyieldexp0 = $self->get_global("ExternalYieldExpectations");
			}
			
			my $xnyieldtab;
			my $xnyieldexp;
			
			if (ref $self->get_global("ExternalYields") eq "MpmasQl3EqnElements::mpmasTableCollection") {
                my $t = $self->argument()->[1];
    			$xnyieldtab = $self->get_global("ExternalYields")->get_subtable($t);
			
			}
			elsif (ref $xnyieldtab0 eq "IndexedTable") {
			    $xnyieldtab = $self->get_global("ExternalYields");
			}
			else {
			    print STDERR "Error: Problem with self->get_global(\"ExternalYields\"): ", $self->get_global("ExternalYields"), "\n";
			}
			if (defined $self->get_global("ExternalYieldExpectations")) {
			    if (ref $self->get_global("ExternalYieldExpectations") eq "MpmasQl3EqnElements::mpmasTableCollection") {
                    my $t = $self->argument()->[2];
        			$xnyieldexp = $self->get_global("ExternalYieldExpectations")->get_subtable($t);
			    
			    }
			    elsif (ref $xnyieldexp0 eq "IndexedTable") {
			        $xnyieldexp = $self->get_global("ExternalYieldExpectations");
			    }
			}
			
			
			
			
			for my $o (@ {$self->get_global("Sorted_Xn_Activities")}) {#
				my $yield = $xnyieldtab->query([$o->[3], $y],3);
				my $stoveryield = $xnyieldtab->query([$o->[3], $y],4);
				my @externalyieldexp = ();
				
				
				
				if ($self->config_entry("EXO_YIELD_EXPECTATIONS")) {
					unless (defined $xnyieldexp->query([$o->[3], $y],3) ) {
					  print STDERR "Error: You turned on EXO_YIELD_EXPECTATIONS, but did not provide an expected yield (product 1) for ".$o->[3]."\n";
					  exit(1);
					}
					$externalyieldexp[0] = $xnyieldexp->query([$o->[3], $y],3) ;

					unless (defined $xnyieldexp->query([$o->[3], $y],4) ) {
					  print STDERR "Error: You turned on EXO_YIELD_EXPECTATIONS, but did not provide an expected yield (product 2) for ".$o->[3]."\n";
					  exit(1);
					}
					$externalyieldexp[1] = $xnyieldexp->query([$o->[3], $y],4) ;

				}
				
				my @extraAttrib =	$self->get_cfg_list_empty_ok("EXTRA_CROPACT_CHARACTERISTICS");
				my @extraAttribValues =	();
				if($self->config_entry("MULTIPERIOD") && scalar( @extraAttrib)) {
					for my $e (	@extraAttrib) {
						push @extraAttribValues, $xnyieldtab->query_by_columnname([$o->[3], $y],$e);
						if ($self->config_entry("EXO_YIELD_EXPECTATIONS")) {
							push @externalyieldexp, $xnyieldexp->query_by_columnname([$o->[3], $y],$e);
						}
					}
				}
				
				if (defined $yield or defined $stoveryield) {
					$xnychead->add_row("","",$o->[3], $yield ,$stoveryield || 0, @extraAttribValues, @externalyieldexp );
					$c++;
				}
				else {
					  print STDERR "Error: No entry in EXTERNAL_CG_YIELDS for link ID ",$o->[3]," for year $y\n";
					  exit(1);
				}
			}
		}
		$self->add_block($xnychead);
		return 1 if ($c > 0);
		return 0;
}
	
