#**************************************************************************
#   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::Intel;
use strict;
use warnings;
use ND::DB;
use NDIRC::Access;
use NDIRC::Misc;
use ND::Include;
require Exporter;

our @ISA = qw/Exporter/;

our @EXPORT = qw/setHostile setNick setAlly setChannel/;

sub setHostile {
	my ($msg, $command) = @_;
	my ($x,$y,$z);
	if(defined $msg && $msg =~ /^(\d+)\D(\d+)\D(\d+)$/){
		$x = $1;
		$y = $2;
		$z = $3;
	}else{
		$ND::server->command("notice $ND::nick Usage: .$command X:Y:Z");
		return;
	}
	if(my $user = dc()){
		my $findid = $ND::DBH->prepare_cached(q{SELECT planetid(?,?,?,tick())});
		my ($id) = $ND::DBH->selectrow_array($findid,undef,$x,$y,$z);
		my $rv = $ND::DBH->do(q{UPDATE planets SET planet_status = 'Hostile' WHERE id = $1},undef,$id);
		if ($rv == 1){
			$ND::server->command("msg $ND::target $x:$y:$z is now marked s hostile");
			intel_log $user->{uid},$id,"Set planet_status to: 'Hostile'";
		}
	}
}

sub setNick {
	my ($msg, $command) = @_;
	my ($x,$y,$z,$nick);
	if(defined $msg && $msg =~ /^(\d+)\D(\d+)\D(\d+) (\S+)$/){
		$x = $1;
		$y = $2;
		$z = $3;
		$nick = $4;
	}else{
		$ND::server->command("notice $ND::nick Usage: .$command X:Y:Z nick");
		return;
	}
	if (my $user = officer){
		my $findid = $ND::DBH->prepare_cached(q{SELECT nick, id FROM planets
			WHERE id = planetid(?,?,?,tick())});
		my ($oldnick,$id) = $ND::DBH->selectrow_array($findid,undef,$x,$y,$z);
		if ($ND::DBH->do('UPDATE planets SET nick = $1 WHERE id = $2'
				,undef,$nick,$id)){
			unless (defined $oldnick){
				$ND::server->command("msg $ND::target $x:$y:$z nick has been set to $ND::B$nick");
			}else{
				$ND::server->command("msg $ND::target $x:$y:$z nick has been changed from $ND::B$oldnick$ND::B to $ND::B$nick");
			}
			intel_log $user->{uid},$id,"Set nick to: $nick";
		}
	}
}

sub setAlly {
	my ($msg, $command) = @_;
	my ($x,$y,$z,$ally);
	if(defined $msg && $msg =~ /^(\d+)\D(\d+)\D(\d+) (\S+)$/){
		$x = $1;
		$y = $2;
		$z = $3;
		$ally = $4;
	}else{
		$ND::server->command("notice $ND::nick Usage: .$command X:Y:Z ally | | % can be used for wildcards \%-crew\% will match [F-Crew]");
		return;
	}
	if (my $user = officer){
		my $aid;
		if ($ally ne 'unknown'){
			($aid,$ally) = $ND::DBH->selectrow_array("SELECT id,name FROM alliances WHERE name ILIKE ?",undef,$ally);
		}
		if ($ally){
			my $findid = $ND::DBH->prepare_cached(q{SELECT id,alliance FROM current_planet_stats 
				WHERE x = ? AND y = ? and z = ?});
			my ($id,$alliance) = $ND::DBH->selectrow_array($findid,undef,$x,$y,$z);
			if($id && $ND::DBH->do('UPDATE planets SET alliance_id = $1 WHERE id = $2'
				,undef,$aid,$id)){
				if (defined $alliance){
					$ND::server->command("msg $ND::target Changed $x:$y:$z from $ND::B$alliance$ND::B to $ND::B$ally");
				}else{
					$ND::server->command("msg $ND::target Setting $x:$y:$z as $ND::B$ally");
				}
				intel_log $user->{uid},$id,"Set alliance_id to: $aid ($ally)";
			}else{
				$ND::server->command("msg $ND::target Couldn't find a planet at $x:$y:$z");
			}
		}else{
			$ND::server->command("msg $ND::target Couldn't find such an alliance");
		}
	}
}

sub setChannel {
	my ($msg, $command) = @_;
	my ($x,$y,$z,$channel);
	if(defined $msg && $msg =~ /^(\d+)\D(\d+)\D(\d+) (\S+)$/){
		$x = $1;
		$y = $2;
		$z = $3;
		$channel = $4;
	}else{
		$ND::server->command("notice $ND::nick Usage: .$command X:Y:Z channel");
		return;
	}
	if (my $user = officer()){
		my $findid = $ND::DBH->prepare_cached(q{SELECT id,channel FROM current_planet_stats 
				WHERE x = ? AND y = ? and z = ?});
		my ($id,$c) = $ND::DBH->selectrow_array($findid,undef,$x,$y,$z);
		if ($ND::DBH->do('UPDATE planets SET channel = $1 WHERE id = $2'
				,undef,$channel,$id)){

			if (defined $c){
				$ND::server->command("msg $ND::target Changed $x:$y:$z from $ND::B$c$ND::B to $ND::B$channel");
			}else{
				$ND::server->command("msg $ND::target Setting $x:$y:$z as $ND::B$channel");
			}
			intel_log $user->{uid},$id,"Set channel to: $channel";
		}else{
			$ND::server->command("msg $ND::target Couldn't find a planet at $x:$y:$z");
		}
	}
}

1;
