#**************************************************************************
#   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 NDWeb::Scans;
use strict;
use warnings;
require Exporter;

our @ISA = qw/Exporter/;

our @EXPORT = qw/parseMilScan doMilScan doPlanetScan/;

my %classes = (Fighter => 'Fi', Corvette => 'Co', Frigate => 'Fr', Destroyer => 'De', Cruiser => 'Cr', Battleship => 'Bs', Structure => 'St', Roids => 'Ro', Resources => 'Re', '-' => '-');

sub parseMilScan ($) {
	my ($file) = @_;
	my @fleets;

	while ($file =~ m{<th class="center">([^<]+)</th>}g) {
		push @fleets, {name => $1, ships => []};
	}

	while ($file =~ m{<tr><td class="left">([^<]+)</td>(.+?)</tr>}g) {
		my $ship = $1;
		next if $ship eq 'Total Ships';
		my $amounts = $2;
		my $i = 0;
		while ($amounts =~ m{<td class="center">([\d,]+)</td>}g) {
			my $fleet = $fleets[$i];
			my $amount = $1;
			$amount =~ s/,//g;
			if ($ship eq 'Total Visible Ships') {
				$fleet->{amount} = $amount;
			} elsif ($amount > 0) {
				push @{$fleet->{ships}}, {ship => $ship, amount => $amount};
			}
			++$i;
		}
	}

	return @fleets;
}

my $addfleet_sql = q{INSERT INTO fleets (name, mission, pid, tick, amount) VALUES(?,?,?,?,?) RETURNING fid};
my $addships_sql = q{INSERT INTO fleet_ships (fid, ship, amount) VALUES(?,?,?)};
my $fleetscan_sql = q{INSERT INTO fleet_scans (fid, id) VALUES(?,?)};

sub doMilScan ($$$) {
	my ($dbh, $scan,$file) = @_;

	my $addfleet = $dbh->prepare_cached($addfleet_sql);
	my $fleetscan = $dbh->prepare_cached($fleetscan_sql);
	my $addships = $dbh->prepare_cached($addships_sql);

	my @fleets = parseMilScan($file);
	for my $fleet (@fleets) {
		next if $fleet->{amount} == 0;
		$addfleet->execute($fleet->{name},$scan->{type},$scan->{pid}
			,$scan->{tick}, $fleet->{amount});
		my ($id) = $addfleet->fetchrow_array;
		$fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
		for my $s (@{$fleet->{ships}}){
			$addships->execute($id, $s->{ship}, $s->{amount}) or die $dbh->errstr;
		}
	}
}

my $addplanetscan_sql = q{INSERT INTO planet_scans
	(id,tick,pid,metal_roids,metal,crystal_roids,crystal,eonium_roids,eonium
		,agents,guards,light,medium,heavy,hidden)
	VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)};

sub doPlanetScan ($$$) {
	my ($dbh, $scan,$file) = @_;

	my $addplanetscan = $dbh->prepare_cached($addplanetscan_sql);

	my @values = ($scan->{id},$scan->{tick},$scan->{pid});
	$file =~ s/(\d),(\d)/$1$2/g;

	while($file =~ m{"center">(Metal|Crystal|Eonium)</td>\D+(\d+)\D+([\d,]+)}g){
		push @values,$2,$3;
	}
	if($file =~ m{Security\ Guards .+? "center">(\d+)</td>
			.+? "center">(\d+)</td>}sx){
		push @values,$1,$2;
	}
	if($file =~ m{<td class="center">([A-Z][a-z]+)</td><td class="center">([A-Z][a-z]+)</td><td class="center">([A-Z][a-z]+)</td>}){
		push @values,$1,$2,$3;
	}
	if($file =~ m{<span class="superhighlight">([\d,]+)</span>}){
		push @values,$1;
	}
	$addplanetscan->execute(@values);
}

