/*
 *	Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *	SOFTWARE.
 */


#include "window.h"
#include "portme.h"
#include "state.h"
#include "story.h"
#include "text.h"
#include "variable.h"


void windowEraseWindow(byte window) {
	byte x;
	byte y;
	byte t;
	byte b;
	byte i;
	byte j;

	// Remember cursor position.
	portCharGetPos(&x, &y);

	// Find top and bottom of area to clear.
	switch (window) {
		case WINDOW_STATUS:
			t = 1;
			b = __state.statusWindowLineCount;
			break;
		case WINDOW_TEXT:
			t = __state.statusWindowLineCount + 1;
			b = storyScreenHeightLines();
			break;
		case WINDOW_SCREEN:
			t = 1;
			b = storyScreenHeightLines();
			x = 1;
			y = storyScreenHeightLines();
			break;
	}

	// Clear.
	for (i=t; i<b; i++) {
		portCharSetPos(1, i);
		for (j=1; j<storyScreenWidthCharacters(); j++) {
			portCharPrint(' ');
		}
	}

	// Put cursor back.
	portCharSetPos(x, y);
}


static void windowPadStatus(byte col) {
	byte i;

	for (i=__state.statusBarLen; i<col; i++) {
		textCharPrint(' ');
	}
}


void windowSet(byte newWindow) {
	byte x;
	byte y;

	__state.currentWindow = newWindow;

	if (newWindow == WINDOW_STATUS) {
		//***TODO*** Formatting disabled.
		portCharGetPos(&__state.savedX, &__state.savedY);
		portCharSetPos(1, (storyVersion() <= 3) ? 2 : 1);
	} else {
		//***TODO*** Formatting restored.
		portCharSetPos(__state.savedX, __state.savedY);
		// Move cursor if it's been left in the status window.
		portCharGetPos(&x, &y);
		if (y <= __state.statusWindowLineCount) {
			portCharSetPos(1, __state.statusWindowLineCount + 1);
		}
		//***TODO*** Reset attributes to normal.
	}
}


void windowSplit(byte newLines) {
	// Maximum 255 lines.
	newLines &= 0xff;

	// Top line is always set for V3 and below.
	if (storyVersion() <= 3) newLines++;

	// Do we have a status window?
	if (newLines) {
		// Need to leave at least one line for the text window.
		if (newLines > storyScreenHeightLines() - 1) {
			__state.statusWindowLineCount = storyScreenHeightLines() - 1;
		} else {
			__state.statusWindowLineCount = newLines;
		}
		// Clear status window in V3 and below.
		if (storyVersion() <= 3) windowEraseWindow(WINDOW_STATUS);
	} else {
		__state.statusWindowLineCount = 0;
		// Move cursor back to text window.
		portCharSetPos(__state.savedX, __state.savedY);
	}

}


void windowUpdateStatusBar(void) {
	byte width = storyScreenWidthCharacters() - 1;

	windowSet(WINDOW_STATUS);
	portCharSetPos(1, 1);
	portAttributeSet(TEXT_ATTR_REVERSE);
	textOutputStream(STREAM_OUTPUT_MEMORY, 0);

	windowPadStatus(1);
	if (variableLoad(16) != 0) {
		textObjectPrint(variableLoad(16));
	}

	if ((storyFlags() & STORY_STATUS_MASK) == STORY_STATUS_HOURS_MINUTES) {
		// Timed game.
		windowPadStatus(width - 21);
		textStringPrint(" Time: ");
		textTimePrint(variableLoad(17), variableLoad(18));
	} else {
		// Scored game.
		windowPadStatus(width - 31);
		textStringPrint(" Score: ");
		textNumPrint(variableLoad(17));
		windowPadStatus(width - 15);
		textStringPrint(" Moves: ");
		textNumPrint(variableLoad(18));
	}

	windowPadStatus(width);
	__state.statusBar[__state.statusBarLen] = 0;

	textOutputStream(-STREAM_OUTPUT_MEMORY, 0);

	textStringPrint(__state.statusBar);

	portAttributeSet(TEXT_ATTR_NORMAL);
	windowSet(WINDOW_TEXT);
}
