#!/usr/bin/perl -w

my $simFile = $ARGV[0];
my $nw1PajekFile = $ARGV[1];
my $nw2PajekFile = $ARGV[2];
my $nw1nw2simPajek = $ARGV[3];
my $threshold = $ARGV[4];

my @sim = &readFile($simFile);

@common_nw_labels = ();
@all_nw1_labels = ();
@all_nw2_labels = ();
@tmp_nw1_common_nodes = ();
@tmp_nw2_common_nodes = ();

&parse_sim_file(@sim);
#print "Common Graph Nodes: @common_nw_labels\n";
#print "All NW-1 Nodes    : @all_nw1_labels\n";
#print "All NW-2 Nodes    : @all_nw2_labels\n";
print "Common Nodes from NW-1: @tmp_nw1_common_nodes\n";
print "Common Nodes from NW-2: @tmp_nw2_common_nodes\n";

print "\n";
$ctr = 0;
$hash_nodes = ();
foreach(@tmp_nw1_common_nodes) {
    if (exists $hash_nodes{$_}) {
	$str = "";
	$str = $hash_nodes{$_};
	$str .= "  $tmp_nw2_common_nodes[$ctr]";
	$hash_nodes{$_} = $str;
    }else {
	$hash_nodes{$_} = $tmp_nw2_common_nodes[$ctr];
    }
    $ctr++;
}

@nw1_pajek = readFile($nw1PajekFile);
$flag = 0;
$tmpLine = shift(@nw1_pajek);
($waste,$length)=split(" ",$tmpLine);

foreach(@nw1_pajek) {
    chomp ($_);
    if ($_ ne "*Arcs") {
	if(!$flag) {
	    print ">$_\n";
	    @tmpString=(); 
	    @tmpString = split(" ", $_);
	    $nodes1{$tmpString[0]}=$tmpString[1];
	} else 	{
	    @tmpArcs=(); 
	    @tmpArcs=split(" ", $_);
	    if(exists ($arcs1{$tmpArcs[0]})){
		$tmpStringnew = $arcs1{$tmpArcs[0]};
		$tmpStringnew .= " $tmpArcs[1]";
		$arcs1{$tmpArcs[0]} = $tmpStringnew;
		$tmpStringnew="";
	    } else  {
		$arcs1{$tmpArcs[0]} = $tmpArcs[1];
	    }
	}
    } 
    elsif($_ eq "*Arcs") {
	$flag=1;
    }
}

@nw2_pajek = readFile($nw2PajekFile);
shift(@nw2_pajek);
$flag=0;
foreach(@nw2_pajek) {
    chomp ($_);
    if ($_ ne "*Arcs") {
	if(!$flag) {
	    @tmpString=(); 
	    @tmpString = split(" ", $_);
	    print "> # $tmpString[0] -- $tmpString[1]\n";
	    $tmpString[0] = $tmpString[0]+$length;
	    $nodes2{$tmpString[0]}=$tmpString[1];
	    print "> # $tmpString[0] -- $tmpString[1]\n";
	}else {
	    @tmpArcs=(); 
	    @tmpArcs=split(" ", $_);
	    $tmpArcs[0] = $tmpArcs[0]+$length;
	    $tmpArcs[1] = $tmpArcs[1]+$length;
	    
	    if(exists ($arcs2{$tmpArcs[0]})){
		$tmpStringnew = $arcs2{$tmpArcs[0]};
		$tmpStringnew .= " ".$tmpArcs[1];
		$arcs2{$tmpArcs[0]} = $tmpStringnew;
		    $tmpStringnew="";
	    } else	{
		$arcs2{$tmpArcs[0]} = $tmpArcs[1];
	    }
	}
    } elsif($_ eq "*Arcs") {
	$flag=1;
    }
}

