package Padre::Plugin::My;

use 5.008;
use strict;
use warnings;
use utf8;
use Padre::Constant ();
use Padre::Plugin   ();
use Padre::Wx       ();

our $VERSION = '0.84';

#our @ISA     = 'Padre::Plugin';
use base 'Padre::Plugin';

#^@ISA used instead of "use base"

#####################################################################
# Padre::Plugin Methods

sub padre_interfaces {
    return (
        'Padre::Plugin'   => 0.66,
        'Padre::Constant' => 0.66,
    );
}

sub plugin_name {
    return 'My Plugin';
}

sub menu_plugins_simple {
    my $self = shift;
    return $self->plugin_name => [
        'About' => sub { $self->show_about },

        'Replace_slash'       => sub { $self->replace_slash },
        'Replace_start_digit' => sub { $self->replace_start_digit },
        'Run_perlcritic_from_activeperl' =>
          sub { $self->run_perlcritic_from_activeperl },

        # 'A Sub-Menu...' => [
        #     'Sub-Menu Entry' => sub { $self->yet_another_method },
        # ],
    ];
}

#####################################################################
# Custom Methods

sub show_about {
    my $self = shift;

    # Locate this plugin
    my $path = File::Spec->catfile( Padre::Constant::CONFIG_DIR,
        qw{ plugins Padre Plugin My.pm } );

    # Generate the About dialog
    my $about = Wx::AboutDialogInfo->new;
    $about->SetName('My Plug-in');
    $about->SetDescription( <<"END_MESSAGE" );
The philosophy behind Padre is that every Perl programmer
should be able to easily modify and improve their own editor.

To help you get started, we've provided you with your own plug-in.

It is located in your configuration directory at:
$path
Open it with with Padre and you'll see an explanation on how to add items.
END_MESSAGE

    # Show the About dialog
    Wx::AboutBox($about);

    return;
}

sub replace_slash {
    my $self = shift;
    my $main = $self->main;

    my $doc  = Padre::Current->document;
    my $text = $doc->text_get();
    $text =~ s#\\#/#g;
    $doc->text_set($text);

    return;
}

sub replace_start_digit {
    my $self = shift;
    my $main = $self->main;

    my $doc  = Padre::Current->document;
    my $text = $doc->text_get();
    $text =~ s/^[0-9]+\s*//mg;
    $doc->text_set($text);

    return;
}

#C:\Perl\bin\wperl.exe -x "C:\Perl\bin\perlcritic-gui" c:\Users\kshunkov\Documents\perl\python\get_trade\get_trade.pl

sub run_perlcritic_from_activeperl {
    my $self = shift;
    my $main = $self->main;
    my $doc  = Padre::Current->document;

    my $filename = $doc->filename;

    my $exec_shell =
q{C:\Perl\bin\wperl.exe -x "C:\Perl\bin\perlcritic-gui" c:\Users\nmishin\Documents\git\perlcritic\perlcritic_profile.perlcriticrc }
      . $filename
      . q{ --run};
    $main->message($exec_shell);

    my $a = run_shell($exec_shell);
    return;
}

sub run_shell {
    my ($cmd) = @_;
    use IPC::Open3 'open3';
    use Carp;
    use English qw(-no_match_vars);
    my @args  = ();
    my $EMPTY = q{};
    my $ret   = undef;
    my ( $HIS_IN, $HIS_OUT, $HIS_ERR ) = ( $EMPTY, $EMPTY, $EMPTY );
    my $childpid = open3( $HIS_IN, $HIS_OUT, $HIS_ERR, $cmd, @args );
    $ret = print {$HIS_IN} "stuff\n";
    close $HIS_IN or croak "unable to close: $HIS_IN $ERRNO";
    ;    # Give end of file to kid.

    if ($HIS_OUT) {
        my @outlines = <$HIS_OUT>;    # Read till EOF.
        $ret = print " STDOUT:\n", @outlines, "\n";
    }
    if ($HIS_ERR) {
        my @errlines = <$HIS_ERR>;    # XXX: block potential if massive
        $ret = print " STDERR:\n", @errlines, "\n";
    }
    close $HIS_OUT or croak "unable to close: $HIS_OUT $ERRNO";

    #close $HIS_ERR or croak "unable to close: $HIS_ERR $ERRNO";#bad..todo
    waitpid $childpid, 0;
    if ($CHILD_ERROR) {
        $ret = print "That child exited with wait status of $CHILD_ERROR\n";
    }
    return 1;
}

sub other_method {
    my $self = shift;
    my $main = $self->main;

    $main->message( 'Hi from My Plugin', 'Other method' );

# my $name = $main->prompt('What is your name?', 'Title', 'UNIQUE_KEY_TO_REMEMBER');
# $main->message( "Hello $name", 'Welcome' );

    # my $doc   = Padre::Current->document;
    # my $text  = $doc->text_get;
    # my $count = length($text);
    # my $filename = $doc->filename;
    # $main->message( "Filename: $filename\nCount: $count", 'Current file' );

    # my $doc   = Padre::Current->document;
    # my $text  = $doc->text_get();
    # $text     =~ s/[ \t]+$//m;
    # $doc->text_set( $text );

    return;
}

1;

__END__

=pod

=head1 NAME

Padre::Plugin::My - My personal plug-in

=head1 DESCRIPTION

This is your personal plug-in. Update it to fit your needs. And if it
does interesting stuff, please consider sharing it on C<CPAN>!

=head1 COPYRIGHT & LICENSE

Currently it's copyrighted © 2008-2010 by The Padre development team as
listed in Padre.pm... But update it and it will become copyrighted © You
C<< <you@example.com> >>! How exciting! :-)

=cut

# Copyright 2008-2011 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.
