package ILMT::URD::HIN::WX2UTF_U;
#use strict;
#use warnings;
use Dir::Self;
use Data::Dumper;
use ILMT::URD::HIN::WX2UTF_U::IndicCC;
use ILMT::URD::HIN::SSFAPI::feature_filter;
use ILMT::URD::HIN::SSFAPI::shakti_tree_api;

my @dispatch_seq = (
    "utf2wx_u"
);

sub process {
    my %args = @_;
    foreach my $submodule (@dispatch_seq) {
        utf8::encode($args{"data"});
        $args{'data'} = __PACKAGE__->can($submodule)->(%args);
        utf8::decode($args{"data"});
    }
    return $args{"data"};
}

sub utf2wx_u {
    my %args = @_;
    $args{'lang'} = $args{'src_lang'};
    $args{'src'} = 'wx';
    $args{'tgt'} = 'utf';
    $args{'type'} = 'ssf';
    return convertor_urd(%args);
}

sub convertor_urd {
    my %par = @_;
    my $data = $par{'data'};
    my $type = $par{'type'};
    my $lang = $par{'lang'};
    my $src  = $par{'src'};
    my $tgt  = $par{'tgt'};
    my $result = "";

    if( lc($src) eq "wx" and lc($tgt) eq "utf")
    {
        #open(STDIN,"<:utf8");
        open INFILE, '<:utf8', \$data or die $!;
        if( lc($type) eq "ssf" )
        {
            #while($line=<STDIN>)
            while(my $line=<INFILE>)
            {
                chomp ($line);
                my $lex_out;
                my ($num, $tkn, $pos, $fs) = split(/\t/,$line);
                if ($tkn !~ /^\^?\@.*/)
                {
                    $lex_out = &wx2utf_urd($tkn, $lang);
                }
                else
                {
                    $lex_out = $tkn;
                }
                if($fs ne "")
                {
                    my @fss = split(/\|/, $fs);
                    my $len = @fss;
                    my @string  = "";
                    my $newfs = "";
                    my $i=0;
                    my @lex_root = ();
                    foreach my $af (@fss)
                    {
                        my $FSreference = &read_FS($af, $line);
                        @lex_root = &get_values("lex", $FSreference);
                        my @cat_root = &get_values("cat", $FSreference);
                        my @fs_vib = &get_values("vib", $FSreference);
                        my @fs_head = &get_values("head", $FSreference);
                        my @fs_name = &get_values("name", $FSreference);
                        foreach my $field (@lex_root)
                        {
                            # if lcat is punc then don't convert it
                            if ($cat_root[0] ne "punc" and $lex_root[0] !~ /^\^?\@.*/)
                            {
                                my $val_out = &wx2utf_urd($field, $lang);
                                my @lex_arr=();
                                push @lex_arr,$val_out;
                                &update_attr_val("lex", \@lex_arr,$FSreference,$af);
                                $string[$i]=&make_string($FSreference,$af);
                            }
                        }
                        foreach my $field1 (@fs_vib)
                        {
                            if ($field1 !~ /^\^?\@.*/)
                            {
                                my $vib_out = &wx2utf_urd($field1, $lang);
                                my @fs_vib_arr=();
                                push @fs_vib_arr,$vib_out;
                                &update_attr_val("vib", \@fs_vib_arr,$FSreference,$af);
                                $string[$i]=&make_string($FSreference,$af);
                            }
                        }
                        foreach my $field (@fs_head)
                        {
                            if ($field !~ /^\^?\@.*/)
                            {
                                my $val_out = &wx2utf_urd($field, $lang);
                                my @head_arr=();
                                push @head_arr,$val_out;
                                &update_attr_val("head", \@head_arr, $FSreference, $af);
                                $string[$i] = &make_string($FSreference, $af);
                            }
                        }
                        foreach my $field (@fs_name)
                        {
                            if ($field !~ /^\^?\@.*/)
                            {
                                my $val_out = &wx2utf_urd($field, $lang);
                                my @name_arr=();
                                push @name_arr,$val_out;
                                &update_attr_val("name", \@name_arr,$FSreference,$af);
                                $string[$i]=&make_string($FSreference,$af);
                            }
                        }
                        $i++;
                    }
                    foreach my $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 =~ /\)\)/)
                    {
                        my ($num,$lex,$pos,$fs) = split(/\t/,$line);
                        $result .= "$num\t$lex\t$pos\t$newfs\n";
                    }
                    else
                    {
                        $result .= "$num\t$lex_out\t$pos\t$newfs\n";
                    }
                } # end if fs ne ""
                else {  # try to understand this else block

                    if($lex ne "((" and $lex ne "))")
                    {
                        $result .= "$num\t$lex_out\t$pos\t$fs\n";
                    }
                    else {
                        $result .= "$line\n";
                    }
                }


            } # end while loop
        } # if format SSF end
        #elsif(($type eq "TEXT") or ($type eq "text"))
        elsif( lc($type) eq "text" )
        {
            while(my $line=<INFILE>)
            {
                #chomp ($line);
                my $text_out = &wx2utf_urd($line, $lang);
                $result .= $text_out;
                    #print "under construction\n";
            }
        } # type text end
        else
        {
            #print "type mismatch\n";
            print "format type $type is NOT supported.\n";
            print "Try $0 --help for more information\n";
            exit(0);
        }
        close(INFILE);
    } # end if statement src=wx tgt=utf

    elsif( lc($src) eq "utf" and lc($tgt) eq "wx")
    {
        #open(STDIN,"<:utf8");
        open INFILE, '<', \$data or die $!;
        if( lc($type) eq "ssf" )
        {
            #print "ssf conversion start\n";
            while(my $line=<INFILE>)
            {
                chomp ($line);
                my ($num, $tkn, $pos, $fs) = split(/\t/,$line);
                my $lex_out = &utf2wx_urd($tkn, $lang);

                if($fs ne "")
                {
                    my @fss = split(/\|/, $fs);
                    my $len = @fss;
                    my @string  = "";
                    my $newfs = "";
                    my $i=0;
                    my @lex_root = ();
                    foreach my $af (@fss)
                    {
                        my $FSreference = &read_FS($af, $line);
                        @lex_root = &get_values("lex", $FSreference);
                        my @cat_root = &get_values("cat", $FSreference);
                        my @fs_vib = &get_values("vib", $FSreference);
                        my @fs_head = &get_values("head", $FSreference);
                        my @fs_name = &get_values("name", $FSreference);
                        foreach my $field (@lex_root)
                        {
                            # if lcat is punc then don't convert it
                            if ($cat_root[0] ne "punc" )
                            {
                                my $val_out = &utf2wx_urd($field, $lang);
                                my @lex_arr=();
                                push @lex_arr,$val_out;
                                &update_attr_val("lex", \@lex_arr,$FSreference,$af);
                                $string[$i]=&make_string($FSreference,$af);
                            }
                        }
                        foreach my $field1 (@fs_vib)
                        {
                            my $vib_out = &utf2wx_urd($field1, $lang);
                            my @fs_vib_arr=();
                            push @fs_vib_arr,$vib_out;
                            &update_attr_val("vib", \@fs_vib_arr,$FSreference,$af);
                            $string[$i]=&make_string($FSreference,$af);
                        }
                        foreach my $field (@fs_head)
                        {
                                my $val_out = &utf2wx_urd($field, $lang);
                                my @head_arr=();
                                push @head_arr,$val_out;
                                &update_attr_val("head", \@head_arr, $FSreference, $af);
                                $string[$i] = &make_string($FSreference, $af);
                        }
                        foreach my $field (@fs_name)
                        {
                               my $val_out = &utf2wx_urd($field, $lang);
                               my @name_arr=();
                               push @name_arr,$val_out;
                               &update_attr_val("name", \@name_arr,$FSreference,$af);
                               $string[$i]=&make_string($FSreference,$af);
                        }
                        $i++;
                    }
                    foreach my $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 =~ /\)\)/)
                    {
                        ($num,$lex,$pos,$fs) = split(/\t/,$line);
                        #print $num,"\t",$lex,"\t",$pos,"\t",$newfs,"\n";
                        binmode OUTFILE, ":utf8";
                        $result .= "$num\t$lex\t$pos\t$newfs\n";
                    }
                    else
                    {
                        $result .= "$num\t$lex_out\t$pos\t$newfs\n";
                        #print $num,"\t",$lex_out,"\t",$pos,"\t",$newfs,"\n";
                    }
                } # end if fs ne ""
                else {  # try to understand this else block
                    if($lex ne "((" and $lex ne "))")
                    {
                        $result .= "$num\t$lex_out\t$pos\t$fs\n";
                        #print $num,"\t",$lex_out,"\t",$pos,"\t",$fs,"\n";
                    }
                    else{
                        $result .= "$line\n";
                    }
                }


            } # end while loop

        } # type SSF end
        elsif( lc($type) eq "text")
        {
            #print "text conversion block called\n";
            while(my $line=<INFILE>)
            {
                #chomp ($line);
                #print "before convert call $line\n";
                my $text_out = &utf2wx_urd($line, $lang);
                $result .= $text_out;
                #print "under construction\n";
            }
        } # end format type text
        else
        {
            print "format type $type is NOT supported.\n";
            print "Try $0 --help for more information\n";
            exit(0);
        }
        close(INFILE);
    } # end elsif statement src=utf tgt=wx
    else
    {
        print "$0: source and target encoding mismatch\n";
        print "Try $0 --help for more information\n";
        exit(0);
    }
    return $result;

} # end of sub convertor_indic

1;