print " NW-1 Arcs\n";
foreach(keys %arcs1) {
    print "==> $_ == $arcs1{$_}\n";
    @nodesIn2 =();
    @nodesIn2 = split(" ",$arcs1{$_});
    foreach $n2 (@nodesIn2)  {
	$results = "$_ $n2 1 c Green";
	push(@arcs, $results);
    }
}

print " NW-2 Arcs\n";
foreach(keys %arcs2) {
    print "==> $_ == $arcs2{$_}\n";
    @nodesIn2 =();
    @nodesIn2 = split(" ",$arcs2{$_});
    foreach $n2 (@nodesIn2)  {
	$results = "$_ $n2 1 c Red";
	push(@arcs, $results);
    }
}

print " NW-1-2 Arcs\n";
foreach(keys %hash_nodes) {
    print "==> $_ == $hash_nodes{$_}\n";
    @nodesIn2 =();
    @nodesIn2 = split(" ",$hash_nodes{$_});
    foreach $n2 (@nodesIn2)  {
	$newIndex = $n2  + $length;
	$result = "$_ $newIndex 1 c Black";
	push(@arcs, $result);
    }
}


open (OUT, ">$nw1nw2simPajek");
@tmp1 = (keys %nodes1);
$l_tmp1 = @tmp1;
print ">> $l_tmp1\n";

@tmp2 = (keys %nodes2);
$l_tmp2 = @tmp2;
print ">> $l_tmp2\n";

$len = $l_tmp1 + $l_tmp2;
print ">> $len\n";

print OUT"*Vertices $len\n";
for($i = 1; $i <= $l_tmp1; $i++) {
    print OUT"$i $nodes1{$i} ic Green bc Green\n";
}

for($i = 1; $i <= $l_tmp2; $i++) {
    $ctr = $i + $l_tmp1;
    print OUT"$ctr $nodes2{$ctr} ic Orange bc Orange\n";
}

print OUT"*Edges\n";
foreach(@arcs) {
    print OUT "$_\n";
}
close(OUT);


#########Subroutine ###################
sub readFile() {
    my $file = $_[0];
    
    @data = ();
    open( IN, $file );
    @data = <IN>;
    close(IN);
    return (@data);
}

sub parse_sim_file() {
    my (@sim_data) = @_;
    
    $rest = "";
    foreach(@sim_data) {
	chomp($_);
	$_=~s/\s$//;
#	print "> $_\n";
	($n1, $n2, $rest) = split(",", $_);
	($n2, $sim_val) = split(":", $n2);
	$n1 =~s/\[//;
	$n2 =~s/://;
	$sim_val =~s/^\s+//;
	$sim_val =~s/\s$//;
	$sim_val =~s/sim=//;	
#	print ">$n1 : $n2 : $sim_val\n";
	
	# Get common nodes having sim val = 1
	if($sim_val >= $threshold) {
	    $common_nw_label = "[$n1 , $n2]";
	    push(@common_nw_labels, $common_nw_label);
#	    if (!$tmp_nw1_common_nodes{$n1}++) {
		push(@tmp_nw1_common_nodes, $n1);
#	    }
#	    if(!$tmp_nw2_common_nodes{$n2}++) {
		push(@tmp_nw2_common_nodes, $n2);
#	    }
	} 
	
	# get all the nw1 and nw2 labels b4 removing common nodes
	if(!$hash_n1{$n1}++) {
	    push(@all_nw1_labels, $n1);
	}

	if(!$hash_n2{$n2}++) {
	    push(@all_nw2_labels, $n2);
	}
    }
}


sub find_non_common_nodes() {
    my @one_nw_nodes = @_;
#    print "### @one_nw_nodes\n";
    
    %count = ();
    foreach $e (@one_nw_nodes){
	$count{$e}++;
    }
    
    @diff = ();
    foreach(keys %count) {
#	print "$_ : $count{$_}\n";
	if ($count{$_} != 2) {
	    push(@diff, $_);
	}
    }
    return(@diff);
}


