package CROSS::DMOSS::Reporters;

use 5.006;
use strict;
use warnings;

=head1 NAME

CROSS::DMOSS::Reporters - functions that build reports

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

use File::Basename;
use File::Slurp qw/read_file/;
use URI::Find;
use Data::Dumper;
use HTML::FormatText::Html2text;
use File::Comments;
use HTTP::Request;
use LWP::UserAgent;
use Text::Aspell;

=head1 SYNOPSIS

Quick summary of what the module does.

Perhaps a little code snippet.

    use CROSS::DMOSS;


=head1 EXPORT

A list of functions that can be exported.  You can delete this section
if you don't export anything, such as for a purely object-oriented module.

=head1 SUBROUTINES/METHODS

=head2 report_validate_links

=cut

sub report_validate_links {
	my ($dmoss) = @_;

	my $x = $dmoss->{res}->{__PACKAGE__}->{proc_validate_links}->{__value};
	return unless $x;

	my $ok = 0;
	my $nok = 0;
	my $more = '';
	foreach (keys %$x) {
		if ($x->{$_}) {
			$ok++;
			$more .= "$_ [ok]<br>";
		}
		else {
			$nok++;
			$more .= "$_ [nok]<br>";
		}
	}

	my $grade = __calc__grade($ok/($ok+$nok));
	return __build_html_box($grade, 'Verify Links', $more);
}

=head2 report_check_spelling

=cut

sub report_check_spelling {
	my ($dmoss) = @_;

	my $x = $dmoss->{res}->{__PACKAGE__}->{proc_check_spelling}->{__value};
	return unless $x;

	my $ok = $x->{ok};
	my $total = $x->{total};
	my $more = '<table style="width: 90%; font-size: 14px; text-align: center;"><tr><th>File</th><th>Words</th><th>Correct</th><th>&#37</th></tr>';

	foreach (keys %{$dmoss->{res}}) {
		next unless exists($dmoss->{res}->{$_}->{proc_check_spelling}->{__value});
		next if ($_ eq '__PACKAGE__');

		my $ok = $dmoss->{res}->{$_}->{proc_check_spelling}->{__value}->{ok};
		my $total = $dmoss->{res}->{$_}->{proc_check_spelling}->{__value}->{total};

		$more .= sprintf("<tr><td>%s</td><td>%d</td><td>%d</td><td>%d</td></tr>",$_,$total,$ok,int($ok/$total*100));
	}
	$more .= sprintf("<tr><th>Total</th><td>%d</td><td>%d</td><td>%d</td></tr></table>",$total,$ok,int($ok/$total*100));

	my $grade = __calc__grade($ok/$total);
	return __build_html_box($grade, 'Spell Checker', $more);
}

=head2 report_verify_license

=cut

sub report_verify_license {
	my ($dmoss) = @_;

	my $x = $dmoss->{res}->{__PACKAGE__}->{proc_verify_license}->{__value};
	return unless $x;

	my $more = '<table style="width: 90%; font-size: 14px; text-align: center;"><tr><th>File</th><th>License</th></tr>';
   foreach (keys %{$dmoss->{res}}) {
      next unless exists($dmoss->{res}->{$_}->{proc_verify_license}->{__value});
      next if ($_ eq '__PACKAGE__');

      my $lic = $dmoss->{res}->{$_}->{proc_verify_license}->{__value};

      $more .= sprintf("<tr><td>%s</td><td>%s</td></tr>",$_,$lic);
   }
	$more .= '</table><br>This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. For more information about GNU General Public License visit <a href="http://www.gnu.org/copyleft/gpl.html">this page</a>.';

	my $grade = __calc__grade(100);
   return __build_html_box($grade, 'Verify License', $more);
}

=head2 report_lines

=cut

