#!/usr/bin/perl

use SFNParse;
use SFNFile;
use strict;


my $getcode;

my $filename = shift || "figures.sfn";
my $basefile = ;
my @figures = read_sfnfile("basics.spl");
push @figures, read_sfnfile($filename);
my @figures;
my @name_list;
my $global_header;
my $def_origin = '?';
my $def_string = '8';

foreach my $f (@temp) {
    if(lc($f->{name}) =~ /global/) {
	$def_origin = $f->{origin};
	$def_string = $f->{string};
	$global_header = $f;	# Global header
    } elsif ($f->{name} =~ /^$/) {
	push @basics,$f;	# Un-named code fragment
    } else {
	push @name_list,$f->{name};
	push @figures,$f;
    }
}

push(@figures,@basics);

display_global_header($global_header);

my $parser = new SFNParse;

while (my $fig = choose_name(\@name_list)) {
    my $followdepth = 0;	# start each figure at zero depth
    display_figure($figures[$fig-1],$followdepth);
    while (my $opt = input("\nDepth: [$followdepth] (+ - . !) = (inc dec zero full)")) {
# try to take care of the poor bozo (me) who forgets to shift for plus
# We'll change this to just get a single key at some point.
    if ($opt =~ /[+=]/) { $followdepth++; } elsif
    ($opt =~ /-/) { $followdepth = 0 unless (--$followdepth > 0);} elsif
    ($opt =~ /\./) { $followdepth = 0; } elsif
    ($opt =~ /!/) { $followdepth = 1000; }
    display_figure($figures[$fig-1],$followdepth);
    }
}
exit;


#############################################################################

sub display_figure {
    my $f = shift;
    my $d = shift;
    display_header($f);
    display_steps($f, $d, 0);
}

sub choose_name {
    my $aref = shift;
    my $i = 0;
foreach my $n (@$aref){
    printf "[%2d] %s",$i+1,$n;
    my $sep = (++$i % 4) ? ", " : "\n";
    print $sep;
    }
    print "\n";
    return input("Select a figure");
}

sub input {
    my $prompt = shift;
    print "$prompt:";
    my $line = <STDIN>;
    chomp $line;
    return $line;
}

sub get_figure_from_code {
	my $code = shift;
	foreach(@figures) {
		next unless ($_->{code} eq $code);
		return $_;
	}
	return ();
}

sub get_figures_from_name {
	my $name = shift;
	my @f;
	foreach(@figures) {
		next unless ($_->{name} =~ /$name/i);
		push @f,$_;
	}
	@f;
}

sub display_header {
	my $f = shift;
	$f->{string} = $def_string unless $f->{string};
	$f->{origin} = $def_origin unless $f->{origin};
	print "Name: $f->{name} Origin: $f->{origin}  String: $f->{string} Ref: $f->{ref}\n\n";
}

sub display_global_header {
    my $f = shift;
    print "These figures have the origin: $f->{origin}.\n";
    print "They were transcribed by $f->{author} ($f->{email})\n";
    print "Refer to $f->{ref} for further information.\n\n";
}

sub display_steps {
	my ($f, $follow, $parentstep) = @_;
	my @steps = @{$f->{steps}};
	my $step_offset = 0;
	my $stepnum = 1;
	my $alts = 0;
	my $in_altblock;
	my $endalt;
	my $startalt = 0;
	my $maxalt = 0;
	my $inalt;
	foreach (@steps) {
		my $text = $parser->stepline($_) || "Bad text!";
		$text =~ s/^\s+//;
		if ($text =~ /^\"/) { # a 'voice' command
			print "$text\n";
			next;
		}

		if ($text =~ s/\{//) {
			if (++$alts > 1) {
				print "( Alternatively , \n";
				$stepnum = $startalt;
			}
			$in_altblock = 1;
			$startalt = $stepnum unless $startalt;
		}
		elsif(!$in_altblock) {		#start of non-altblock line
			$alts = 0;
		}

		if ($in_altblock && $alts > 1) {$inalt = "  ";}

		if ($text =~ s/\}//) {
			$in_altblock = 0;
			$maxalt = $stepnum if $stepnum > $maxalt;
			if ($alts > 1) {
				$endalt = "\n)";
			}
		}
		else {
			$stepnum  = $maxalt+1 if ($maxalt > $stepnum && !$in_altblock);
		}

		$text =~ s/\s+/ /g;
		$text =~ s/\s+$//;
		$text = ucfirst($text);
		$text =~ s/^Repeat steps (\d+) to (\d+)/"Repeat steps ".($1 + $step_offset + $parentstep)." to ".($2 + $step_offset + $parentstep)/e;
		if ($text =~ /Perform (\w+)/ && $follow) {
			my $f1 = get_figure_from_code($1);
			if ($f1) {$step_offset += display_steps($f1, $follow-1, $stepnum-1 + $step_offset);}
			else {print $stepnum, " ", $text, ". [Code not found]\n";}
		}
		else {
			my $step = $stepnum+ $step_offset + $parentstep." ";
			print $inalt, $step, $text, ".",$endalt,"\n";
			$endalt="";
			$inalt = "";
			$stepnum++;
		}
	}
	return scalar(@steps) + $step_offset;
}

