use List::Util qw(any);

our $VERSION = "20200420-2";

sub cmd_pslack_add {
    my ($arg, $server, $witem) = @_;
    my @new_channels = split(/[ ,]+/, $arg);
    my @old_channels = split(/[ ,]+/, Irssi::settings_get_str('pslack_channels'));

    my %seen;
    my @merged_channels = grep(!$seen{$_}++, @old_channels, @new_channels);

    Irssi::settings_set_str('pslack_channels', join(' ', @merged_channels));
    print("Translation active on channels: ", join(', ', @merged_channels))
}

sub cmd_pslack_del {
    my ($arg, $server, $witem) = @_;

    my @old_channels = split(/[ ,]+/, Irssi::settings_get_str('pslack_channels'));
    my @deleted_channels = split(/[ ,]+/, $arg);

    my %seen;
    grep(!@seen{$_}++, @deleted_channels);
    my @remaining_channels = grep(!$seen{$_}++, @old_channels);

    Irssi::settings_set_str('pslack_channels', join(' ', @remaining_channels));
    print("Translation active on channels: ", join(', ', @remaining_channels))
}

sub cmd_pslack_list {
    my ($arg, $server, $witem) = @_;
    my @channels = split(/[ ,]+/, Irssi::settings_get_str('pslack_channels'));

    print("Translation active on channels: ", join(', ', @channels))
}

sub cmd_pslack_help {
    my ($arg, $server, $witem) = @_;
    print_help();
}

sub print_help {
    my $help = "pslack $VERSION

Translates messages bridged from Slack to IRC to show the correct nickname.

Example:
    Before: < bot> <nick> Hello world!
    After: <\$nick> Hello world!

Commands:
    /pslack_add Add a channel for sender translation. Separate multiple channels with spaces.
    /pslack_del Remove a channel from sender translation. Separate multiple channels with spaces.
    /pslack_list Show channels on which sender translation is enabled.
    /pslack Displays this message.

Variables:
    pslack_channels Space-separated list of channels on which the sender translation is active.
    pslack_botname Name of the bot performing the translation. Separate with spaces for multiple bots.

What to do now:
    Run /pslack_add #your_channel
";
    print($help);
}


sub sig_message_public_first {
    my ($server, $msg, $nick, $addr, $target) = @_;

    my $botnicks_str = Irssi::settings_get_str('pslack_botname');
    my @botnicks = split /[ ,]/, $botnicks_str;

    my $channels_str = Irssi::settings_get_str('pslack_channels');
    my @channels = split /[ ,]/, $channels_str;

    if ((any { $_ eq $nick } @botnicks) && (any { $_ eq $target } @channels)) {
        if ($msg =~ /^<(\w+)> (.*)$/){
            $nick = $1;
            $msg = $2;
            $addr = $nick . "!slack";
            Irssi::signal_continue($server, $msg, $nick, $addr, $target);
        }
    }
}

sub sig_message_public_last {
    my ($server, $msg, $nick, $addr, $target) = @_;

    if ($addr =~ /^.*!slack$/) {
        my $window = $server->window_find_item($target);
        my $theme = $window->{theme} || Irssi::current_theme;
        my $format = $theme->get_format('fe-common/core', 'pubmsg');
        (my $slack_format = $format) =~ s/\$2/:/g;
        $window->command("^format pubmsg $slack_format");
        Irssi::signal_continue($server, $msg, $nick, $addr, $target);
        $window->command("^format pubmsg $format");
    }
}

print_help();

Irssi::settings_add_str('pslack', 'pslack_channels', '#ohjelmointiputka');
Irssi::settings_add_str('pslack', 'pslack_botname', '||');

Irssi::command_bind('pslack_add', 'cmd_pslack_add');
Irssi::command_bind('pslack_del', 'cmd_pslack_del');
Irssi::command_bind('pslack_list', 'cmd_pslack_list');
Irssi::command_bind('pslack', 'cmd_pslack_help');

Irssi::signal_add_first('message public', 'sig_message_public_first');
Irssi::signal_add_last('message public', 'sig_message_public_last');
