#!/usr/bin/perl

use utf8;
use strict;
use warnings;
use Encode qw(encode_utf8);

if ($#ARGV !=3) {
	print "usage: po_insert_translations.pl language translation_file OBO_file output_file\n";
	exit;
}

my $lang = $ARGV[0];
my $trans_file = $ARGV[1];
my $obo_file = $ARGV[2];
my $out_file = $ARGV[3];

my $line_end;
if($lang eq "SP") {
	$line_end = "(Spanish)\" EXACT Spanish [POC:Maria_Alejandra_Gandolfo]";
}elsif($lang eq "JP") {
	$line_end = "(Japanese)\" EXACT Japanese [NIG:Yukiko_Yamazaki]";
}else{
		print "Please choose either SP (for Spanish) or JP (for Japanese)";
		exit;
}

my %lang_hash;

open(language_File,"$trans_file");
while(<language_File>) {
	my $line = $_;
	chomp $line;
	my ($term, $name, $translation, $defn) = split(/\t/, $line);
	if ($translation eq "") {
			next;
	}
	$translation =~ s/\"//g;
	if(defined($lang_hash{$term})) {
		$lang_hash{$term} = "$lang_hash{$term}\t$translation";
	}else{
		$lang_hash{$term} = $translation;
	}
}

# need to make sure that the translations are in alphabetical order
foreach my $key (keys %lang_hash) {
	my @translations = split("\t", $lang_hash{$key});
	my @sorted_translations = sort {lc($a) cmp lc($b)} @translations;
	my $length = @sorted_translations;
	$lang_hash{$key} = "$sorted_translations[0]";
	for (my $i=1; $i<$length; $i++) {
		$lang_hash{$key} = "$lang_hash{$key}\t$sorted_translations[$i]";
	}
}
	

open(in_File,"$obo_file");
open(output_File,">$out_file");

my $line_prev = "";
my $po_id;
my $found_synonyms =0;

my @before_ids = ("id", "is_anonymous", "name", "namespace", "alt_id", "def", "comment", "subset");
my @after_ids = ("xref", "is_a", "intersection_of", "union_of", "disjoint_from", "relationship", "is_obsolete", "replaced_by", "consider");

while(<in_File>) {
	my $line_curr = $_;
	chomp $line_curr;
	
	if($line_curr =~ m/^\[Term\]/) {
		$found_synonyms = 0;
		print output_File "$line_curr\n";
		next;
		
	}
	
	if($line_curr =~ m/^&/) {
		print output_File "$line_curr\n";
		next;
	}
	
	if($line_curr =~ m/^id:\s+(PO:\d+)/) {
		$po_id = $1;
	}
	
	if ($line_curr =~ m/^synonym:/) {
		$found_synonyms = 1;
		if(defined($lang_hash{$po_id})) {
			foreach my $new_synonym(split("\t",$lang_hash{$po_id})) {
				my $new_synonym_line = "synonym: \"$new_synonym $line_end";
				if (lc($new_synonym_line) lt lc($line_curr) && lc($new_synonym_line) gt lc($line_prev)) {  # need to lowercase the string otherwise the ascii string compare will not work right
					print output_File "$new_synonym_line\n";
				}elsif( $new_synonym_line eq $line_curr) {
					# translation already in file
					next;
				}
			}
		}else{
			#print "po id not found\t$po_id\n";
		}
	}
	

	
	my $line_curr_identifier = "no match";
	$line_curr =~ m/^(\w+)\:/;
	if(defined($1)) {
		$line_curr_identifier = $1;
	}
	
	my $line_prev_identifier = "no match";
	$line_prev =~ m/^(\w+)\:/;
	if(defined($1)) {
		$line_prev_identifier = $1;
	}
	
	my $count_before = grep /^$line_prev_identifier$/, @before_ids;
	my $count_after = grep /^$line_curr_identifier$/, @after_ids;
	
	if($count_before && $count_after) {
		if(defined($lang_hash{$po_id})) {
			if(!$found_synonyms) {
				foreach my $new_synonym(split("\t",$lang_hash{$po_id})) {
					my $new_synonym_line = "synonym: \"$new_synonym $line_end";
					print output_File "$new_synonym_line\n";
				}
			}
		}else{
			#print "po id not found\t$po_id\n";
		}
	}

	# put in section to deal with Japanese chars.
	#  Japanese characters are utf-8, and should come after the ascii chars of spanish
	if ($line_prev =~ m/^synonym:/ && $count_after && $lang eq "JP") {
		if(defined($lang_hash{$po_id})) {
			foreach my $new_synonym(split("\t",$lang_hash{$po_id})) {
				my $new_synonym_line = "synonym: \"$new_synonym $line_end";
					print output_File "$new_synonym_line\n";
			}
		}
	}
	
	
	print output_File "$line_curr\n";
	$line_prev=$line_curr;
	
}

