package CatalystX::HelpText::Script::SearchUndocumentedHelpText;
use Moose;
use Moose::Autobox;
use MooseX::Types::Path::Class qw/ Dir /;
use MooseX::Types::Moose qw/Str Undef/;
use File::Find;
use Data::Dumper;
use Getopt::Long::Descriptive; # Force GLD as we override bits..
use MooseX::Types::LoadableClass qw/ LoadableClass /;
use Template;
use Capture::Tiny qw/capture/;
use namespace::autoclean;

has help_files_path => (
    is => 'ro',
    isa => Dir,
    coerce => 1,
    required => 1,
    handles => {
        _get_file => 'file',
    }
);

has template_search_dir => (
    is => 'ro',
    isa => Str,
    default => './',
);

has filename_pattern => (
    is => 'ro',
    isa => Str,
    default => '\.(html|tt)$',
);

has help_files_ext => (
    is => 'ro',
    isa => Str|Undef,
    default => 'html',
);

sub run {
    my ($self) = @_;
    my $file_vs_keys = {};
    my @undocumented_keys = ();
    foreach my $fn ($self->find_files->flatten) {
        $file_vs_keys->{$fn} = $self->find_helptext_keys($fn);
        foreach my $key ($file_vs_keys->{$fn}->flatten) {
            unless ($self->is_there_helptext_file_for_key($key)) {
                push @undocumented_keys, $key
            }
        }
    }
    $self->print_result([ @undocumented_keys ], $file_vs_keys);
}

sub find_files {
    my ($self) = @_;
    my $filename_pattern = $self->filename_pattern;
    my @files = ();
    find(
        {
            wanted => sub {
                my $filename = $File::Find::name;
                return unless -f $filename;
                return unless $filename =~ /$filename_pattern/;
                push @files, $filename;
            },
            bydepth => 1
        }, $self->template_search_dir->flatten);
    return [ @files ];
}

sub find_helptext_keys {
    my ($self, $fn) = @_;
    my $dir = $self->template_search_dir;
    my @keys = ();
    my $t = Template->new({
        INCLUDE_PATH => [ $self->template_search_dir ],
        ABSOLUTE => 1,
    });
    my ($stdout, $stderr) = capture {
        $t->process($fn, {  help_text => sub { push @keys, shift } });
    };
    return [ @keys ];
}

sub is_there_helptext_file_for_key {
    my ($self, $key) = @_;
    my $file = $self->_get_file($key);
    $file .= "." . $self->help_files_ext if defined($self->help_files_ext);
    return (-e $file);
}

sub print_result {
    my ($self, $undocumented_keys, $file_vs_keys) = @_;
    print "Undocumented help text keys: \n";
    print " - $_" for ($undocumented_keys->flatten);
}

with qw/
    MooseX::Getopt
/;

__PACKAGE__->meta->make_immutable;
__PACKAGE__->new_with_options->run unless caller;

1;

=head1 NAME

CatalystX::HelpText::Script::SearchUndocumentedHelpText

=head1 SYNOPSIS

    search_undocumented_templates.pl

=head1 SEE ALSO

=over

=item L<CatalystX::HelpText>

=back

=head1 AUTHOR

Toomas Doran, C<< t0m at state51.co.uk >>

Cinxgler Mariaca Minda, C<< cinxgler at ci-info.com >>

=head1 COPYRIGHT

Copyright Oscar Music and Media 2011.

=head1 LICENSE

This sofware is free software, and is licensed under the same terms as perl itself.

=cut

