package MpmasInputBlockWithValueReplace;

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

sub set_cur_valuehash {
    $_[0]->{cur_valuehash} = $_[1];
}

sub cur_valuehash {
   return $_[0]->{cur_valuehash} if (exists $_[0]->{cur_valuehash});
}
sub set_cur {
      $_[0]->{cur} = $_[1];
}
sub cur {
  return $_[0]->{cur};
}
sub write {
	my ($self, $out , $commentmode, $leftmargin,$cur,$valuetab) = @_;
	$self->set_cur_valuehash($valuetab) if (defined $valuetab );
	$self->set_cur($cur);
	my $i = -1;
	for my $r ( @{ $self->{Rows} } ) {
		$i++;
		print $out "\n" and next if ( not $commentmode and $r->{type} == 1 );
		next if ( not $commentmode and $r->{type} == 2 );
		print $out "\n" and next if ($commentmode and $r->{type} == 3 );
		next if ($commentmode and $r->{type} == 4 );
		
		my $string = $leftmargin;
		for my $c (0..$#{$r->{row}}){	
			if (defined $r->{row}->[$c] and ( $c >= $self->{commentary_columns} or $commentmode) ) {
				my $s = $r->{row}->[$c];
				if ( $s =~  s/&get_value\(/\$self->get_value(/) {
					my $x = 0;
					#print $r->[$c]."\n";
					eval( "\$x = ". $s) ; 
					print $@ if $@;
					#print $x."\n";
					$string .= sprintf("%.4e", $x);
				}
				else {
					$string .= $r->{row}->[$c];
				}
			}
			$string .= "\t";
		}
		print $out $string."\n";
	}
	return 1;
}

sub get_value {
      return $_[0]->cur_valuehash->query([$_[0]->cur, $_[1]],3) if (ref $_[0]->cur_valuehash and defined  $_[0]->cur_valuehash->query([$_[0]->cur, $_[1]],3));
      return 0;
}
1;
