/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of SanKit -- A multiplatform set of C code
 * for reading and writing PGN formatted chess games. In addition
 * it can be used as an engine to determine in real time if proposed
 * moves are legal or not. It appears to generate near perfect PGN
 * from incorrectly formatted files.
 *
 * Copyright 2012 David Hoerl All Rights Reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

extern "C"
{
	#include "sandef.h"
	#include "sanmnd.h"
	#include "sanvar.h"

	#include "sanint.h"
	#include "sandci.h"
	#include "sanmpu.h"
	#include "santrm.h"

	#include "sanenu.h"

	#include "sanutl.h"
	#include "sanpgn.h"

	#include "sanrdr.h"
	#include "santag.h"

	#include "sangtc.h"
}

#include "PGN_Parser.h"

using namespace std;
using namespace SANKit;

void dumpGame(Game& g);

int main (int argc, const char *argv[])
{
	if(argc != 2) {
		fprintf(stderr, "%s usage: %s input_file result_file\n", argv[0], argv[0]);
		exit(1);
	}

	const char *boards = "/tmp/PGNmoves.txt";
    SANInit();

    optnv[optn_nabd] = 0;	// no auto board display
    optnv[optn_namd] = 1;	// no auto move display
    optnv[optn_napr] = 1;	// no auto program reply
    optnv[optn_emde] = 1;	// extraordinary move disambiguation effort

	unlink(boards);
	do_fptr = fopen(boards, "w+");
	assert(do_fptr);

	st_fptr = NULL;

    siT n = 0;
    bool ret = SANRGSingle((const charptrT)argv[1], &n);
	assert(ret);
	
	printf("HALF MOVES hn=%d\n", hn);
	if(ret) {
		fseek(do_fptr, 0, SEEK_SET); // reuse same file
		
		FILE *fptr = fopen((const charptrT)argv[1], "r"); // sankit closed it, so reopen it
		assert(fptr);
		
		Game game(fptr, do_fptr); // parse game
		fclose(fptr);

		dumpGame(game); // whatever you want to do
	} else {
		//printf("Error: failed after game %u at this line %s.\n", gameCount, parseError);
	}
	fclose(do_fptr);

	return 0;
}

void dumpGame(Game& g)
{
	printf("HEADER:\n%s\n", g.GetHeader().c_str());
	
	size_t halves = g.GetHalfMoveCount();
	
	printf("%s\n", g.GetNewBoard().c_str());
	
	for(size_t i=0; i<halves; ++i) {
		printf("HALF MOVE %zd:\n%s\n\n", i+1, g.GetBoard(i/2, (i&1) ? false : true).c_str() );
	}
	
	// so lets reconstruct the last board by getting each piece
	
	char loc[3] = { ' ', ' ', 0 };
	
	size_t full_moves = g.GetFullMoveCount();
	
	printf("Lets look at last board again using the move/piece API.\n");
	for(char r='8'; r >= '1'; --r) {
		for(char f = 'a'; f <= 'h'; ++f) {
			loc[0] = f; loc[1] = r;
			
			piece p = g.GetPiece(full_moves-1, (halves&1) ? 1 : 0, loc);

#if 0 // if you want to make it exactly the same change 0 to 1
			const char *str;
			if(p == empty) {
				int x = (r - '1') & 1;
				int y = (f - 'a') & 1;
				str = (x ^ y) ? "  " : "::";
			} else {
				str = g.StringForPiece(p);
			}
			printf("%s ", str);
#else
			printf("%s ", g.StringForPiece(p));
#endif
		}
		printf("\n");
	}
}
