use strict;
use warnings;

our $VERSION = '1.4.5'; # 7b1f0e3710d7df9
our %IRSSI = (
    authors     => 'Nei',
    contact     => 'Nei @ anti@conference.jabber.teamidiot.de',
    url         => "http://anti.teamidiot.de/",
    name        => 'hideshow',
    description => 'Removes and re-adds lines to the Irssi buffer view.',
    license     => 'GNU GPLv2 or later',
   );

# Usage
# =====
# Use this script to hide and re-add lines into your Irssi view. You
# can grab a custom-modified recentdepart.pl to hide smart-filtered
# messages instead of ignore, if you do
#
#  /set recdep_use_hideshow ON
#
# You can use trigger.pl with:
#
#  /trigger add ... -command 'script exec $$Irssi::scripts::hideshow::hide_next = 1'
#
# instead of -stop

# Options
# =======
# /set hideshow_level <levels>
# * list of levels that should be hidden from view
#
# /set hideshow_hide <ON|OFF>
# * if hiding is currently enabled or not. make a key binding to
#   conveniently toggle this setting (see below)

# Commands
# ========
# you can use this key binding:
#
# /bind meta-= command ^toggle hideshow_hide
#
# /scrollback status hidden
# * like /scrollback status, but for the hidden part (some statistics)

use Irssi;
use Irssi::TextUI;
use version;

my $irssi_version = qv(Irssi::parse_special('v$J') =~ s/-.*//r);
if ($irssi_version < v1.1.0) {
    die "This script requires Irssi v1.1 or later\n";
}

sub setc () {
    $IRSSI{name}
}

sub set ($) {
    setc . '_' . $_[0]
}

my $HIDE;
my $hide_level;
my $ext_hidden_level = MSGLEVEL_LASTLOG << 1;
my $multi_msgs_level = (MSGLEVEL_QUITS|MSGLEVEL_NICKS);


sub hideshow {
    if ($HIDE) {
	my $level = Irssi::bits2level($hide_level | $ext_hidden_level);
	Irssi::command("^foreach window ^window hidelevel $level");
    } else {
	Irssi::command('^foreach window ^window hidelevel -all -hidden');
    }
}

sub setup_changed {
    my $old_level = $hide_level;
    $hide_level = Irssi::settings_get_level( set 'level' );
    my $old_hidden = $HIDE;
    $HIDE = Irssi::settings_get_bool( set 'hide' );
    if (!defined $old_hidden || $HIDE != $old_hidden || $old_level != $hide_level) {
	hideshow();
    }
}

sub init_hideshow {
    setup_changed();
    $Irssi::scripts::hideshow::hide_next = undef;
}

sub UNLOAD {
    if ($irssi_version >= v1.2.0) {
	$hide_level = Irssi::settings_get_level('window_default_hidelevel');
	my $level = Irssi::bits2level($hide_level);
	Irssi::command('^foreach window ^window hidelevel -all -hidden');
	Irssi::command("^foreach window ^window hidelevel $level");
    } else { # $irssi_version >= v1.1.0
	Irssi::command('^foreach window ^window hidelevel -all hidden');
    }
}

my $multi_msgs_last;

sub prt_text_issue {
    my $dest = $_[0];
    my $stripd = $_[2];
    unless (ref $dest) {
	$multi_msgs_last = undef;
	$Irssi::scripts::hideshow::hide_next = undef;
	return;
    }

    my $hide;
    if ($dest->{level} & $multi_msgs_level
	   && defined $multi_msgs_last && $multi_msgs_last eq $stripd) {
	$hide = 1;
    }
    elsif ($Irssi::scripts::hideshow::hide_next) {
	$multi_msgs_last = undef;
	$hide = 1;
	if ($dest->{level} & $multi_msgs_level) {
	    $multi_msgs_last = $stripd;
	}
    }
    else {
	$multi_msgs_last = undef;
    }
    $Irssi::scripts::hideshow::hide_next = undef;

    if ($hide) {
	$_[0] = Irssi::Server::format_create_dest($dest->{server}, $dest->{target}, $dest->{level} | $ext_hidden_level, $dest->{window});
	&Irssi::signal_continue;
    }
}

Irssi::signal_add_last({
    'setup changed' => 'setup_changed',
});
Irssi::signal_add({
    'print text'    => 'prt_text_issue',
});

Irssi::settings_add_level( setc, set 'level', '' );
Irssi::settings_add_bool( setc, set 'hide', 1 );

init_hideshow();

{ package Irssi::Nick }