sub report_lines {
   my ($dmoss) = @_;

	my $t_total = 0;
	my $t_comments = 0;
	my $more = '<table style="width: 90%; font-size: 14px; text-align: center;"><tr><th>File</th><th>Lines</th><th>Comments</th><th>&#37</th></tr>';

	foreach (keys %{$dmoss->{res}}) {
      next unless exists($dmoss->{res}->{$_}->{proc_lines}->{__value});
      next unless ($dmoss->{typeof}->{$_});
      next unless ($dmoss->{typeof}->{$_} =~ /(.*?source|makefile)/i);

		my $total = $dmoss->{res}->{$_}->{proc_lines}->{__value}->{total};
		my $comments = $dmoss->{res}->{$_}->{proc_lines}->{__value}->{comments};
      $more .= sprintf("<tr><td>%s</td><td>%d</td><td>%d</td><td>%d&#37</td></tr>",$_,$total,$comments,int($comments/$total*100));

		$t_total += $total;
		$t_comments += $comments;
	}
	$more .= sprintf("<tr><th>Total</th><td>%d</td><td>%d</td><td>%d&#37</td></tr>",$t_total,$t_comments,int($t_comments/$t_total*100));
	$more .= '</table>';

	my $grade = __calc__grade($t_comments/$t_total);
   return __build_html_box($grade, 'Comment Lines', $more);
}

=head2 report_rel_id_doc

=cut

sub report_rel_id_doc {
   my ($dmoss) = @_;

    # XXX use module to tokenize
	my $r = `cat /tmp/full_plain_text.txt | tr -sc 'A-Za-z' '\\n' | sort | uniq`;
	my %docs;
	foreach (split /\n/, $r) {
		$docs{$_}++;
	}

	my $found = 0;
	my $total = 0;
	$r = `cat /tmp/full_func_id.txt`;
	foreach my $id (split /\n/, $r) {
		my $flag = 0;
      my $res = `conc_cexpander $id`;
		foreach (split /\n/, $res) {
			if ($_ =~ m/\[(\w)\]\s+(.*)\s+/) {
				$flag = 1 if (exists($docs{$2}));
			}
		}
		$found++ if $flag;
		$total++;
	}
	
	$found = 1 unless $found;
	my $more = $total>0 ?'Function identifiers found in documentation: '.int($found/$total*100).'%' : 'not fount';
	my $grade = $total>0 ? __calc__grade($found/$total) : 'not found';
   return __build_html_box($grade, 'Function Identifiers Found in Documentation', $more);
}

=head2 report_classify

=cut

sub report_classify {
   my ($dmoss) = @_;

	my $r = `dmoss-classify`;

	my $lines = split /\n/, $r;
	my $grade = __calc__grade($lines/2);
	my @lines = split /\n/, $r;
	$lines[1] =~ s/^\W+//;
	my $more = "<ul><li>$lines[0]<ul><li>$lines[1]</li></ul></li></ul>";

   return __build_html_box($grade, 'Automatic Classification', $more);
}

sub __calc__grade {
	my $num = shift;
	
	return 'F' if $num < 0.1;
	return 'D' if $num < 0.4;
	return 'C' if $num < 0.6;
	return 'B' if $num < 0.8;
	return 'A';
}

sub __build_html_box {
	my ($grade, $title, $more) = @_;
	my $tag = lc $title;
	$tag =~ s/\W+/_/g;

	my $html = "<div class='grade_box grade_".lc($grade)."'>\b";
	$html .= "<span class='grade'>$grade</span><span class='title'><a href='javascript: show(\"$tag\");'>$title</a></span>\n";
	$html .= "<div style='display: none;' id='$tag' class='grade_box_more'><span class='more'>$more</span></div>";
	$html .= "</div>\n";

	return $html;
}

=head1 AUTHOR

Nuno Carvalho, C<< <smash at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-cross-dmoss at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CROSS-DMOSS>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc CROSS::DMOSS


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CROSS-DMOSS>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/CROSS-DMOSS>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/CROSS-DMOSS>

=item * Search CPAN

L<http://search.cpan.org/dist/CROSS-DMOSS/>

=back


=head1 ACKNOWLEDGEMENTS


=head1 LICENSE AND COPYRIGHT

Copyright 2012 Nuno Carvalho.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


=cut

1; # End of CROSS::DMOSS
