#**************************************************************************
#   Copyright (C) 2006 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::Def;
use strict;
use warnings;
use ND::DB;
use ND::Include;
use NDIRC::Access;
use NDIRC::Misc;
use CGI qw/:standard/;
require Exporter;

our @ISA = qw/Exporter/;

our @EXPORT = qw/showCall takeCall covCall ignoreCall
	setDefPrio setCalc getCalc/;

sub showCall {
	my ($msg,$command) = @_;
	my ($id);
	if (defined $msg && $msg =~ /^(\d+)$/){
		$id = $1;
	}else{
		$ND::server->command("notice $ND::nick Usage: $command callid");
		return;
	}
	if (dc()){
		my $f = $ND::DBH->prepare(<<SQL
		SELECT i.id,coords(p.x,p.y,p.z), p.planet_status,p.nick, p.alliance, p.race,i.eta,i.amount,i.fleet,i.shiptype,p.relationship,c.landing_tick - (SELECT value::integer FROM misc WHERE id = 'TICK')
		FROM incomings i
			JOIN calls c ON i.call = c.id
				JOIN current_planet_stats p ON i.sender = p.id
				WHERE i.call = ? 
				ORDER BY p.x,p.y,p.z;
SQL
);
		$f->execute($id);
		while (my @row = $f->fetchrow()){
			@row = map (valuecolor(0),@row);
			$ND::server->command("notice $ND::nick (CALL $id) $row[0]: $row[1], $row[3] ($row[2]), $row[4] ($row[10]), $row[5], ETA: $row[11](/$row[6]), Amount: $row[7],  $row[8], Type: $row[9]");
		}
	}
}

sub setCalc {
	my ($msg,$command) = @_;
	my ($id, $calc);
	if (defined $msg && $msg =~ /^(\d+) (.+)$/){
		$id = $1;
		$calc = escapeHTML($2);
	}else{
		$ND::server->command("notice $ND::nick Usage: $command callid calc");
		return;
	}
	if (my $user = dc()){
		if ($ND::DBH->do(q{UPDATE calls SET calc = $2 WHERE id = $1}
				,undef,$id, $calc) == 1){
			$ND::server->command("notice $ND::nick Updated calc call $ND::B$id$ND::O");
			def_log $user->{uid}, $id , 'Updated calc to: [URL]'
				.$calc.'[/URL]';
		}
	}
}

sub getCalc {
	my ($msg,$command) = @_;
	my ($id);
	if (defined $msg && $msg =~ /^(\d+)$/){
		$id = $1;
	}else{
		$ND::server->command("notice $ND::nick Usage: $command callid");
		return;
	}
	if (my $user = dc()){
		my $calc = $ND::DBH->selectrow_array(q{SELECT calc
			FROM calls WHERE id = $1}
			,undef,$id);
		$ND::server->command("notice $ND::nick calc for call $ND::B$id$ND::O: $calc");
	}
}

sub setDefPrio {
	my ($msg,$command) = @_;
	my ($min,$max);
	if (defined $msg && $msg =~ /^(\d+)\D(\d+)$/){
		$min = $1;
		$max = $2;
	}else{
		$ND::server->command("notice $ND::nick Usage: $command Min Max");
		return;
	}
	if (hc()){
		$ND::DBH->begin_work;
		my $update = $ND::DBH->prepare('UPDATE misc SET value = ? :: int WHERE id = ?');
		$update->execute($min,'DEFMIN');
		$update->execute($max,'DEFMAX');
		if ($ND::DBH->commit){
			$ND::server->command("msg $ND::target min def prio set to $ND::B$min$ND::B and max set to $ND::B$max");
		}else{
			$ND::server->command("msg $ND::target something went wrong");
		}
	}
}

1;
