/*
 *	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 <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <dirent.h>

#include "portme.h"
#include "story.h"
#include "text.h"
#include "ui.h"
#include "lib.h"

#pragma push_macro("bool")
#undef bool
#define DOS_IMPLEMENTATION
#include "dos.h"
#pragma pop_macro("bool")


#ifdef MESSAGES
#define MSG_PORT_UNABLE_TO_ALLOCATE_PATH    "Unable to allocate path %d."
#define MSG_PORT_UNABLE_TO_ALLOCATE_STORY   "Unable to allocate story %d."
#define MSG_PORT_UNABLE_TO_OPEN_STORY       "Unable to open %s!"
#define MSG_PORT_UNABLE_TO_READ_STORY       "Unable to read %s!"
#else
#define MSG_PORT_UNABLE_TO_ALLOCATE_PATH    ""
#define MSG_PORT_UNABLE_TO_ALLOCATE_STORY   ""
#define MSG_PORT_UNABLE_TO_OPEN_STORY       ""
#define MSG_PORT_UNABLE_TO_READ_STORY       ""
#endif


#ifdef _WIN32
#define PS  '\\'
#else
#define PS  '/'
#endif


static uint8_t *_RAM       = NULL;
static uint8_t _attr       = 0;


void portAttributeSet(byte attribute) {
	switch (attribute) {
		case TEXT_ATTR_NORMAL:
			portColorSet(7, 1);
			break;

		case TEXT_ATTR_REVERSE:
			portColorSet(1, 7);
			break;

		case TEXT_ATTR_BOLD:
			portColorSet(15, 1);
			break;

		case TEXT_ATTR_EMPHASIS:
			portColorSet(14, 1);
			break;
	}
}


uint8_t portByteGet(uint32_t address) {
	return _RAM[address];
}


void portByteSet(uint32_t address, uint8_t value) {
	_RAM[address] = value;
}


char portCharGet(void) {
	char           c;
	enum keycode_t key;

#if 0
	static char     playback[] = "open mailbox\ntake leaflet\nread leaflet\ndrop leaflet\n";
	static uint32_t pointer = 0;

	if (pointer < strlen(playback)) return playback[pointer++];
#endif

	do {
		c   = *readchars();
		key = *readkeys();
		if (key == KEY_RETURN) c = 13;
		if (key == KEY_BACK)   c = 127;
		if (key == KEY_DELETE) c = 8;
		if (key == KEY_UP)     c = (char)UI_KEY_UP;
		if (key == KEY_RIGHT)  c = (char)UI_KEY_RIGHT;
		if (key == KEY_DOWN)   c = (char)UI_KEY_DOWN;
		if (key == KEY_LEFT)   c = (char)UI_KEY_LEFT;
	} while (c == 0);

	return c;
}


void portCharGetPos(byte *x, byte *y) {
	*x = wherex() + 1;
	*y = wherey() + 1;
}


void portCharPrint(char c) {
	char      ch[2] = { 0, 0 };
	uint16_t *p;
	uint16_t  v = (_attr << 8) + ' ';
	byte      x;

	if ((wherex() >= screenwidth()) || (c == '\n')) {
		gotoxy(0, wherey() + 1);
		while (wherey() >= screenheight()) {
			memmove(((uint16_t*)screenbuffer()), ((uint16_t*)screenbuffer()) + screenwidth(), screenwidth() * (screenheight() - 1) * sizeof(uint16_t));
			p = (uint16_t *)screenbuffer() + (screenwidth() * (screenheight() - 1));
			for (x=0; x<screenwidth(); x++) *p++ = v;
			gotoxy(0, wherey() - 1);
		}
		return;
	}

	ch[0] = c;
	cputs(ch);
}


void portCharSetPos(byte x, byte y) {
	gotoxy(x - 1, y - 1);
}


void portColorSet(byte f, byte b) {
	textcolor(f);
	textbackground(b);
	_attr = (b << 4) + f;
}


void portCursorShow(bool s) {
	if (s) {
		curson();
	} else {
		cursoff();
	}
}


void portDie(char *fmt, ...) {
	va_list ap;

	printf("\n");
	va_start(ap, fmt);
	vprintf(fmt, ap);
	va_end(ap);
	printf("\n");

	exit(1);
}


uint32_t portFClose(void *stream) {
	return fclose((FILE *)stream);
}


void *portFOpen(char *pathname, char *mode) {
	return fopen(pathname, mode);
}


uint32_t portFRead(void *ptr, uint32_t size, uint32_t nmemb, void *stream) {
	return fread(ptr, size, nmemb, (FILE *)stream);
}


uint32_t portFWrite(void *ptr, uint32_t size, uint32_t nmemb, void *stream) {
	return fwrite(ptr, size, nmemb, (FILE *)stream);
}


void portFileLister(char *path) {
	DIR           *dir;
	struct dirent *dirent;
	uint32_t       l;
	FILE          *in;
	byte           x;
	byte           y;
	byte           i;

	if ((dir = opendir(path))) {
		for (;;) {
			if ((dirent = readdir(dir))) {
				// Is this a file or symlink?
				if ((dirent->d_type == DT_REG) || (dirent->d_type == DT_LNK)) {
					// Combine path and filename.
					x = 0;
					y = libStrLen(path);
					for (i=0; i<y; i++) __memoryBuffer[x++] = path[i];
					y = libStrLen(dirent->d_name);
					for (i=0; i<y; i++) __memoryBuffer[x++] = dirent->d_name[i];
					__memoryBuffer[x] = 0;
					// Get it's length.
					in = fopen(__memoryBuffer, "rb");
					if (in) {
						fseek(in, 0, SEEK_END);
						l = ftell(in);
						fseek(in, 0, SEEK_SET);
						fclose(in);
						// Add it to the story chooser.
						uiFileAdd(dirent->d_name, l);
					}
				}
			} else {
				break;
			}
		}
		closedir(dir);
	}
}


void portFree(void *ptr) {
	free(ptr);
}


void *portMalloc(uint32_t size) {
	return malloc(size);
}


uint16_t portRandomGet(int16_t range) {
	// If range is zero, randomize with "best" random seed.
	// If range is negative, use the positive value to seed.

	if (range <= 0) {
		srandom(range < 0 ? -range : time(NULL));
		return 0;
	}

	return (random() % range) + 1;
}


void portScreenClear(void) {
	clrscr();
}


void portStoryLoad(char *story, uint32_t *length) {
	FILE     *in;
	uint32_t  i;

	in = fopen(story, "rb");
	if (!in) portDie(MSG_PORT_UNABLE_TO_OPEN_STORY, story);

	_RAM = (byte *)malloc(*length);
	if (!_RAM) {
		fclose(in);
		portDie(MSG_PORT_UNABLE_TO_ALLOCATE_STORY, *length);
	}

	if (fread(_RAM, *length, 1, in) != 1) {
		free(_RAM);
		fclose(in);
		portDie(MSG_PORT_UNABLE_TO_READ_STORY, story);
	}

	fclose(in);

	for (i=0; i<*length; i++) storyAddToChecksum(i, _RAM[i]);
}


char *portSymbolsGet(void) {
	//  Box drawing and other symbols needed by the UI.
	//
	//    0---1---2
	//    |       |
	//    7       3
	//    |       |
	//    6---5---4

	return "\xda\xc4\xbf\xb3\xd9\xc4\xc0\xb3";
}


uint16_t portWordGet(uint32_t address) {
	return ((uint16_t)_RAM[address] << 8) | ((uint16_t)_RAM[address + 1]);
}


void portWordSet(uint32_t address, uint16_t value) {
	_RAM[address]     = (value >> 8) & 0xff;   // MSB first.
	_RAM[address + 1] = value & 0xff;
}


int main(int argc, char *argv[]) {
	byte  x;
	byte  l;
	char *noPath = "./";
	char *path   = NULL;

	// One argumet, the program name.  Use same path as binary.
	if (argc == 1) {
		// Remove executable name from path.
		l = libStrLen(argv[0]);
		for (x=l; x>0; x--) {
			if (argv[0][x] == PS) break;  // Path separator.
		}
		if (x > 0) {
			path = (char *)malloc(x + 1);
			if (path == 0) portDie(MSG_PORT_UNABLE_TO_ALLOCATE_PATH, 0);
			for (l=0; l<=x; l++) path[l] = argv[0][l];
			path[l] = 0;
		} else {
			path = noPath;
		}
	}

	// User provided at least one command line argument.  Use first one.
	if (argc >  1) {
		// Does it end with a '/' or '\'?
		l = libStrLen(argv[1]);
		if (argv[1][l-1] != PS) l++;  // Add space for a slash.
		path = (char *)malloc(l);
		if (path == 0) portDie(MSG_PORT_UNABLE_TO_ALLOCATE_PATH, 1);
		for (x=0; x<l; x++) path[x] = argv[1][x];
		path[x-1] = PS;
		path[x] = 0;
	}

	setvideomode(videomode_80x25_9x16);

	uiSizeSet(80, 25);
	uiGameSelect(path);

	free(_RAM);
	if (path != noPath) free(path);

	return 0;
}
