// This is from Odin examples:
// https://github.com/odin-lang/examples/blob/master/by_example/dir_info/main.odin

package main

import "core:fmt"
import "core:os"
import "core:path/filepath"
import "core:strings"

<beginfold id='1'></beginfold id='1'>/**
 * This is
 * a multiline
 * comment
 <endfold id='1'>*/</endfold id='1'>

packedDirective :: struct #packed <beginfold id='2'>{</beginfold id='2'>
	x: u8,
	y: i32,
	z: u16,
	w: u8,
<endfold id='2'>}</endfold id='2'>

Foo :: enum <beginfold id='2'>{</beginfold id='2'>
	A,
	B,
	C,
	D,
<endfold id='2'>}</endfold id='2'>

@private
_SS_MAXSIZE   :: 128

@require_results
some_proc::proc "contextless"() -> i32<beginfold id='2'>{</beginfold id='2'>
    return 0
<endfold id='2'>}</endfold id='2'>

@(private = "file")
print_file_info :: proc(fi: os.File_Info) <beginfold id='2'>{</beginfold id='2'>
    context.user_index = 100
    flt := 12.33
    flt_e := 1e9
	bin_ok := 0b110001
	bin_wrong := 0b1122001010123123
	oct := 0o1237
	oct_wrong := 0o1239999
	hex := 0xFF123AC
	hex_wrong := 0xFF123ACHH

	// Split the path into directory and filename
	_, filename := filepath.split(fi.fullpath)

	SIZE_WIDTH :: 12
	buf: [SIZE_WIDTH]u8

	raw_str := `this_is raw ;';@@""\\\' string`
	// Print size to string backed by buf on stack, no need to free
	_size := "-" if fi.is_dir else fmt.bprintf(buf[:], "%v", fi.size)

	a: u8 = cast(u8)'"'
	b: u8 = '"'

	// Right-justify size for display, heap allocated
	size := strings.right_justify(_size, SIZE_WIDTH, " ")
	defer delete(size)

	val: i32 = 1
	switch (val) <beginfold id='2'>{</beginfold id='2'>
	case 1:
		val = 2
	case 2:
		val = 3
	case:
		val = 0
	<endfold id='2'>}</endfold id='2'>

	// This should not turn anything below into string colors
	triggerCharacter := "\""

	if fi.is_dir <beginfold id='2'>{</beginfold id='2'>
		fmt.printf ("%v [%v]\n word", size, filename)
	<endfold id='2'>}</endfold id='2'> else <beginfold id='2'>{</beginfold id='2'>
		fmt.printf("%v %v\n", size, filename)
	<endfold id='2'>}</endfold id='2'>
<endfold id='2'>}</endfold id='2'>

main :: proc() <beginfold id='2'>{</beginfold id='2'>
	cwd := os.get_current_directory()
	fmt.println("Current working directory:", cwd)

	f, err := os.open(cwd)
	defer os.close(f)

	if err != os.ERROR_NONE <beginfold id='2'>{</beginfold id='2'>
		// Print error to stderr and exit with errorcode
		fmt.eprintln("Could not open directory for reading", err)
		os.exit(1)
	<endfold id='2'>}</endfold id='2'>

	fis: []os.File_Info
	defer os.file_info_slice_delete(fis) // fis is a slice, we need to remember to free it

	fis, err = os.read_dir(f, -1) // -1 reads all file infos
	if err != os.ERROR_NONE <beginfold id='2'>{</beginfold id='2'>
		fmt.eprintln("Could not read directory", err)
		os.exit(2)
	<endfold id='2'>}</endfold id='2'>

	for fi in fis <beginfold id='2'>{</beginfold id='2'>
		print_file_info(fi)
	<endfold id='2'>}</endfold id='2'>

	enumTest := Foo.A
	#partial switch enumTest <beginfold id='2'>{</beginfold id='2'>
	case .A:
		fmt.println("A letter\n nextline \xfe \u200B \000 \U0001f4ae")
	case .D:
		fmt.println("D")
	<endfold id='2'>}</endfold id='2'>
<endfold id='2'>}</endfold id='2'>

