package MpmasTimehandler;

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

sub filename {
	return "TimeHandler";
}
sub prepare_shared {
    my $self = shift;
    my $start = $self->config_entry("SEASONSTART");
    my $end =  $self->config_entry("SEASONEND");
    croak ("Misspecified season: SEASONSTART and SEASONEND have to be in (1..12).\n") unless ( 1<= $end and $end <=12 and  1<= $start and $start <=12);
    croak ("Please specify STARTYEAR and ENDYEAR.\n") unless ( defined  $self->config_entry("STARTYEAR") and defined  $self->config_entry("ENDYEAR"));
    
    my $irrigmonths;
    my @sortedirrigmonths;
    if ($end < $start) {
		$irrigmonths = $end - $start + 13;
		@sortedirrigmonths = ($start..12,1..$end);
    }
    else {
		$irrigmonths = $end - $start + 1;
		@sortedirrigmonths = ($start..$end);
    }
    my $simp;
    
	if($self->config_entry("MULTIPERIOD") and $self->config_entry("PERIODS_PER_YEAR") != 1) {

		if (12 % $self->config_entry("PERIODS_PER_YEAR")  != 0  or $self->config_entry("PERIODS_PER_YEAR") < 1) {
			print STDERR "Error: Invalid PERIODS_PER_YEAR. Valid values: 1,2,3,4,6,12.\n";
			exit(1);
		}

		my $periodLengthInMonths = 12 / $self->config_entry("PERIODS_PER_YEAR");

    	if ($self->config_entry("ENDMONTH") >= $self->config_entry("STARTMONTH"))
		{
			$simp = ($self->config_entry("ENDMONTH") - $self->config_entry("STARTMONTH")) / $periodLengthInMonths;
			$simp += ($self->config_entry("ENDYEAR") - $self->config_entry("STARTYEAR")) * $self->config_entry("PERIODS_PER_YEAR") ;
			$self->set_config_entry("YEAROFFIRSTHARVEST" , $self->config_entry("STARTYEAR") + 1);

		}
		else
		{
			$simp = ( (12 - $self->config_entry("STARTMONTH") + 1) +  $self->config_entry("ENDMONTH"))  
					+ ($self->config_entry("ENDYEAR") - $self->config_entry("STARTYEAR") - 1) * 12   ;
			$simp /=   $periodLengthInMonths;
			$self->set_config_entry("YEAROFFIRSTHARVEST" , $self->config_entry("STARTYEAR") );

		}
		
    }
    else {
		if ($self->config_entry("STARTMONTH") > $self->config_entry("ENDMONTH") ){
			$simp = $self->config_entry("ENDYEAR") - $self->config_entry("STARTYEAR");
			$self->set_config_entry("YEAROFFIRSTHARVEST" , $self->config_entry("STARTYEAR") + 1);
			
			
		}
		else {
			$simp = $self->config_entry("ENDYEAR") - $self->config_entry("STARTYEAR") + 1;
			$self->set_config_entry("YEAROFFIRSTHARVEST" , $self->config_entry("STARTYEAR") );
		}
	}
    $self->set_config_entry("NYEARS",$simp);

    $self->set_config_entry("IRRIGMONTH", $irrigmonths); 
    $self->set_config_entry("SORTEDIRRIGMONTHS", \@sortedirrigmonths); 
}
sub prepare_special {
	my $self = shift;	
	my $block1 = MpmasInputBlock->new(2,10);
	
	$block1->add_row("THE TIME HANDLER FILE");
	$block1->add_row("1","Specification of model time ");	
	$block1->add_row("","Start of model (Year, month, day)", $self->config_entry("STARTYEAR"), $self->config_entry("STARTMONTH"), $self->config_entry("STARTDAY") );
	$block1->add_row("","End of model time (Year, month, day)", $self->config_entry("ENDYEAR"), $self->config_entry("ENDMONTH"), $self->config_entry("ENDDAY"));
	my $mp = $self->config_entry("NYEARS");
	$block1->add_row("", "Number of model periods", "$mp");
	
	$block1->add_row("");
	$block1->add_row("2","Specification of irrigation time (MPMAS)");
	$block1->add_row("", "First month of cropping season",$self->config_entry("SEASONSTART") );
	$block1->add_row("", "Last month of cropping season", $self->config_entry("SEASONEND"));
	$block1->add_row("", "Number of Irrigation months", $self->config_entry("IRRIGMONTH") );
	$block1->add_row("","Northern Hemisphere?", $self->config_entry("NORTHERN"));
	
	if($self->config_entry("MULTIPERIOD")) {
		$block1->add_row("");
		$block1->add_row("","Simulation periods per year", $self->config_entry("PERIODS_PER_YEAR"));	
		$block1->add_row("","Cyclic count of simulation period at simulation start", $self->config_entry("SEASON_OF_START_PERIOD"));	

	}
	$self->add_block($block1);
}

1;
