package view;
use base 'ASF::View';

# BUILD CONSTRAINT:  all views must return $content, $extension.
# additional return values (as seen below) are optional.  However,
# careful use of copies, externals and dependency management in path.pm
# can resolve most issues with this constraint.

use strict;
use warnings;
use ASF::Util qw/read_text_file shuffle sort_tables/;
use File::Temp qw/tempfile/;
use LWP::UserAgent ();
 
use Dotiac::DTL qw/Template/;
use JSON qw(decode_json);

#++ emulate LWP:Simple get
my $ua = LWP::UserAgent->new(timeout => 10);
$ua->env_proxy;
$ua->ssl_opts(
    verify_hostname => 1,
    # As used by curl on cms.zones
    SSL_ca_file => '/usr/local/share/certs/ca-root-nss.crt'
);

# debug
# $ua->add_handler("request_send",  sub { shift->dump; return });
# $ua->add_handler("response_done", sub { shift->dump; return });

# curl fallback
# sub get{
#     my $url=shift;
#     my $data = qx(curl -s "$url");
#     my $stat = $?;
#     if ($stat == 0) {
#         return $data;
#     } elsif ($stat == -1) { # don't replace $!
#         print STDERR "curl failed $!\n";
#         return undef;
#     } else {
#         print STDERR "curl failed $stat $!\n";
#         $! = $stat;
#         return undef;
#     }
# }

sub get{
    my $url=shift;
    my $response = $ua->get($url);
    if ($response->is_success) {
        print $url," - OK\n";
        return $response->decoded_content;
    }
    else {
        print $url,' - ',$response->status_line,"\n";
        $! = $response->code; # LWP::Simple does not even do this
        return undef
    }
}
#-- emulate LWP:Simple get

# Has the same behavior as the above for foo.page/bar.txt
# files, parsing them into a bar variable for the template.
# Otherwise presumes the template is the path.

my $DOAP_JSON = "https://projects.apache.org/json/foundation/projects.json";
my $INC_DOAP_JSON = "https://projects.apache.org/json/foundation/podlings.json";
my $FINGERPRINTS = "https://uls.apache.org/fingerprints_raw.html";

# parsed contents of $DOAP_JSON
my $json_projects;

# parsed contents of $INC_DOAP_JSON
my $json_podlings;

# override default news_page to provide projects and podlings
sub news_page {
    my %args = @_;
    my $count=0;
    for (get_randomised_projectnames()) {
        my $result = get_project_json($_);
        next unless defined $result;
        push @{$args{projects}}, $result;
        last if ++$count == 3;
    }
    $count = 0;
    for (get_randomised_podlingnames()) {
        my $presult = get_podling_json($_);
        next unless defined $presult;
        push @{$args{podlings}}, $presult;
        last if ++$count == 3;
    }
   # Only wanted for this page
    get_project_list(\%args) if $args{path} eq '/index.html';
    return ASF::View::news_page(%args);
}

my $CI_JSON = "https://whimsy.apache.org/public/committee-info.json";

# wrapper for single_narrative to provide pmc and officers etc.
sub single_narrative_wrapper {
    my %args = @_;
    get_committee_data(\%args);
    return ASF::View::single_narrative(%args);
}


# get names from list
sub getnames {
    my $hash = shift;
    my @names;
    for my $l (keys %{$hash}) {
        push @names,$hash->{$l}{'name'};
    }
    return @names;
}

# define a container key to reduce risk of clashing
my $CI_KEY='CI'; # top-level key holding CI non-pmc data

# split and rotate a name to sort on last name
sub rotate_name {
    my @parts = split ' ',shift;
    unshift @parts, (pop @parts); # rotate
    join ' ', @parts;
}

sub get_committee_data {
    my $args = shift;
    my $json = decode_json(get $CI_JSON || "{}");
    my $cttees = $json->{committees};

    # The pmcs are a sorted array, as key access is not required
    $args->{'pmcs'} = []; # array of {name, chair, site}

    # The nonpmcs are referenced by the key from CI
    # hopefully officers won't have any of the same keys...
    $args->{$CI_KEY} = {}; # hash: key=CI key, entry = {[name], chair|person, [site]}

    for my $k (sort { lc $cttees->{$a}{'display_name'} cmp lc $cttees->{$b}{'display_name'} } keys %{$cttees}) {
        my @chairs = getnames($cttees->{$k}{'chair'});
        if ($cttees->{$k}{'pmc'}) {
            push @{$args->{'pmcs'}}, {
                name => $cttees->{$k}{'display_name'},
                chair => join(',',@chairs),
                site => $cttees->{$k}{'site'},
            }
        } else { # a committee of some kind
            $args->{$CI_KEY}{$k} =  {
                chair => join(',',@chairs),
            }
        }
    }
    my $officers = $json->{'officers'};
    for my $k (keys %{$officers}) {
        my @names = getnames($officers->{$k}{'roster'});
        $args->{$CI_KEY}{$k} = { # assume the keys are distinct from non-pmcs
            person => join(',',@names)
        }
    }

    $args->{$CI_KEY}{'board'} = []; # use array to retain order
    my $board = $json->{'board'}{'roster'};
    my @names;
    for my $k (keys %{$board}) {
        push @names, $board->{$k}{'name'};
    }
    @{$args->{$CI_KEY}{'board'}} = sort {rotate_name($a) cmp rotate_name($b)} @names;
}

