#**************************************************************************
#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
#                                                                         *
#   This program is free software; you can redistribute it and/or modify  *
#   it under the terms of the GNU General Public License as published by  *
#   the Free Software Foundation; either version 2 of the License, or     *
#   (at your option) any later version.                                   *
#                                                                         *
#   This program is distributed in the hope that it will be useful,       *
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
#   GNU General Public License for more details.                          *
#                                                                         *
#   You should have received a copy of the GNU General Public License     *
#   along with this program; if not, write to the                         *
#   Free Software Foundation, Inc.,                                       *
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
#**************************************************************************/

package NDIRC::IrcContext;
use strict;
use warnings;
use feature ':5.10';

use Moose;
use namespace::autoclean;
extends "NDIRC::Context";

use Set::Object ();

has host => (
	is => 'ro',
	isa => 'Str',
	required => 1
);

has nick => (
	is => 'ro',
	isa => 'Str',
	required => 1
);

has server => (
	is => 'ro',
	isa => 'Object',
	required => 1
);

has reply_string => (
	is => 'ro',
	isa => 'Str',
	required => 1,
);

sub reply {
	my ($self,$msg) = @_;

	my @command = split / /, $self->reply_string;
	$self->message(@command, $msg);
}

sub message {
	my ($self,$command, $target, $msg) = @_;

	$msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
	$msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;

	#Split the message, using the, slightly modified, algorithm from splitlong.pl in the irssi distribution.
	if ($command eq 'privmsg'){
		my $lend = ' ...';
		my $lstart = '... ';
		my $maxlength = $self->server->{msg_length} - bytes::length("privmsg $target :" . $self->server->nick_name());
		my $maxlength2 = $maxlength - bytes::length($lend);

		if (bytes::length($msg) > ($maxlength)) {
			my @spltarr;

			while (bytes::length($msg) > ($maxlength)) {
				my $pos = rindex($msg, " ", $maxlength2);
				push @spltarr, substr($msg, 0, ($pos < ($maxlength/10 + 4)) ? $maxlength2  : $pos)  . $lend;
				$msg = $lstart . substr($msg, ($pos < ($maxlength/10 + 4)) ? $maxlength2 : $pos+1);
			}

			push @spltarr, $msg;
			for (@spltarr) {
				$self->command($command, $target, $_);
			}
			return;
		}
	}
	$self->command($command, $target, $msg);
}

sub command {
	my ($self,@command) = @_;

	$self->server->yield(@command);
}

sub _build_uid {
	my ($self) = @_;

	my $query = $self->model->prepare(q{
SELECT uid FROM users
WHERE hostmask = $1
		});
	$query->execute($self->host);

	if (my ($uid) = $query->fetchrow_array){
		$query->finish;
		return $uid;
	}
	return -4;
}

sub valuecolor {
	shift @_;
	my $s = $_;
	$s = $_[1] if $#_ >= 1;
	$s = "" unless defined $s;
	return "<c05>$s</c>" if $s eq 'Hostile';
	return "<c03>$s</c>" if $s eq 'Friendly';
	return "<c12>$s</c>" if $s eq 'Nap' or $s eq 'NAP';
	return "<b>$s</b>" if $_[0];
	return $s;
}

__PACKAGE__->meta->make_immutable;
1;
