use strict; use warnings;
$INC{'Encode/ConfigLocal.pm'}=1;
require Encode;

weechat::register('Mouse', 'Nei <anti.teamidiot.de>', '0.2', 'GPL3', 'Mouse handler', 'stop_mouse', '');

## Edited by ArZa:
# - scroll bars when mouse is on
# - no temp_off
# - /input jump_previously_visited_buffer on middle click
# - gestures: dragging up/down scrolls rowchange*10

weechat::hook_modifier('input_text_content', 'mouse_evt2', '');
weechat::hook_command('mouse_input', 'parse mouse input (bind to meta2-M)', '', '', '', 'mouse_input', '');
#weechat::hook_signal('mouse', 'mouse_scroll', '');
#weechat::hook_signal('mouse', 'mouse_temp_off', '');
weechat::hook_signal('mouse', 'mouse_main', '');

our $MOUSE_INPUT = undef;
our $MOUSE_CODE = '';

our $lastcol=undef;
our $lastrow=undef;
our $lastact=undef;

init_mouse();

sub mouse_main {
	Encode::_utf8_on($_[2]);
	my $c=$_[2];
	if (defined $c) {
	
      my @res = split //, $c;
      map { $_ = (ord)-32 } @res;
	  
	  my $act=$res[0];
	  my $col=$res[1];
	  my $row=$res[2];

	  my @max=split(' ', `stty size`);
	  	  
	  my $maxrow=$max[0];
	  my $maxcol=$max[1];
	  
	  #debug# weechat::print('', "act: $act; row: $row/$maxrow; col: $col/$maxcol");
	  
	  if($act==64){ # scroll up
		if($col<15){ # left side = buffers
		  weechat::command('', '/buffer -1');
		}elsif($col==$act){ # right side = nicklist
		  weechat::command('', '/bar scroll nicklist * y-10%');
		}else{ # middle
		  if($row==1){ # first row = title
		    weechat::command('', '/bar scroll title * x-10%');
		  }elsif($row==$maxrow-1){ # second last row = status
		    weechat::command('', '/bar scroll status * x-10%');
		  }elsif($row==$maxrow){ # last row = input
		    weechat::command('', '/input move_previous_char');
		  }else{ # chat area
		    weechat::command('', '/window scroll_up');
		  }
		}
	  $lastact=$act;
	  $lastcol=$col;
	  $lastrow=$row;
	  }
	  
	  elsif($act==65){ # scroll down
		if($col<15){ # left side = buffers
		  weechat::command('', '/buffer +1');
		}elsif($col==$act){ # right side = nicklist
		  weechat::command('', '/bar scroll nicklist * y+10%');
		}else{ # middle
		  if($row==1){ # first row = title
		    weechat::command('', '/bar scroll title * x+10%');
		  }elsif($row==$maxrow-1){ # second last row = status
		    weechat::command('', '/bar scroll status * x+10%');
		  }elsif($row==$maxrow){ # last row = input
		    weechat::command('', '/input move_next_char');
		  }else{ # chat area
		    weechat::command('', '/window scroll_down');
		  }
		}
	  $lastact=$act;
	  $lastcol=$col;
	  $lastrow=$row;
	  }
	  
	  elsif($act==0 && ($col>15||$col==0||$col==3) ){ # left click, not in buffers list
	    $lastact=$act;
	    $lastcol=$col;
		$lastrow=$row;
	  }
	  
	  elsif($act==1){ # middle click
	    weechat::command('', '/input jump_previously_visited_buffer');
	    $lastact=$act;
	    $lastcol=$col;
		$lastrow=$row;
	  }
	  
	  elsif( $act==3 ){ # release
	    if(
		 defined($lastrow) && # last row defined
		 defined($lastcol) && # last column defined
		 ( ($col != $lastcol && $col != $act) || $row != $lastrow ) && # location changed
		 ( $col>15 || ( $col==3 && $lastcol != 3 ) )
		){ # gesture
		  #debug# weechat::print('', "$lastact: ($lastcol, $lastrow) -> $act: ($col, $row)");
		  if($row!=$lastrow){ weechat::command( '', '/window scroll '.($row-$lastrow).'0' ); }
		}
	  }

	  
	}

}

#sub mouse_scroll {
#	Encode::_utf8_on($_[2]);
#	if ($_[2] =~ /^`/) {
#		weechat::command('', '/window scroll_up');
#	}
#	elsif ($_[2] =~ /^a/) {
#		weechat::command('', '/window scroll_down');
#	}
#	weechat::WEECHAT_RC_OK;
#}

#sub mouse_temp_off {
#	Encode::_utf8_on($_[2]);
#	if ($_[2] =~ /^#/) {
#		stop_mouse_tracking();
#		#weechat::hook_timer(500, 0, 1, 'init_mouse_tracking', '');
#		init_mouse_tracking();
#	}
#	weechat::WEECHAT_RC_OK;
#}

sub mouse_input {
	weechat::hook_timer(300, 0, 1, 'mouse_input_stop', '');
	$MOUSE_INPUT = 3;
	weechat::WEECHAT_RC_OK;
}

sub mouse_input_stop {
	$MOUSE_INPUT = undef;
	$MOUSE_CODE = '';
	weechat::WEECHAT_RC_OK;
}

sub mouse_evt2 {
	Encode::_utf8_on($_[3]);
	if ($MOUSE_INPUT) {
		my $buf = weechat::current_buffer();
		my $npos = weechat::buffer_get_integer($buf, 'input_pos')-1;
		$MOUSE_CODE .= substr $_[3], $npos, 1, '';
		weechat::buffer_set($buf, 'input_pos', $npos);
		--$MOUSE_INPUT;
		unless ($MOUSE_INPUT) {
			weechat::hook_signal_send('mouse', weechat::WEECHAT_HOOK_SIGNAL_STRING, $MOUSE_CODE);
			mouse_input_stop();
		}
	}
	$_[3];
}

sub init_mouse_tracking {
	print STDERR "\e[?1005h\e[?1000h";
	weechat::WEECHAT_RC_OK;
}

sub init_mouse {
	weechat::command('', '/key bind meta2-M /mouse_input');
	init_mouse_tracking();
	weechat::WEECHAT_RC_OK;
}

sub stop_mouse_tracking {
	print STDERR "\e[?1000l\e[?1005l";
	weechat::WEECHAT_RC_OK;
}

sub stop_mouse {
	stop_mouse_tracking();
	weechat::command('', '/key reset meta2-M');
	weechat::WEECHAT_RC_OK;
}
