#include "vga.h"
#include "io.h"
#include "log.h"

static uint cursor_x = 0;
static uint cursor_y = 0;

static ushort color = WHITE;

static ushort *fb = (ushort *)0xB8000;

static void move_cursor()
{
	ushort loc = cursor_y * 80 + cursor_x;
	outb(0x3d4, 14); // Setting high cursor byte
	outb(0x3d4, loc >> 8);

	outb(0x3d4, 15); // low byte
	outb(0x3d4, loc & 0xff);
}

static void scroll()
{
	ushort blank = ' ' | color << 8;

	while (cursor_y >= 25) // end of line
	{
		// scroll everything up
		memcpy(fb, &fb[80], 24 * 80 * 2);

		for (int i = 24 * 80; i < 25 * 80; i++)
		{
			fb[i] = blank;
		}

		cursor_y--;
	}
}

void vga_set_color(enum vga_colors fg, enum vga_colors bg)
{
	color = (bg << 4) | fg & 0xf;
}

void vga_put(char c)
{
	switch (c)
	{
	case '\b':
		if (cursor_x > 0)
			cursor_x--;
		fb[cursor_y * 80 + cursor_x] = ' ' | (color << 8);
		break;
	case '\t':
		cursor_x = (cursor_x + 8) & ~7;
		break;
	case '\n':
		cursor_y++;
	case '\r':
		cursor_x = 0;
		break;
	default:
		fb[cursor_y * 80 + cursor_x] = c | (color << 8);
		cursor_x++;
	}

	if (cursor_x >= 80) // off screen
	{
		cursor_x = 0;
		cursor_y++;
	}

	scroll();
	move_cursor();
}

void vga_clear()
{
	memset(fb, 0, 80 * 25 * 2);
	cursor_x = 0;
	cursor_y = 0;
	move_cursor();
}

void vga_write(char *c)
{
	for (int i = 0; c[i]; i++)
		vga_put(c[i]);
}

void vga_putd(uint d)
{
	char str[48];
	memset(str, 0, 48);
	uint i = 0;

	do
	{
		str[i++] = (d % 10) + '0';
		d /= 10;
	} while (d > 0 && i < 48); // should never be more than 48 digits anyway

	for (uint j = i; j; j--)
	{
		vga_put(str[j - 1]);
	}
}

static bool vga_put_nibble(uchar n, bool first)
{
//	if (first && n == 0)
//		return true;

	if (n <= 9)
		vga_put('0' + n);
	else
		vga_put('A' + n - 10);

	return false;
}

void vga_putx(uint x)
{
	bool first = false;

	for (int shift = 24; shift >= 0; shift -= 8)
	{
		uchar byte = x >> shift;

		first = vga_put_nibble((byte & 0xf0) >> 4, first);
		first = vga_put_nibble(byte & 0x0f, first);
	}
}
