package main

import "core:fmt"
import "core:os"
import "core:strings"

read_stdin :: proc() -> []string {
	data, err := os.read_entire_file_from_file(os.stdin, context.allocator)
	content := string(data)
	return strings.split_lines(content)
}

print_token :: proc(token: ^Token, depth: int = 0) {
	indent := strings.repeat("    ", depth, context.temp_allocator)

	type_str := ""
	switch token.type {
	case TokenType.Root:
		type_str = "Root"
	case TokenType.Text:
		type_str = "Text"
	case TokenType.Italics:
		type_str = "Italics"
	case TokenType.Bold:
		type_str = "Bold"
	case TokenType.BreakLine:
		type_str = "BreakLine"
	case TokenType.Header:
		type_str = "Header"
	case TokenType.Paragraph:
		type_str = "Paragraph"
	case TokenType.Blockquote:
		type_str = "Blockquote"
	case TokenType.UnorderedList:
		type_str = "UnorderedList"
	case TokenType.OrderedList:
		type_str = "OrderedList"
	case TokenType.ListItem:
		type_str = "ListItem"
	case TokenType.Strikethrough:
		type_str = "Strikethrough"
	case TokenType.Table:
		type_str = "Table"
	case TokenType.TableRow:
		type_str = "TableRow"
	case TokenType.TableHeaderRow:
		type_str = "TableHeaderRow"
	case TokenType.TableCell:
		type_str = "TableCell"
	case TokenType.TableSeparator:
		type_str = "TableSeparator"
	case TokenType.Left:
		type_str = "Left"
	case TokenType.Right:
		type_str = "Right"
	case TokenType.Middle:
		type_str = "Middle"
	case TokenType.FootnoteDef:
		type_str = "FootnoteDef"
	case TokenType.FootnoteRef:
		type_str = "FootnoteRef"
	case TokenType.CodeBlock:
		type_str = "CodeBlock"
	case TokenType.InlineCode:
		type_str = "InlineCode"
	case TokenType.HorizontalRule:
		type_str = "HorizontalRule"
	case TokenType.Underline:
		type_str = "Underline"
	case TokenType.Superscript:
		type_str = "Superscript"
	case TokenType.Subscript:
		type_str = "Subscript"
	case TokenType.HeaderLink:
		type_str = "HeaderLink"
	case TokenType.Link:
		type_str = "Link"
	case TokenType.Image:
		type_str = "Image"
	case TokenType.Audio:
		type_str = "Audio"
	case TokenType.Video:
		type_str = "Video"
	case TokenType.Iframe:
		type_str = "Iframe"
	case TokenType.Script:
		type_str = "Script"
	case TokenType.ScriptBlock:
		type_str = "ScriptBlock"
	case TokenType.Variable:
		type_str = "Variable"
	case TokenType.VariableDef:
		type_str = "VariableDef"
	}

	value_str := ""
	switch v in token.value {
	case string:
		value_str = v
	case int:
		value_str = fmt.aprintf("%d", v, allocator = context.temp_allocator)
	}

	if value_str != "" {
		fmt.printf("%s%s(%s)", indent, type_str, value_str)
	} else {
		fmt.printf("%s%s", indent, type_str)
	}
	fmt.println()

	for child in token.children {
		print_token(child, depth + 1)
	}
}

main :: proc() {
	lines := read_stdin()
	parsed := parse(lines)
	print_html(&parsed)
}
