#include "Console.h"

static Console *instance;
ushort *videoMemory;
static uchar X;
static uchar Y;
static uchar backColor;
static uchar foreColor;

void moveCursor();
void scrollScreen();

Console::Console()
{
}
Console::~Console()
{
}
Console *Console::GetInstance()
{
	if(!instance)
	{
		instance = new Console();
		instance->InitialiseConsole();
	}
	return instance;
}
void Console::InitialiseConsole()
{
	videoMemory = (ushort *)0xB8000;
	backColor = 0;
	foreColor = 15;
}
void Stream::Write(char c)
{
	uchar attributeByte = (backColor << 4) | (foreColor & 0x0F);
	ushort attribute = attributeByte << 8;

	if((c == '\b') && X)
	{
		X--;
		ushort *location = videoMemory + (Y * 80 + X);

		*location = ' ' | attribute;
	}
	else if(c == '\t')
		X = (X + 8) & ~(8 - 1);
	else if(c == '\r')
		X = 0;
	else if(c == '\n')
	{
		X = 0;
		Y++;
	}
	else if(c >= ' ')
	{
		ushort *location = videoMemory + (Y * 80 + X);

		*location = c | attribute;
		X++;
	}

	if(X >= 80)
	{
		X = 0;
		Y++;
	}
	scrollScreen();
	moveCursor();
}

void Stream::Write(char *string)
{
	int i = 0;

	while(string[i])
		Write(string[i++]);
}

void Stream::Write(const char *string)
{
	Write((char *)string);
}

void Stream::WriteLine(char *string)
{
	Write(string);
	Write("\r\n");
}

void Stream::WriteLine(const char *string)
{
	WriteLine((char *)string);
}

void Stream::WriteLine()
{
	WriteLine("");
}

void Stream::WriteHexadecimal(uint number)
{
	int tmp;
	char noZeroes = 1;
	int i;

	Write((char *)"0x");
	for (i = 28; i > 0; i -= 4)
	{
		tmp = (number >> i) & 0xF;
		if (tmp == 0 && noZeroes != 0)
			continue;

		noZeroes = 0;
		if (tmp >= 0xA)
			Write(tmp - 0xA + 'a');
		else
			Write(tmp + '0');
	}

	tmp = number & 0xF;
	if (tmp >= 0xA)
		Write(tmp - 0xA + 'a');
	else
		Write(tmp + '0');
}
void Stream::WriteDecimal(uint number)
{
	uint acc = number;
	char c[32];
	int i = 0;
	int j = 0;
	char c2[32];

	if (number == 0)
	{
		Write('0');
		return;
	}
	while (acc > 0)
	{
		c[i] = '0' + acc % 10;
		acc /= 10;
		i++;
	}
	c[i] = 0;

	c2[i--] = 0;
	while(i >= 0)
	{
		c2[i--] = c[j++];
	}
	Write(c2);
}
void Console::ClearConsole()
{
	uchar attributeByte = (backColor << 4) | (foreColor & 0x0F);
	ushort space = 0x20 | (attributeByte << 8);
	int i = 0;

	for(i = 0; i < 80 * 25; i++)
		videoMemory[i] = space;
	X = Y = 0;
	moveCursor();
}

void moveCursor()
{
	ushort cursorLocation = Y * 80 + X;

	outb(0x3D4, 14);
	outb(0x3D5, cursorLocation >> 8);
	outb(0x3D4, 15);
	outb(0x3D5, cursorLocation);
}

void scrollScreen()
{
    uchar attributeByte = (backColor << 4) | (foreColor & 0x0F);
    ushort blank = ' ' | (attributeByte << 8);

    if(Y >= 25)
    {
        int i;

        for (i = 0; i < 24 * 80; i++)
        {
            videoMemory[i] = videoMemory[i + 80];
        }

        for (i = 24 * 80; i < 25 * 80; i++)
        {
            videoMemory[i] = blank;
        }
        Y = 24;
    }
}

bool Console::ColorVideoCard()
{
	char c = (*(volatile ushort*)0x410) & 0x30;

	return (c != 0x30);
}
