#!/usr/bin/perl

# Games::Checkers, Copyright (C) 1996-2012 Mikhael Goikhman, migo@cpan.org
#
# 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 3 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, see <http://www.gnu.org/licenses/>.

# Try --help for usage.

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../lib";
use Getopt::Long qw(:config no_ignore_case bundling);

use Games::Checkers::PDNParser;
use Games::Checkers::Game;

my $script_name = ($0 =~ m:([^/]+)$:, $1);
my $delay = 1;
my $break = 10;
my $game_start = 1;
my $use_term = 0;  # 0 means detect automatically
my $dumb_term = 0;
my $dumb_chars = 0;
my $fullscreen = 0;
my $variant = undef;  # means auto-detect

sub show_help (;$) {
	my $is_error = shift || 0;
	my $out = $is_error ? \*STDERR : \*STDOUT;
	my $usage = qq{
		Usage: $script_name [OPTIONS] file.pdn
		The automatic replay of recorded Checkers games from PDN file.

		Options:
			-h --help          show this help and exit
			-d --delay N       pause in seconds between the moves (default: $delay)
			-b --break N       pause in seconds between the games (default: $break)
			-s --start N       skip N-1 games, start with N's game
			-t --use-term      use terminal even if graphical frontend is available
			-T --dumb-term     do not position terminal cursor
			-C --dumb-chars    do not use fancy drawing characters
			-f --fullscreen    start in fullscreen mode (incompatible with -t)
			-v --variant NAME  'russian', 'english', 'spanish', 'international' etc.

		Usually the game variant is auto-detected (either from GameType tag in PDN
		or games/variant subdir), but you may also force it using --variant.
	};
	$usage =~ s/^\n//; $usage =~ s/^\t\t?//mg;
	print $out $usage;
	exit $is_error;
}

GetOptions(
	"h|help"        => sub { show_help(0) },
	"d|delay=s"     => \$delay,
	"b|break=s"     => \$break,
	"s|start=i"     => \$game_start,
	"t|use-term!"   => \$use_term,
	"T|dumb-term!"  => \$dumb_term,
	"C|dumb-chars!" => \$dumb_chars,
	"f|fullscreen!" => \$fullscreen,
	"v|variant=s"   => \$variant,
) || show_help(1);

my $games_dir = "$FindBin::Bin/../data/games";
$games_dir = "$FindBin::Bin/../share/pcheckers/games"
	unless -d $games_dir;
my $filename = shift;
$filename = "default/default.pdn" if !$filename && -t;
$filename ||= "-" unless -t;
show_help() unless $filename;
$filename = "$games_dir/$filename" if $filename =~ m!^\w+\/[^\/]+$!;

($variant) = $filename =~ m!\bgames/(\w+)/[^\/]+$! unless $variant;

my $pdn_parser = Games::Checkers::PDNParser->new($filename, $variant);

my $game_count = 0;
while (my $pdn_record = $pdn_parser->next_record) {
	$game_count++;
	next if $game_count < $game_start;
	my ($move_verges, $values, $variant, $board) = @$pdn_record;

	my $game = Games::Checkers::Game->new(
		variant => $variant,
		board => $board,
		title => ($values->{White} || "???") . ' - ' . ($values->{Black} || "???"),
		description => join("\n", map {
			$values->{$_} ? "$_: $values->{$_}" : ()
		} qw(Event Site Date Round Game)),
		use_term => $use_term,
		dumb_term => $dumb_term,
		dumb_chars => $dumb_chars,
		fullscreen => $fullscreen,
	);

	$game->show_board;

	while (@$move_verges) {
		$game->sleep($delay);

		my $move_verge = shift @$move_verges;
		my ($is_beat, $src, @dsts) = @$move_verge;

		my $move = $game->create_move($is_beat, $src, @dsts);
		die "Corrupt game #$game_count record? Move ($src @dsts) can't be created\n"
			unless $move;

		$game->show_move($move);
		$game->show_board;
	}

	$game->show_result_code($values->{Result}, $break);
}
