use strict;
use Irssi;

use vars qw/$VERSION %IRSSI/;

$VERSION = '2009-01-28';

%IRSSI = (
  name => 'wsay',
  authors => 'Jorma TerÃ¤s',
  contact => 'hanski@gmail.com',
  url => 'http://www.wamma.fi',
  license => 'Public Domain',
  description => 'wanna say? wamma say!'
);

my $g_font_data = <<FONT_DATA;
 ###
#   #
#####
#   #
#   #

####
#   #
####
#   #
####

 ###
#   #
#
#   #
 ###

####
#   #
#   #
#   #
####

#####
#
#####
#
#####

#####
#
####
#
#

 ###
#   
#  ##
#   #
 ###

#   #
#   #
#####
#   #
#   #

###
 #
 #
 #
###

   #
   #
   #
#  #
 ##

#  #
# #
##
# #
#  #

#
#
#
#
####

#   #
## ##
# # #
# # #
#   #

#   #
##  #
# # #
#  ##
#   #

 ###
#   #
#   #
#   #
 ###

####
#   #
####
#
#

 ###
#   #
# # #
#  ##
 ###

####
#   #
####
# #
#  #

 ###
#   
 ###
    #
 ###

#####
  #
  #
  #
  #

#   #
#   #
#   #
#   #
 ###

#   #
#   #
#   #
 # #
  #

#   #
#   #
# # #
## ##
#   #

#   #
 # #
  #
 # #
#   #

#   #
 # #
  #
  #
  #

#####
   #
  #
 #
#####

 ###
  #
 # #
# # #
#   #

 # #
  #
 # #
# # #
#   #

#   #
 ###
#   #
#   #
 ###

 ##
   #
 ###
#  #
 ###

# 
#
###
#  #
###

 __
 ##
#
# 
 ##

   #
   #
 ###
#  #
 ###

 ##
#  #
###
#  
 ##

  ##
 #
###
 #
 #

 ###
#  #
 ###
   #
 ##

# 
# 
###
#  #
#  #

 #
__
##
 #
 #

  #
  _
  #
# #
 ##

#
#  #
###
#  #
#  #

 #
 #
#
#
##

__
## #
# # #
# # #
# # #

_  _
###
#  #
#  #
#  #

 ___
 ###
#   #
#   #
 ###

___
###
#  #
###
#

 ___
 ###
#  #
 ###
   #

___
###
#  #
#
#
      
 ####
#   
 ### 
    # 
####

 #
###
 #
 #
  #

_  _
#  #
#  #
#  #
 ##

__  _
#   #
#   #
 # #
  #

__
#       #
 #  #  #
  ## ##
  #   #

_   _
#  #
 ##
 # #
#   #

_   _
#   #
 # # 
  #
  #

 __
####
  ##
 ##
####

 ##
 __
 ###
#  #
####

#  #
 ##
   #
#  #
 ###

_  _
#  #
 ##
#  #
 ##

 ##
#  #
#  #
#  #
 ##
 
 #
##
 #
 #
 #

 ###
#   #
  ##
 #
#####
 
 ###
#   #
   #
#   #
 ###

  ##
 # #
####
   #
   #
   
####
#
####
    #
####

 ###
#
####
#   #
 ###

####
   #
 ##
  #
  #
  
 ###
#   #
 ###
#   #
 ###
 
 ###
#   #
 ####
    #
 ###

  #
 #
#
 #
  #

#
 #
  #
 #
#

___
###
___
###
___

 # #
#####
 # #
#####
 # #

    #
   #
  #
 #
#

#
 #
  #
   #
    # 

#
#
#
_
#

 ##
#  #
  #
  _
  #

_
_
###
_
_

_
_
_
_
#

.
FONT_DATA

my (%g_charmap, @g_font_faces, @g_face_widths);

sub init
{
	my $i = 0;
	for my $char (qw/A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Ã… Ã„ Ã– a b c d e f g h i 
j k l m n o p q r s t u v w x y z Ã¥ Ã¤ Ã¶ 0 1 2 3 4 5 6 7 8 9 < > = # \/ \ ! ? - ./)
	{
		$g_charmap{$char} = $i++;
	}
	my $lines = [];
	my $width = 0;
	for my $line (split(/\n/, $g_font_data))
	{
		chomp $line;
		$line =~ s/\s+$//g;
		if ($line eq "")
		{
			push @g_font_faces, $lines;
			push @g_face_widths, $width;
			$lines = [];
			$width = 0;
		}
		else
		{
			push @$lines, $line;
			$width = length $line if (length $line > $width);
		}
	}

	#print "my g_charmap =\n(";
	#while(my ($key, $value) = each(%g_charmap))
	#{
	#	print '  \'' . $key . '\' (' . ord($key) . ') => \'' . $value . '\'';
	#}
	#print ");";
}

sub cmd_wsay
{
	my ($input, $server, $witem) = @_;

	if (length($input) < 1)
	{
		Irssi::print("yo dawg i herd u liek short messages so i giev u long message so u can write short message 
while u read long message");
		return;
	}


	my @colors = ( 0, 2, 3, 4, 6, 7, 9, 10, 12, 13 );
	for (my $i = 0; $i < scalar(@colors); $i++)
	{
		my $idx = int(rand(scalar @colors));
		push(@colors, splice(@colors, $idx, 1));
	}
	my $color_idx = 0;
	my @output_rows = ( [], [], [], [], [] );
  my @inputarray = split(//, $input);
	for (my $ii = 0; ($ii < @inputarray); ++$ii)
	{
		my $char = $inputarray[$ii];
		# Auto-mangle UTF-8.
		if(ord($char) == 195)
		{
			$char = $char . $inputarray[++$ii];
		}
			
		if ($char eq ' ')
		{
			for (my $row = 0; $row < 5; $row++)
			{
				push @{$output_rows[$row]}, "   ";
			}
			next;
		}
		elsif (!exists($g_charmap{$char}))
		{
			print "wsay: unknown char: " . ord($char);
			next;
		}
		my $face_idx = $g_charmap{$char};
		my $color = sprintf("\003,%d", $colors[++$color_idx % scalar(@colors)]);
		for (my $row = 0; $row < 5; $row++)
		{
			my $line = $g_font_faces[$face_idx]->[$row];
			$line .= ' ' x ($g_face_widths[$face_idx] - length($line));
			$line =~ s/(^| )#/$1$color#/g;
			$line =~ s/#($| )/#\003$1/g;
			$line =~ s/[#_]/ /g;
			push @{$output_rows[$row]}, $line;
		}
	}

	for my $row (@output_rows)
	{
		my $msg = join(" ", @{$row});
		if ($witem && ($witem->{type} eq "QUERY" || $witem->{type} eq "CHANNEL"))
		{
			$witem->command("MSG ".$witem->{name}." ".$msg);
		}
		else
		{
			print $msg;
		}
	}
}

init();
Irssi::command_bind('wsay', 'cmd_wsay');
