#! /usr/bin/perl
use strict;
use warnings;
use CGI;
use JSON::XS;
use lib '..';
use Position;
use IPC::Open2;
use Chess::PGN::Parse;

our %openings = ();
read_openings();

my $cgi = CGI->new;
my ($chld_out, $chld_in);
my $pid = IPC::Open2::open2($chld_out, $chld_in, "../binlookup", "../open.mtbl", "40");

# Root position. Basically ignore everything except the opening (and later some root game stuff).
my $fen = $cgi->param('fen') // 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
my $pos = Position->from_fen($fen);
my $hex = unpack('H*', $pos->bitpacked_fen);
print $chld_in $hex, "\n";
chomp (my $line = <$chld_out>);

my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo, $timestamp, $pgn_file_number, $pgn_start_position, @moves) = split / /, $line;
my @json_moves = ();
push @json_moves, {
	white => $white * 1,
	draw => $draw * 1,
	black => $black * 1,
	white_avg_elo => $num_elo == 0 ? undef : $white_avg_elo * 1,
	black_avg_elo => $num_elo == 0 ? undef : $black_avg_elo * 1,
	num_elo => $num_elo * 1
};

my $opening = $openings{$opening_num} // 'A00: Start position';

my $root_game;
eval {
	die "Missing PGN position data." if (!defined($pgn_file_number) || !defined($pgn_start_position));
	my $pgntext = read_root_pgn($pgn_file_number, $pgn_start_position);
	my $pgn = Chess::PGN::Parse->new(undef, $pgntext);
	$pgn->read_game() or die;
	$pgn->parse_game() or die;

	my $tags = $pgn->tags;
	$root_game = {};
	$root_game->{'white'} = $pgn->white;
	$root_game->{'white_elo'} = $tags->{'WhiteElo'};
	$root_game->{'black'} = $pgn->black;
	$root_game->{'black_elo'} = $tags->{'BlackElo'};
	$root_game->{'event'} = $pgn->event;
	$root_game->{'date'} = $pgn->date;
	$root_game->{'result'} = $pgn->result;
	$root_game->{'eco'} = $pgn->eco;
	$root_game->{'moves'} = int(((scalar @{$pgn->moves}) + 1) / 2);
};

# Explore one move out.
for my $move (@moves) {
	my ($np, $uci_move) = $pos->make_pretty_move($move);
	my $hex = unpack('H*', $np->bitpacked_fen);
	print $chld_in $hex, "\n";
	my $line = <$chld_out>;
	my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo) = split / /, $line;
	push @json_moves, {
		move => $move,
		white => $white * 1,
		draw => $draw * 1,
		black => $black * 1,
		white_avg_elo => $num_elo == 0 ? undef : $white_avg_elo * 1,
		black_avg_elo => $num_elo == 0 ? undef : $black_avg_elo * 1,
		num_elo => $num_elo * 1
	};
}

print $cgi->header(-type=>'application/json');
print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_game => $root_game });

sub read_openings {
	open my $fh, "../openings.txt"
		or die "../openings.txt: $!";
	for my $line (<$fh>) {
		chomp $line;
		my ($hash, $eco, $opening, $variation, $subvariation) = split /\t/, $line;
		if ($variation eq '') {
			$openings{$hash} = $eco . ": " . $opening;
		} else {
			$openings{$hash} = $eco . ": " . $opening . ": " . $variation;
		}
	}
	close $fh;
}

sub read_root_pgn {
	my @pgnnames;
	open my $pgnnamesfh, "<", "../pgnnames.txt"
		or die "../pgnnames.txt: $!";
	while (<$pgnnamesfh>) {
		chomp;
		push @pgnnames, $_;
	}
	close $pgnnamesfh;

	if ($pgn_file_number > $#pgnnames) {
		die "Unknown PGN file number $pgn_file_number";
	}

	my $root_pgn;
	open my $pgnfh, "<", "../" . $pgnnames[$pgn_file_number]
		or die $pgnnames[$pgn_file_number] . ": $!";
	sysseek($pgnfh, $pgn_start_position, 0)
		or die "Could not seek to $pgn_start_position: $!";
	sysread($pgnfh, $root_pgn, 32768)
		or die "Could not read PGN from $pgn_start_position at $pgnnames[$pgn_file_number]: $!";
	close $pgnfh;
	$root_pgn =~ s/^.*?(\[Event )/$1/s;
	$root_pgn =~ s/^(.+?)\[Event .*/$1/s;

	return $root_pgn;
}
