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

weechat::register('mouse-gestures', 'ArZa, Nei', '0.1', 'GPL3', 'Mouse gestures', '', '');
weechat::hook_signal('mouse', 'mouse_main', '');

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

weechat::config_set_plugin('debug', 0) unless (weechat::config_is_set_plugin('debug'));

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` || '34 127');
#	  my @max=split(' ', `stty size`);
	  	  
	  my $maxrow=$max[0];
	  my $maxcol=$max[1];
	  
	  weechat::print('', "act: $act; row: $row/$maxrow; col: $col/$maxcol ($_[2])") if (weechat::config_get_plugin('debug'));

	  
	  if($act==64){ # scroll up
		if($col<15){ # left side = buffers
		  if($lastact==1){ weechat::command('', '/bar scroll buffers * y-30%'); }
		  else{ 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');
		  }
		}
	  }
	  
	  elsif($act==65){ # scroll down
		if($col<15){ # left side = buffers
		  if($lastact==1){ weechat::command('', '/bar scroll buffers * y+30%'); }
		  else{ 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');
		  }
		}
	  }


	  if($act==72){ # alt + scroll up
		if($col<15){ # left side = buffers
		  weechat::command('', '/bar scroll buffers * y-50%');
		}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('', '/buffer -1');
		  }
		}
	  }
	  
	  elsif($act==73){ # alt + scroll down
		if($col<15){ # left side = buffers
		  weechat::command('', '/bar scroll buffers * y+50%');
		}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('', '/buffer +1');
		  }
		}
	  }

	  
	  elsif($act==0 && ($col>15||$col==0||$col==3) ){ # left click, not in buffers list
	  }
	  
	  elsif($act==1){ # middle click
	    #weechat::command('', '/input jump_previously_visited_buffer');
	  }



	  if($act==88){ # ctrl + alt + scroll up
		if($col<15){ # left side = buffers
		  weechat::command('', '/bar scroll buffers * x-10%');
		}elsif($col==$act){ # right side = nicklist
		  weechat::command('', '/bar scroll nicklist * x-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');
		  }
		}
	  }
	  
	  elsif($act==89){ # ctrl + alt + scroll down
		if($col<15){ # left side = buffers
		  weechat::command('', '/bar scroll buffers * x+10%');
		}elsif($col==$act){ # right side = nicklist
		  weechat::command('', '/bar scroll nicklist * x+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');
		  }
		}
	  }

	  
	  elsif($act==0 && ($col>15||$col==0||$col==3) ){ # left click, not in buffers list
	  }
	  
	  elsif($act==1){ # middle click
	    #weechat::command('', '/input jump_previously_visited_buffer');
	  }

	  
	  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 && $lastact==0){ weechat::command( '', '/window scroll '.($row-$lastrow).'0' ); }
		}
		if($lastact==1 && $row==$lastrow){
		  if($col==3 && $lastcol==1){ weechat::command('', '/bar toggle nicklist'); }
		  elsif($col<15 && $col==$lastcol){ weechat::command('', '/bar toggle buffers'); }
		}
		
	  }
	  
	  
	  if($act==80){ # ctrl + scroll up
	    if($col<15){ # buffers list
		  weechat::command('', '/buffer move -1');
		}elsif($col==$act){ # nicklist
		  weechat::command('', '/bar scroll nicklist * y-90%');
		}else{ # buffer
		  weechat::command('', '/window scroll -20');
		}
	  }
	  
	  if($act==81){ # ctrl + scroll down
	    if($col<15){ # buffers list
		  weechat::command('', '/buffer move +1');
		}elsif($col==$act){ # nicklist
		  weechat::command('', '/bar scroll nicklist * y+90%');
		}else{ # buffer
		  weechat::command('', '/window scroll +20');
		}
	  }
	  
    if(defined($act)){ $lastact=$act; }
    if(defined($col)){ $lastcol=$col; }
    if(defined($row)){ $lastrow=$row; }

	}

  weechat::WEECHAT_RC_OK;

}
