#include <stddef.h>
#include <stdint.h>
#include "BasicRenderer.h"
#include "cstr.h"
#include "efiMemory.h"
#include "memory.h"
#include "Bitmap.h"
#include "PageFrameAllocator.h"

struct BootInfo {
	Framebuffer* framebuffer;
	PSF1_FONT* psf1_Font;
	EFI_MEMORY_DESCRIPTOR* mMap;
	uint64_t mMapSize;
	uint64_t mMapDescriptorSize;
};

// supposed to protect against stack smashing but we have no way to actually like
// do anything when we detect it soooo...
extern "C" void __stack_chk_fail(void) {
	return;
}
uint8_t testBuffer[20];
extern "C" void _start(BootInfo* bootInfo) {
	BasicRenderer newRenderer = BasicRenderer(bootInfo->framebuffer, bootInfo->psf1_Font);

	// display information of memory map
	uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize;


	PageFrameAllocator newAllocator;
	newAllocator.ReadEFIMemoryMap(bootInfo->mMap, bootInfo->mMapSize, bootInfo->mMapDescriptorSize);

	newRenderer.Print(0xFFFFFFFF, "Free RAM: ");
	newRenderer.Print(0xFF00FFFF, to_string(newAllocator.GetFreeRAM() / 1024));
	newRenderer.Print(0xFF00FFFF, " KB");
	newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
	newRenderer.Print(0xFFFFFFFF, "Used RAM: ");
	newRenderer.Print(0xFF00FFFF, to_string(newAllocator.GetUsedRAM() / 1024));
	newRenderer.Print(0xFF00FFFF, " KB");
	newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
	newRenderer.Print(0xFFFFFFFF, "Reserved RAM: ");
	newRenderer.Print(0xFF00FFFF, to_string(newAllocator.GetReservedRAM() / 1024));
	newRenderer.Print(0xFF00FFFF, " KB");
	newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};

	//newRenderer.Print(0xFFFFFFFF, to_string(GetMemorySize(bootInfo->mMap, mMapEntries, bootInfo ->mMapDescriptorSize)));


	// for (int i = 0; i < mMapEntries; i++) {
	// 	EFI_MEMORY_DESCRIPTOR* desc = (EFI_MEMORY_DESCRIPTOR*)((uint64_t)bootInfo ->mMap (i * bootinfo->mMapDescriptorSize));
	// 	newRenderer.Print(0xFFFFFFFF, EFI_MEMORY_TYPE_STRINGS[desc->type]);
	// 	newRenderer.Print(0xFFFFFFFF, " ");
	// 	newRenderer.Print(0xFFFF00FF, to_string(desc->numPages * 4096 / 1024));
	// 	newRenderer.Print(0xFFFF00FF, " KB");
	// 	newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
	// }
  
	return;
}