package ILMT::URD::HIN::Morph::adj_gen;

use Exporter qw(import);
use ILMT::URD::HIN::SSFAPI::feature_filter;
use ILMT::URD::HIN::SSFAPI::shakti_tree_api;

our @EXPORT = qw(adj_gen);

sub adj_gen {
    my ($input, $output) = @_;

    open ($infile, '<', $input) or die "$!";
    open ($outfile, '>', $output) or die "$!";

    while($line=<$infile>)
    {
        chomp ($line);
        ($addr, $tkn, $pos, $fs) = split(/\t/,$line);
        if($fs ne "")
        {
            @fss = split(/\|/, $fs);
            my $len = @fss;
            @string  = "";
            $newfs = "";
            my $i=0;
            foreach $af (@fss)
            {
                my $FSreference = &read_FS($af, $line);
                my @lex_val = &get_values("lex", $FSreference);
                my @cat_val = &get_values("cat", $FSreference);
                my @gen_val = &get_values("gen", $FSreference);
                my @num_val = &get_values("num", $FSreference);
                #print "lex:$lex cat:$cat gen:$gen num:$num\n";

                if ($lex_val[0] =~ /(\.)*I$/ and $cat_val[0] eq "adj" and $gen_val[0] eq "f" and $num_val[0] eq "any")
                {
                    $lex = $lex_val[0];
                    $lex =~ s/I$/A/;
                    #print "my lex $lex";
                    my @lex_arr=();
                    push @lex_arr,$lex;
                    &update_attr_val("lex", \@lex_arr, $FSreference, $af);
                    $string[$i] = &make_string($FSreference, $af);
                }
                elsif ($lex_val[0] =~ /(\.)*(IM|Iz)$/ and $cat_val[0] eq "adj" and $gen_val[0] eq "f" and $num_val[0] eq "any")
                {
                    $lex = $lex_val[0];
                    $lex =~ s/(IM|Iz)$/Az/;
                    #print "my lex $lex";
                    my @lex_arr=();
                    push @lex_arr,$lex;
                    &update_attr_val("lex", \@lex_arr, $FSreference, $af);
                    $string[$i] = &make_string($FSreference, $af);
                }
                else
                {
                    $lex = $lex_val[0];
                    my @lex_arr=();
                    push @lex_arr,$lex;
                    &update_attr_val("lex", \@lex_arr, $FSreference, $af);
                    $string[$i] = &make_string($FSreference, $af);

                }


                $i++;
            }
            foreach $string (@string)
            {
                if(--$len)
                {
                    $newfs=$newfs.$string."|";
                }
                else
                {
                    $newfs=$newfs.$string;
                }
            }
            delete @string[0..$#string];
            delete @lex_root[0..$#lex_root];
            delete @fss[0..$#fss];
            if($line =~ /\(\(/ or $line =~ /\)\)/)
            {
                ($addr1,$lex,$pos,$fs) = split(/\t/,$line);
                if ($output ne "")
                {
                    #print $outfile $num,"\t",$lex,"\t",$pos,"\t",$newfs,"\n";
                    print $outfile "$addr1\t$lex\t$pos\t$newfs\n";
                }
                else
                {
                    print $addr1,"\t",$lex,"\t",$pos,"\t",$newfs,"\n";
                }
            }
            else
            {
                if ($output ne "")
                {
                    print $outfile $addr,"\t",$tkn,"\t",$pos,"\t",$newfs,"\n";
                }
                else
                {
                    print $addr,"\t",$tkn,"\t",$pos,"\t",$newfs,"\n";
                }
            }
        } # end if fs ne ""
        else {  # try to understand this else block

            if($lex ne "((" and $lex ne "))")
            {
                if ($output ne "")
                {
                    print $outfile $addr,"\t",$tkn,"\t",$pos,"\t",$fs,"\n";
                }
                else
                {
                    print $addr,"\t",$tkn,"\t",$pos,"\t",$fs,"\n";
                }
            }
            else {
                if ($output ne "")
                {
                    print $outfile $line."\n";
                }
                else
                {
                    print $line."\n";
                }
            }
        }


    } # end while loop
    close($infile);
    close($outfile);
}
