/*
 *    interface.c
 *      Rozhrani s uzivatelem
 *
 */

#include<stdio.h>
#include"data.h"

extern TTah historie[MAXPOCTAH];	/* 1.tah = [1] */
extern int IQ, SIQ;

void TiskTahu(TMG mg, int p)
{
	int i;
	char t[5];
	printf("Moves possible (%d): ", p);
	for (i = 0; i < p; i++) {
		Num2Not(mg[i].odkud, &t[0]);
		Num2Not(mg[i].kam, &t[2]);
		printf("%s ", t);
	}
	printf("\n");
}

void TiskSach(THra H)
{
	int x, y;
	printf(" \n  -----------------------------------------\n");
	for (y = 7; y >= 0; y--) {
		printf("%d |", y + 1);
		for (x = 0; x <= 7; x++)
			switch (H.sachovnice[x][y]) {
			case PPOL:
				printf("    |");
				break;
			case BPES:
				printf("  P |");
				break;
			case BSTR:
				printf("  B |");
				break;
			case BJEZ:
				printf("  N |");
				break;
			case BVEZ:
				printf("  T |");
				break;
			case BDAM:
				printf("  Q |");
				break;
			case BKRA:
				printf("  K |");
				break;
			case CPES:
				printf(" *P |");
				break;
			case CSTR:
				printf(" *B |");
				break;
			case CJEZ:
				printf(" *N |");
				break;
			case CVEZ:
				printf(" *T |");
				break;
			case CDAM:
				printf(" *Q |");
				break;
			case CKRA:
				printf(" *K |");
				break;
			default:
				printf("  F");
			}
		printf(" \n  -----------------------------------------\n");
	}
	printf("     a    b    c    d    e    f    g    h \n");
}

void TiskHist(THra H)
{
	int i;
	char odc[3], doc[3];
	for (i = 1; i <= H.PHT; i++) {
		Num2Not(historie[i].odkud, odc);
		Num2Not(historie[i].kam, doc);
		if (i % 2 == 1)
			printf("%d. %2.2s-%2.2s", (i + 1) / 2, odc, doc);
		else
			printf("  %2.2s-%2.2s\n", odc, doc);
	}
	if (H.PHT % 2 == 1)
		printf("\n");
}

void TiskFig(THra H)
/* vytiskne pozice vsech figur */
{
	int i, x;
	char notpoz[3];
	THrac *hrac;
	printf("DEBUG: Pozice vsech figur:\n");
	hrac = &(H.bily);
	printf(" BILY: ");
	for (x = 0; x <= 1; x++) {
		for (i = 0; i <= 15; i++) {
			switch (hrac->figury[i].fig) {
			case BPES:
				printf("  P");
				break;
			case BSTR:
				printf("  S");
				break;
			case BJEZ:
				printf("  J");
				break;
			case BVEZ:
				printf("  V");
				break;
			case BDAM:
				printf("  Q");
				break;
			case BKRA:
				printf("  K");
				break;
			case CPES:
				printf(" *P");
				break;
			case CSTR:
				printf(" *S");
				break;
			case CJEZ:
				printf(" *J");
				break;
			case CVEZ:
				printf(" *V");
				break;
			case CDAM:
				printf(" *Q");
				break;
			case CKRA:
				printf(" *K");
				break;
			}
			if ((hrac->figury[i].fig) != PPOL) {
				Num2Not(hrac->figury[i].pos, notpoz);
				printf(" :%s ; ", notpoz);
			}
		}		/* for i */
		if (x == 0) {
			hrac = &(H.cerny);
			printf(" \nCERNY: ");
		}
		/* prohodit hrace + newline */
		else
			printf(" \n");
	}			/* for x */
}

void Num2Not(TPozice p, char *ch)
{
	ch[0] = (char) (p.x + (int) ('a'));
	ch[1] = (char) (p.y + (int) ('1'));
	ch[2] = '\0';
}

void Not2Num(char *ch, TPozice * p)
{
	p->x = (int) (ch[0] - 'a');
	p->y = (int) (ch[1] - '1');
}

void KonecHry(void)
{
	printf(" quiting ...\n");
}

/* soubory */

int ReadCFG(void)
{
	int k, l;
	FILE *f;

	f = fopen(CFGFILE, "rt");
	if (f == NULL) {
		printf("Warning: error reading configuration file %s: ", CFGFILE);
		perror(NULL);
		printf("Using default values\n");
		return 0;
	} else {
		fscanf(f, "%d %d", &k, &l);
		if (k > 0 && l <= 0) {
			IQ = k;
			SIQ = l;
		} else {
			printf("Incorrect values in %s", CFGFILE);
			return 0;
		}
	}
	return 1;
}

void SaveFile(THra * H, char *fname)
{
	FILE *f;
	int i;
	char t[5];
	f = fopen(fname, "wt");
	if (f == NULL)
		perror("Error saving file");
	else {
		fprintf(f, "%d\n", H->PHT);
		for (i = 1; i <= H->PHT; i++) {
			Num2Not(historie[i].odkud, &t[0]);
			Num2Not(historie[i].kam, &t[2]);
			fprintf(f, "%d %s\n", i, t);
		}
		fclose(f);
		printf("%s written.\n", fname);
	}
}

void OpenFile(THra * H, char *fname)
{
	FILE *f;
	int i, j, pht;
	char t[5];
	HraInic(H);
	f = fopen(fname, "rt");
	if (f == NULL)
		perror("Error opening file");
	else {
		fscanf(f, "%d\n", &pht);	/* j -> zahodit ... */
		for (i = 1; i <= pht; i++) {
			if (!feof(f)) {
				fscanf(f, "%d %s\n", &j, t);
				if (j != i)
					printf("%d: Nonconsistent data\n", i);
				Not2Num(&(t[0]), &(historie[i].odkud));
				Not2Num(&(t[2]), &(historie[i].kam));
				ZpracujTah(t, H);
			} else
				printf("%d: Nonconsistent data\n", i);
		}
		fclose(f);
		printf("%s opened.\n", fname);
	}
}
