/*
 * @(#)CubesU.c
 *
 * Copyright 1994 - 2023  David A. Bagley, bagleyd AT verizon.net
 *
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of the author not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * 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.
 */

/* Undo algorithm for Cubes */

#include "CubesP.h"

static int startSpace;
int *startPosition = NULL;

static void
newStack(CubesStack *s)
{
	if (s->lastMove != NULL || s->firstMove != NULL)
		return;
	if (!(s->lastMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	if (!(s->firstMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	s->firstMove->previous = s->lastMove->next = NULL;
	s->firstMove->next = s->lastMove;
	s->lastMove->previous = s->firstMove;
	s->count = 0;
}

static void
pushStack(CubesStack *s, MoveRecord **move)
{
	if (!(s->currMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	s->lastMove->previous->next = s->currMove;
	s->currMove->previous = s->lastMove->previous;
	s->currMove->next = s->lastMove;
	s->lastMove->previous = s->currMove;
	*move = &(s->currMove->move);
	s->count++;
}

static void
popStack(CubesStack *s)
{
	s->currMove = s->lastMove->previous;
	s->lastMove->previous->previous->next = s->lastMove;
	s->lastMove->previous = s->lastMove->previous->previous;
	free(s->currMove);
	s->count--;
}


static MoveRecord *
topStack(CubesStack *s)
{
	return &(s->lastMove->previous->move);
}

static int
emptyStack(CubesStack *s)
{
	return (s->lastMove->previous == s->firstMove);
}

static void
flushStack(CubesStack *s)
{
	while (s->lastMove->previous != s->firstMove) {
		s->currMove = s->lastMove->previous;
		s->lastMove->previous->previous->next = s->lastMove;
		s->lastMove->previous = s->lastMove->previous->previous;
		free(s->currMove);
	}
	s->count = 0;
}

static void
deleteStack(CubesStack *s)
{
	flushStack(s);
	if (s->firstMove) {
		free(s->firstMove);
		s->firstMove = NULL;
	}
	if (s->lastMove) {
		free(s->lastMove);
		s->lastMove = NULL;
	}
}

/**********************************/

void
newMoves(CubesStack *s)
{
	newStack(s);
}

void
deleteMoves(CubesStack *s)
{
	deleteStack(s);
}

static void
writeMove(MoveRecord *move, int direction)
{
#if 0
	move->direction = direction;
#endif
	move->packed = (unsigned char) (direction & 0xF);
}

static void
readMove(MoveRecord *move, int *direction)
{
#if 0
	*direction = move->direction;
#endif
	*direction = (int) (move->packed & 0xF);
}

void
setMove(CubesStack *s, int direction)
{
	MoveRecord *move;

	pushStack(s, &move);
	writeMove(move, direction);
}

void
getMove(CubesStack *s, int *direction)
{
	readMove(topStack(s), direction);
	popStack(s);
}

int
madeMoves(CubesStack *s)
{
	return !emptyStack(s);
}

void
flushMoves(CubesWidget w, CubesStack *s, Boolean undo)
{
	int i;

	flushStack(s);
	if (undo) {
		startSpace = w->cubes.spacePosition;
		startPosition[w->cubes.sizeBlock - 1] = 0;
		for (i = 0; i < w->cubes.sizeBlock; i++)
			startPosition[i] = w->cubes.blockOfPosition[i];
	}
}

int
numMoves(CubesStack *s)
{
	return s->count;
}

Boolean
scanMoves(FILE *fp, CubesWidget w, int moves)
{
	int direction, l, c;

	for (l = 0; l < moves; l++) {
		while ((c = getc(fp)) != EOF && c != SYMBOL);
		if (fscanf(fp, "%d", &direction) != 1) {
			(void) fprintf(stderr,
				"corrupt scan, expecting an integer for move %d\n", l);
			return False;
		}
		if (!movePuzzleDir(w, direction, INSTANT)) {
			(void) fprintf(stderr,
				"%d move in direction %d, cannot be made.",
				l, direction);
			return False;
		}
	}
	return True;
}

void
printMoves(FILE *fp, CubesStack *s)
{
	int direction, counter = 0;

	s->currMove = s->firstMove->next;
	(void) fprintf(fp, "moves\tdirection\n");
	while (s->currMove != s->lastMove) {
		readMove(&(s->currMove->move), &direction);;
		(void) fprintf(fp, "%d%c\t%d\n", ++counter, SYMBOL, direction);
		s->currMove = s->currMove->next;
	}
}

Boolean
scanStartPosition(FILE *fp, CubesWidget w)
{
	int i, num, c;

	while ((c = getc(fp)) != EOF && c != SYMBOL);
	for (i = 0; i < w->cubes.sizeBlock; i++) {
		if (fscanf(fp, "%d ", &num) != 1) {
			(void) fprintf(stderr,
				"corrupt start record, expecting an integer for %d\n", i);
			return False;
		}
		startPosition[i] = num;
		if (!num)
			startSpace = i;
	}
	return True;
}

void
printStartPosition(FILE *fp, CubesWidget w)
{
	int i;

	(void) fprintf(fp, "\nstartingPosition%c\n", SYMBOL);
	for (i = 0; i < w->cubes.sizeBlock; i++) {
		(void) fprintf(fp, "%4d", startPosition[i]);
		if (!((i + 1) % w->cubes.sizeX))
			(void) fprintf(fp, "\n");
		if (!((i + 1) % w->cubes.sizeRect))
			(void) fprintf(fp, "\n");
	}
}

void
setStartPosition(CubesWidget w)
{
	int i;

	w->cubes.spacePosition = startSpace;
	for (i = 0; i < w->cubes.sizeBlock; i++)
		w->cubes.blockOfPosition[i] = startPosition[i];
	drawAllBlocks(w);
}
