#! /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 $includetransp = $cgi->param('includetransp') // 1;

my $pos = Position->from_fen($fen);
my ($json_root_pos, $root_aux_data) = get_json_move($pos, undef, $chld_in, $chld_out);

my $opening = $openings{$json_root_pos->{'opening_num'}} // 'A00: Start position';
my @json_moves = ($json_root_pos);

my $root_game;
eval {
	if (!exists($root_aux_data->{'pgn_file_number'}) ||
	    !exists($root_aux_data->{'pgn_start_position'})) {
		die "Missing PGN position data."
	}
	my $pgntext = read_root_pgn($root_aux_data->{'pgn_file_number'}, $root_aux_data->{'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 (@{$root_aux_data->{'moves'}}) {
	my ($np, $uci_move) = $pos->make_pretty_move($move);
	my $json_pos;
	if ($includetransp) {
		($json_pos, undef) = get_json_move($np, undef, $chld_in, $chld_out);

		# See if this move exists only due to transpositions.
		my ($alt_json_pos, undef) = get_json_move($np, $root_aux_data->{'pos_hash'}, $chld_in, $chld_out);
		if (!defined($alt_json_pos)) {
			$json_pos->{'transpose_only'} = 1;
		}
	} else {
		($json_pos, undef) = get_json_move($np, $root_aux_data->{'pos_hash'}, $chld_in, $chld_out);
	}
	$json_pos->{'move'} = $move;
	push @json_moves, $json_pos;
}

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 ($pgn_file_number, $pgn_start_position) = @_;
	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;
}

sub get_json_move {
	my ($pos, $filter_prev_pos_hash, $chld_in, $chld_out) = @_;
	my $bpfen_hex = unpack('H*', $pos->bitpacked_fen);
	my $prev_pos_hash_hex = '';
	if (defined($filter_prev_pos_hash)) {
		$prev_pos_hash_hex .= unpack('H*', pack('S', $filter_prev_pos_hash));
	}
	print $chld_in $bpfen_hex, "\n", $prev_pos_hash_hex, "\n";

	# Read the hash of this position.
	chomp (my $pos_hash = <$chld_out>);

	chomp (my $line = <$chld_out>);
	if ($line eq '-') {
		warn "Missing pos '" . $pos->fen . "' " . $filter_prev_pos_hash;
		return (undef, undef);
	}

	my ($white, $draw, $black, $computer, $opening_num, $white_sum_elo, $black_sum_elo, $num_elo, $timestamp, $pgn_file_number, $pgn_start_position, @moves) = split / /, $line;
	my $json_pos = {
		white => $white,
		draw => $draw,
		black => $black,
		computer => $computer,
		white_avg_elo => $num_elo == 0 ? undef : $white_sum_elo / $num_elo,
		black_avg_elo => $num_elo == 0 ? undef : $black_sum_elo / $num_elo,
		num_elo => $num_elo,
		opening_num => $opening_num,
	};
	my $aux_data = {  # Only relevant for the root.
		pos_hash => $pos_hash * 1,
		moves => \@moves,
		pgn_file_number => $pgn_file_number,
		pgn_start_position => $pgn_start_position,
	};
	return ($json_pos, $aux_data);
}
