package main

import "core:fmt"

MessageLevel :: enum {
	FIXME,
	Warning,
	Error,
	Fatal,
}

// FIXME: Add file
Message :: struct {
	level:           MessageLevel,
	message:         string,
	source_location: SourceLocation,
}

g_message_list: [dynamic]Message

message_create :: proc(level: MessageLevel, message: string, source_location: SourceLocation) -> Message {
	return Message{level = level, message = message, source_location = source_location}
}

source_print :: proc(data: ^[]u8, range: TextRange) {
	pos := TextPosition{1, 1}
	should_print := false

	additional_spaces := 0
	for ch in data {
		should_print := pos.line >= range.start.line && pos.line <= range.end.line
		if should_print {
			if pos.column == 1 {
				fmt.printf("\x1B[90m  % 4d | \x1B[0m", pos.line)
			}
			if range.start.line == pos.line && range.start.column - 1 == pos.column {
				fmt.printf("\x1B[95m")
			}
			if ch == '\t' {
				additional_spaces += 1
				fmt.printf("  ")
			} else {
				fmt.printf("%c", ch)
			}
			a := TextPosition{range.end.line, range.end.column - 1}
			if pos == a {
				fmt.printf("\x1B[0m")
			}
		}

		pos.column += 1
		if ch == '\n' {
			pos.column = 1
			pos.line += 1
		}
	}

	fmt.printf("       ")
	for _ in 0 ..< range.start.column + u64(additional_spaces) {
		fmt.printf(" ")
	}

	fmt.printf("\x1B[95m")
	for _ in 0 ..= range.end.column - range.start.column {
		fmt.printf("^")
	}
	fmt.println(" Here \x1B[0m")
}

message_print :: proc(message: ^Message, source_code: ^[]u8) {
	fmt.printf("\x1b[1m")
	if message.level == .FIXME {
		fmt.printf("\x1B[94mFIXME\x1B[0m \x1b[37mat ")
	} else if message.level == .Warning {
		fmt.printf("\x1B[93mWarning\x1B[0m \x1b[37mat ")
	} else if message.level == .Error {
		fmt.printf("\x1B[91mError\x1B[0m \x1b[37mat ")
	} else if message.level == .Fatal {
		fmt.printf("\x1B[91;1mError\x1B[0m \x1b[37mat ")
	}

	fmt.printf(
		"\x1B[32m%s\x1b[90m:\x1B[36m{}:{}\x1B[0m->\x1B[36m{}:{}\x1B[0m: ",
		message.source_location.file,
		message.source_location.range.start.line,
		message.source_location.range.start.column,
		message.source_location.range.end.line,
		message.source_location.range.end.column,
	)

	fmt.println(message.message)

	source_print(source_code, message.source_location.range)
}
