package main

import "./parser"
import "./renderer"
import "./token"
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(tok: ^token.Token, depth: int = 0) {
	indent := strings.repeat("    ", depth, context.temp_allocator)

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

	value_str := ""
	switch v in tok.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 tok.children {
		print_token(child, depth + 1)
	}
}

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