sub get_project_list {
    my $args = shift;
    my $json = decode_json(get $CI_JSON || "{}");
    my $cttees = $json->{committees};

    my @cttees;
    for my $k (sort { lc $cttees->{$a}{'display_name'} cmp lc $cttees->{$b}{'display_name'} } keys %{$cttees}) {
        if ($cttees->{$k}{'pmc'}) {
            my $letter = uc substr $cttees->{$k}{'display_name'},0,1;
            if ($k eq 'httpd') { # want this first
                unshift @cttees, {
                    name => $cttees->{$k}{'display_name'},
                    site => $cttees->{$k}{'site'},
                    description => $cttees->{$k}{'description'},
                    letter => $letter,
                }
            } else {
                push @cttees, {
                    name => $cttees->{$k}{'display_name'},
                    site => $cttees->{$k}{'site'},
                    description => $cttees->{$k}{'description'},
                    letter => $letter,
                }
            }
        }
    }
    # The web-page needs the data divided into 6 columns. Allow for 26 label rows as well
    my $percol = int(1+(scalar(@cttees)+26)/6); # round up so we don't lose any off the end
    my $done=0;
    my $colnum=0;
    my $lastletter = 'H'; # httpd starts the list
    my $total=0;
    for my $c (@cttees) {
        $total++;
        my $letter = $c->{letter};
        if ($letter ne $lastletter) { # new letter starts here
            $done++; # allow for letter header
            push @{$args->{project_list}[$colnum]}, 
                {description => $c->{description}, name => $c->{name}, site => $c->{site}, letter => $c->{letter}};
            $lastletter = $letter;
        } else {
            push @{$args->{project_list}[$colnum]},
                {description => $c->{description}, name => $c->{name}, site => $c->{site}};
        }
        $done++;
        if ($done >= $percol) {
           $colnum++;
           $done = 0;
        }
    }
    return $args;
}

# Does not appear to be used.
sub news_podlings {
    my %args = @_;
    my $count=0;
    for (get_randomised_podlingnames()) {
        my $presult = get_podling_json($_);
        next unless defined $presult;
        push @{$args{podlings}}, $presult;
        last if ++$count == 3;
    }
    return ASF::View::news_podlings(%args);
}

sub exports {
    my %args = @_;
    my $template = "content$args{path}";
    $args{breadcrumbs} = ASF::View::breadcrumbs($args{path});

    my $page_path = $template;
    $page_path =~ s/\.[^.]+$/.page/;
    if (-d $page_path) {
        for my $f (grep -f, glob "$page_path/*.mdtext") {
            $f =~ m!/([^/]+)\.mdtext$! or die "Bad filename: $f\n";
            $args{$1} = {};
            read_text_file $f, $args{$1};
        }
        $args{table} = `xsltproc $page_path/eccnmatrix.xsl $page_path/eccnmatrix.xml`;

    }
    
    return Template($template)->render(\%args), html => \%args;
}

sub get_project_json {
    my $name = shift;
    # Don't give up if a particular entry cannot be found
    my $entry = $json_projects->{$name} or warn "Can't find $name: $!\n";
    return undef unless defined $entry; # should not happen
    return undef if $entry->{pmc} =~ /attic/;
    return { # keys taken from doap2perl.xsl
        name => $entry->{name},
        shortdesc => $entry->{shortdesc},
        description => $entry->{description},
        url => $entry->{homepage},
        pmc => $entry->{pmc},
        logo => "/logos/res/$entry->{pmc}/default.png",
        doap => $entry->{doap}, # debug
    }
}

sub get_podling_json {
    my $pname = shift;
    # Don't give up if a particular entry cannot be found
    my $pentry = $json_podlings->{$pname} or warn "Can't find $pname: $!\n";
    return undef unless defined $pentry; # should not happen
    return { # keys taken from doap2perl.xsl
        name => $pentry->{name},
        shortdesc => $pentry->{description}, # no shortdesc here
        description => $pentry->{description},
        url => $pentry->{homepage},
        # pmc => $pentry->{pmc}, # pmc is always Incubator
        logo => "/logos/res/$pname/default.png", # Todo: We have no incubator project logos
        doap => "", # debug
    }
}

sub get_randomised_projectnames {
    # don't die, just return empty JSON
    my $json = get $DOAP_JSON || "{}";
    $json_projects = decode_json $json;
    my @names = keys %{$json_projects};
    shuffle \@names;
    return @names;
}

sub get_randomised_podlingnames {
    # don't die, just return empty JSON
    my $pjson = get $INC_DOAP_JSON || "{}";
    $json_podlings = decode_json $pjson;
    my @pnames = keys %{$json_podlings};
    shuffle \@pnames;
    return @pnames;
}

sub fingerprints {
    my %args = @_;
    my $template = "content$args{path}";
    $args{breadcrumbs} = ASF::View::breadcrumbs($args{path});
    $args{fingerprints} = get $FINGERPRINTS || "<!-- no fingerprints -->";
    return Template($template)->render(\%args), html => \%args;
}
1;

=head1 LICENSE

           Licensed to the Apache Software Foundation (ASF) under one
           or more contributor license agreements.  See the NOTICE file
           distributed with this work for additional information
           regarding copyright ownership.  The ASF licenses this file
           to you under the Apache License, Version 2.0 (the
           "License"); you may not use this file except in compliance
           with the License.  You may obtain a copy of the License at

             http://www.apache.org/licenses/LICENSE-2.0

           Unless required by applicable law or agreed to in writing,
           software distributed under the License is distributed on an
           "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
           KIND, either express or implied.  See the License for the
           specific language governing permissions and limitations
           under the License.
