package Common;

#interface
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&find_longest_substring &my_wait &print_code_words &translate &no_silent &endl &print_dict_tree &print_dict_list &tree_view &list_view &translate_mask &read_key &min);

use strict;

our $WAITING_MODE = 1;
our $NO_WAITING_MODE = 0;

our $NO_SILENT_MODE = 2;
our $SILENT_MODE = 0;

our $TREE_VIEW = 4; 
our $NO_TREE_VIEW = 0; 

our $LIST_VIEW = 8;
our $NO_LIST_VIEW = 0; 


#implementation

use Term::ReadKey;

###############################################################################
sub min
{
	my $a = shift;
	my $b = shift;
	return $a < $b ? $a : $b;
}

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

sub translate_mask
{
# in:
# mask
# out:
# mask description
	my $mask = shift;
	my $ret = "";
	$ret .= (($mask & $WAITING_MODE) == $WAITING_MODE) ? "waiting " : "no_waiting ";
	$ret .= (($mask & $NO_SILENT_MODE) == $NO_SILENT_MODE) ? "no_silent " : "silent ";
	$ret .= (($mask & $TREE_VIEW) == $TREE_VIEW) ? "print_tree " : "";
	$ret .= (($mask & $LIST_VIEW) == $LIST_VIEW) ? "print_list " : "";
}

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

sub endl
{
	print "\n";
}

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

sub print_code_words
{
# in:
# encoded (ref array)

	my $ref_code_words = shift;
	foreach my $a (@$ref_code_words) {
		print translate($a) . " ";
	}
	print "\n";
}

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

sub translate
{
# in:
# code_word - internal format (scalar)
# out:
# formatted code_word

	my $code_word = shift;
	$code_word =~ s/_/, /g;
	return "($code_word)";
}

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

sub tree_view
{
# in:
# mode mask (scalar)
# out:
# tree_view (bool)

	my $mode = shift;
	return (($mode & $TREE_VIEW) == $TREE_VIEW);
}

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

sub list_view
{
# in:
# mode mask (scalar)
# out:
# list_view (bool)

	my $mode = shift;
	return (($mode & $LIST_VIEW) == $LIST_VIEW);
}

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

sub no_silent
{
# in:
# mode mask (scalar)
# out:
# silent_mode (bool)

	my $mode = shift;
	return (($mode & $NO_SILENT_MODE) == $NO_SILENT_MODE);
}

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

sub my_wait
{
# in:
# mode mask (scalar)

	my $mode = shift;
	if (no_silent($mode) && ($mode & $WAITING_MODE) == $WAITING_MODE) {
		my $key = read_key();
		return $key;
	}
	return undef;
}

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

sub read_key
{
	ReadMode("cbreak");
	my $key = ReadKey(0);
	ReadMode("normal");
	$| = 1; # nastaveni flush
	return $key;
}

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

sub print_dict_tree
{
# in:
# dictionary (ref array)

# struct of dictionary:
# node consists of two items: number of node and hash (keys: letters, values: nodes)

	my ($ref_dict) = shift @_;

	my @lifo = ();
	my $depth = 0;
	my $key = "";
	push @lifo, $ref_dict, $depth, $key;
 	my $node;
	while ($#lifo != -1) {
		$key = pop @lifo;
		$depth = pop @lifo;
		$node = pop @lifo;
		print "    " x $depth . "'$key'  $$node[0]\n";
		foreach my $a (reverse sort keys %{$$node[1]}) { 
			push @lifo, $$node[1]{$a}, $depth+1, $a;
		}
	}
}

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

sub print_dict_list
{
# in:
# dictionary (ref array)

# struct of dictionary:
# node consists of two items: number of node and hash (keys: letters, values: nodes)

	my ($ref_dict) = shift @_;

	my %out = ();
	my @fifo = ();
 	my $prefix = "";
	push @fifo, $ref_dict, $prefix;
 	my $node;
	while ($#fifo != -1) {
		$node = shift @fifo;
		$prefix = shift @fifo;
		while (my ($key, $value) = each( %{$$node[1]} )) {
			push @fifo, $value, $prefix . $key;
		}
# 		next if $$node[0] == -1;
		$out{$$node[0]} = $prefix;
	}
	foreach my $c (sort {$a <=> $b} keys %out) {
		printf "%2d .. $out{$c}\n", $c if $out{$c};
	}
}

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

sub find_longest_substring
{
# special function for LZ77 and LZSS coding
# in:
# pattern (scalar)
# string (scalar)
# out:
# position from end of string
# length of found sunbstring

	my ($p_co, $p_kde) = @_;
	if ($p_kde eq "") {
		return (0,0);
	}
	my $len_co = length($p_co);
	my $len_kde = length($p_kde);

	my $start_position = $len_kde-1;
	$p_kde .= $p_co;

	my $pos = rindex($p_kde, $p_co, $start_position);
	while($pos == -1) {
		chop $p_co;
		last if $p_co eq "";
		$pos = rindex($p_kde, $p_co, $start_position);
	}
	if ($pos == -1 || $pos >= $len_kde) {
		return (0,0);
	}
	my $zpet = $len_kde - $pos;
	my $kolik = length($p_co);
	return ($zpet, $kolik);
}


1;