#!/usr/bin/env perl
use Modern::Perl '2012';
use lib '../lib';

use File::Slurp qw(slurp);
use HTML::TreeBuilder;
use Data::Dump qw(pp);
use MGH_Biostat::TravEpi::SimpleRulesBase::Translator::LangFile qw(read_lang_file write_lang_file);

binmode \*STDOUT, ':encoding(UTF-8)';

my @langs = qw(en-US ar bn es fr-FR hi ht pt-BR zh-CN);

my @extracts = (
    [ '0.1.1'           => 'title1' ],
    [ '0.1.2'           => 'title2' ],
    [ '0.1.3'           => 'recs' ],
    [ '0.1.3.1'         => 'visit-pcp' ],
    [ '0.1.4.0'         => 'ebolawarn' ],
    [ '0.1.5'           => 'printbtn' ],
    [ '0.1.5.1'         => 'checklist' ],
    [ '0.1.5.2.0'       => 'before' ],
    [ '0.1.5.2.1'       => 'seehcp' ],
    [ '0.1.5.2.2.0'     => 'talk-to-hcp' ],
    [ '0.1.5.2.2.1'     => 'routinevacc-adult' ],
    [ '0.1.5.2.2.2'     => 'routinevacc-child' ],
    [ '0.1.5.2.2.3'     => 'routinevacc-unspec' ],
    [ '0.1.5.2.2.4'     => 'vaccines' ],
    [ '0.1.5.2.2.4.1.0' => 'hepa' ],
    [ '0.1.5.2.2.4.1.1' => 'je' ],
    [ '0.1.5.2.2.4.1.2' => 'mening' ],
    [ '0.1.5.2.2.4.1.3' => 'meningsa' ],
    [ '0.1.5.2.2.4.1.4' => 'polio' ],
    [ '0.1.5.2.2.4.1.5' => 'rabies' ],
    [ '0.1.5.2.2.4.1.6' => 'typhoid' ],
    [ '0.1.5.2.2.4.1.7' => 'yfreq' ],
    [ '0.1.5.2.2.4.1.8' => 'yfmaybe' ],
    [ '0.1.5.2.2.5'     => 'malaria' ],
    [ '0.1.5.2.2.6'     => 'othervacc' ],
    [ '0.1.5.2.3'       => 'plan' ],
    [ '0.1.5.2.4.0'     => 'insurance' ],
    [ '0.1.5.3.0'       => 'during' ],
    [ '0.1.5.3.1'       => 'takemeds' ],
    [ '0.1.5.3.2.0'     => 'take-malaria-meds' ],
    [ '0.1.5.3.2.1'     => 'take-normal-meds' ],
    [ '0.1.5.3.3'       => 'protect' ],
    [ '0.1.5.3.4.0'     => 'bugs-diseases' ],
    [ '0.1.5.3.4.1'     => 'bugs-normal' ],
    [ '0.1.5.3.4.2'     => 'food-western' ],
    [ '0.1.5.3.4.3'     => 'food-rest' ],
    [ '0.1.5.3.4.4'     => 'road' ],
    [ '0.1.5.4.0'       => 'after' ],
    [ '0.1.5.4.1.0'     => 'seek-med-care' ],
    [ '0.1.5.4.1.1'     => 'followup-w-hcp' ],
    [ '0.1.6.0'         => 'addtlinfo' ],
    [ '0.1.6.1'         => 'cdclinks' ],
    [ '0.1.6.3'         => 'askhcp-if-qs' ],
    [ '0.1.6.4'         => 'learnmore' ],
    [ '0.1.6.5'         => 'startover' ],
    [ '0.1.6.6'         => 'bye' ],
);

my %used_extracts = (
    map { $_ => 1 }
      map { $_->[ 0 ] }
      @extracts
);

foreach my $lang (@langs) {
    print "$lang .. ";
    my $lang_file = '../lang/recommendations.' . $lang . '.lang';

    my $lang_data = read_lang_file($lang_file);

    my $d = slurp( '/Users/rem16/projects/travepi/trhip/templates/recommendations.' . $lang . '.tmpl', { binmode => ':encoding(UTF-8)' } );
    $d =~ s/\[% (?:START|END|INCLUDE).+?%\]//g;

    my $tree = HTML::TreeBuilder->new();
    $tree->no_expand_entities(1);
    $tree->parse($d);
    $tree->eof();

    foreach my $e (@extracts) {
        my ( $address, $label ) = @$e;
        my $elt = $tree->address($address);

        my $t = '';

        if ( !defined($elt) ) {
            ;
            ##say "$lang : $label : undef";
        }
        elsif ( ref($elt) ) {
            my @content = $elt->content_list();
            my $out     = '';
            foreach my $subelt (@content) {
                unless ( ref($subelt) and $used_extracts{ $subelt->address() } ) {
                    $out .= ref($subelt) ? $subelt->as_HTML('') : $subelt;
                }
            }
            $t = $out;
            ##say "$lang : $label : $t";
        }
        else {
            $t = $elt;
            ##say "$lang : $label : $t";
        }

        $t =~ s/^\s*//;
        $t =~ s/\s*$//;

        $lang_data->{'_stanzas'}{$label}{'transl'} = $t;
    }

    write_lang_file($lang_file, $lang_data);
    say "done";
    ##say pp($lang_data);

}

__END__