#**************************************************************************
#   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::Commands::Intel;

use strict;
use warnings;
use feature ':5.10';

use Moose;
use MooseX::MethodAttributes;

sub sethostile
	: Help(Usage: .sethostile X:Y:Z)
	: ACL(irc_sethostile)
{
	my ($self,$c,$msg) = @_;
	my ($x,$y,$z) = $msg =~ /^(\d+)\D(\d+)\D(\d+)$/ or die 'ARGS';
	my $dbh = $c->model;

	my $findid = $dbh->prepare(q{SELECT planetid(?,?,?,tick())});
	my ($id) = $dbh->selectrow_array($findid,undef,$x,$y,$z);
	$dbh->begin_work;
	my $rv = $dbh->do(q{UPDATE planets SET planet_status = 'Hostile' WHERE id = $1}
		,undef,$id);
	if ($rv == 1){
		$c->reply("$x:$y:$z is now marked s hostile");
		$c->intel_log($id,"Set planet_status to: 'Hostile'");
	}
	$dbh->commit;
}

sub setnick
	: Help(Usage: .setnick X:Y:Z nick)
	: ACL(irc_setnick)
{
	my ($self,$c,$msg) = @_;
	my ($x,$y,$z,$nick) = $msg =~ /^(\d+)\D(\d+)\D(\d+) (\S+)$/ or die 'ARGS';
	my $dbh = $c->model;

	my $findid = $dbh->prepare(q{
SELECT nick, id, alliance_id FROM planets WHERE id = planetid(?,?,?,tick())
		});
	my $planet = $dbh->selectrow_hashref($findid,undef,$x,$y,$z);
	unless($planet->{id}){
		$c->reply("Couldn't find any planet with coords $x:$y:$z");
	}elsif($planet->{alliance_id} ~~ 1){
		$c->reply("This is an ND planet.");
	}else{
		$dbh->begin_work;
		$dbh->do(q{UPDATE planets SET nick = $1 WHERE id = $2}
			,undef,$nick,$planet->{id});
		if ($planet->{nick}){
			$c->reply("$x:$y:$z nick has been changed from <b>$planet->{nick}</b> to <b>$nick</b>");
		}else{
			$c->reply("$x:$y:$z nick has been set to $nick");
		}
		$c->intel_log($planet->{id},"Set nick to: $nick");
		$dbh->commit;
	}
}

sub setally
	: Help(Usage: .setally X:Y:Z ally | % can be used for wildcards \%-crew\% will match [F-Crew])
	: ACL(irc_setally)
{
	my ($self,$c,$msg) = @_;
	my ($x,$y,$z,$ally) = $msg =~ /^(\d+)\D(\d+)\D(\d+) (\S+)$/ or die 'ARGS';
	my $dbh = $c->model;

	my $aid;
	if ($ally ne 'unknown'){
		($aid,$ally) = $dbh->selectrow_array(q{
SELECT id,name FROM alliances WHERE name ILIKE ?
		},undef,$ally);
	}
	if ($aid ~~ 1){
		$c->reply("Don't set ND planets manually.");
	}elsif ($ally){
		my $findid = $dbh->prepare(q{
SELECT id,alliance,alliance_id FROM current_planet_stats WHERE x = ? AND y = ? and z = ?
		});
		my ($id,$alliance,$alliance_id) = $dbh->selectrow_array($findid,undef,$x,$y,$z);
		unless ($id){
			$c->reply("Couldn't find a planet at $x:$y:$z");
		}elsif ($alliance_id ~~ 1){
			$c->reply("$x:$y:$z is an ND planet.");
		}elsif ($alliance_id ~~ $aid){
			$c->reply("$x:$y:$z is already set to $ally");
		}else{
			$dbh->begin_work;
			$dbh->do(q{UPDATE planets SET alliance_id = $1 WHERE id = $2}
				,undef,$aid,$id);
			if (defined $alliance){
				$c->reply("Changed $x:$y:$z from <b>$alliance</b> to <b>$ally</b>");
			}else{
				$c->reply("Setting $x:$y:$z as <b>$ally</b>");
			}
			$c->intel_log($id,"Set alliance_id to: $aid ($ally)");
			$dbh->commit;
		}
	}else{
		$c->reply("Couldn't find such an alliance");
	}
}

1;
