package MpmasSoilToNRU;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use List::Util qw[min max];
use CtUtilities qw(queryhash extqueryhash);
use MpmasInputfile;
use MpmasInputBlock;
our @ISA = qw(MpmasInputfile);

sub filename {
	return "SoilToNRU";
}
sub required {
	my $self = shift;
	return ($self->config_entry("STATICNRU") >= 2);
}
sub prepare_special {
	my ($self) = @_;
	my $compat = MpmasInputBlock->new(3,10);
	$compat->add_comment_row("THE SOILTONRU FILE");
	$compat->add_empty_rows(1);
	$compat->add_comment_row("", "Type of user-defined NRU assignment");
	$compat->add_empty_rows(1);
	
	if ($self->config_entry("STATICNRU") == 2) {
		$compat->add_comment_row("1", "Compatibilty of soil types with NRUs");
		$compat->add_empty_rows(1);
		$compat->add_comment_row("1.1", "NRUs in compatibility groups");
		$compat->add_comment_row("","","NRU ID","GroupID");
		
		print STDERR "\nError: Missing NRU_COMPATIBILITY [MPMAS TABLES].\nThis is required because STATICNRU has been set to 2\n"  and exit(1) unless (defined $self->config_entry("NRU_COMPATIBILITY"));
		my $nrutab = $self->config_entry("NRU_COMPATIBILITY")->transform($self->get_dbh,1);
		
		for my $i (0..$self->config_entry("NNRU") -1) {
			my $n = $nrutab->query([$i],2);
			print STDERR "Error in NRU_COMPATIBILITY, you did not indicate a compatibility group for NRU $i\n" and exit(1) unless (defined $n);
			$compat->add_row("","","",$n);
		}
		$compat->add_empty_rows(2);

		$compat->add_comment_row("1.2", "Soil types in compatibility groups");
			$compat->add_comment_row("","","Soil type ID","GroupID");

		print STDERR "\nError: Missing SOIL_COMPATIBILITY [MPMAS TABLES].\nThis is required because STATICNRU has been set to >=2\n"  and exit(1) unless (defined $self->config_entry("SOIL_COMPATIBILITY"));
		my $soiltab = $self->config_entry("SOIL_COMPATIBILITY")->transform($self->get_dbh,1);
		for my $i (0..$self->config_entry("NSOIL") -1) {
			my $n = $soiltab->query([$i],2);
			print STDERR "Error in SOIL_COMPATIBILITY, you did not indicate a compatibility group for soil type $i\n" and exit(1) unless (defined $n);
			$compat->add_row("","",$i,$n);
		}		
		$self->add_block($compat);
		
		
			my $similhead = MpmasInputBlock->new(3,20);

		$similhead->add_comment_row("2", "Grouping of cells into NRUs");
		$similhead->add_empty_rows(1);
				

		print STDERR "\nError: Missing SOIL_SIMILARITY [MPMAS TABLES].\nThis is required because STATICNRU has been set to >=2\n"  and exit(1) unless (defined $self->config_entry("SOIL_SIMILARITY"));
		my $soilsim = $self->config_entry("SOIL_SIMILARITY")->transform($self->get_dbh,2);
		my $simil = MpmasInputBlock->new(3,30);

		my $maxlevel = 0;
		my $tab;
		for my $i (0..$self->config_entry("NSOIL") -1) {
			 my $m = max( $soilsim->query([$i],0));
			 $maxlevel = $m if ($m > $maxlevel);
		}
		for my $i (0..$self->config_entry("NSOIL") -1) {
			my @row = ("","",$i);
			my $previous = -$i; 
			for my $j (0..$maxlevel  ) {
				my $x = $soilsim->query([$i, $j],3);
				$x = $previous unless (defined $x);
				push @row , $x;
				$previous = $x;
			}
			$simil->add_row(@row);
		}
		my  @levelheader = map { "Level $_"} (0..$maxlevel);
		$similhead->add_row("","","Number of Levels",$maxlevel +1);

		$similhead->add_comment_row("","","Soil type ID",@levelheader);
		$self->add_block($simil);
		$self->add_block($similhead);
	}
	elsif ($self->config_entry("STATICNRU") == 3  or $self->config_entry("STATICNRU") == 4 ) {
		$compat->add_comment_row("1", "NRU definitions");
		$compat->add_empty_rows(1);
		
		$compat->add_comment_row("","","", "NRU ID", "SoilID", "Ubound. Distance", ($self->config_entry("STATICNRU") == 4 ? "Ubound parcel size": undef )  );

		print STDERR "\nError: Missing SOIL_DISTANCE [MPMAS TABLES].\nThis is required because STATICNRU has been set to 3 or 4\n"  and exit(1) unless (defined $self->config_entry("SOIL_DISTANCE"));
		my $soiltab = $self->config_entry("SOIL_DISTANCE")->transform($self->get_dbh,1);
		
		for my $i (0..$self->config_entry("NNRU") -1) {
			my $soil = $soiltab ->query([$i],2);
			print STDERR "\nError: Missing soilID entry for NRU $i in SOIL_DISTANCE [MPMAS TABLES]\n" unless (defined $soil);
			my $distance = $soiltab ->query([$i],3);
			print STDERR "\nError: Missing distance entry for NRU $i in SOIL_DISTANCE [MPMAS TABLES]\n" unless (defined $distance);
			my $size;
			if ($self->config_entry("STATICNRU") == 4)	{	
					$size = $soiltab ->query([$i],4);
					print STDERR "\nError: Missing distance parcel size entry for NRU $i in SOIL_DISTANCE [MPMAS TABLES] (STATICNRU == 4)\n" unless (defined $size);
			}
			$compat->add_row("","","",$i, $soil, $distance, $size);
			
		}
		$self->add_block($compat);		
	}
	elsif ($self->config_entry("STATICNRU") == 5 ) {
		$compat->add_comment_row("1", "NRU definitions");
		$compat->add_empty_rows(1);
		$compat->add_row("","","User-defined categories", $self->config_entry("NUDEF_FOR_NRU_DEFINITION"));
		$compat->add_comment_row("","","", "NRU ID", "SoilID", "Ubound. Distance", "Ubound.Direction",  "Ubound.user-defined");

		print STDERR "\nError: Missing SOIL_DISTANCE_UDEF [MPMAS TABLES].\nThis is required because STATICNRU has been set to 5\n"  and exit(1) unless (defined $self->config_entry("SOIL_DISTANCE_UDEF"));
		my $soiltab = $self->config_entry("SOIL_DISTANCE_UDEF")->transform($self->get_dbh,1);
		
		for my $i (0..$self->config_entry("NNRU") -1) {
			my $soil = $soiltab ->query([$i],2);
			print STDERR "\nError: Missing soilID entry for NRU $i in SOIL_DISTANCE_UDEF [MPMAS TABLES]\n" unless (defined $soil);
			my $distance = $soiltab ->query([$i],3);
			print STDERR "\nError: Missing distance entry for NRU $i in SOIL_DISTANCE_UDEF [MPMAS TABLES]\n" unless (defined $distance);
			my $direction;
			$direction = $soiltab ->query([$i],4);
			print STDERR "\nError: Missing direction  entry for NRU $i in SOIL_DISTANCE_UDEF [MPMAS TABLES] (STATICNRU == 5)\n" unless (defined $direction);
			my @udefs;
			for my $j (0..$self->config_entry("NUDEF_FOR_NRU_DEFINITION") -1) {

				
				my $temp = $soiltab->query([$i],5 + $j);
				print STDERR "\nError: Missing user-defined column No. $j for NRU $i in SOIL_DISTANCE_UDEF [MPMAS TABLES] (STATICNRU == 5, NUDEF_FOR_NRU_DEFINITION == ". $self->config_entry("NUDEF_FOR_NRU_DEFINITION") . ")\n" unless (defined $temp);				
				push @udefs, $temp;
			}
			$compat->add_row("","","",$i, $soil, $distance, $direction, @udefs);
			
		}
		$self->add_block($compat);		
	}
}
1;
