package examples

import "core:fmt"
import "core:log"
import "core:mem"
import "core:os"

import qr ".."

main :: proc() {
	//----- General setup ----------------------------------
	{
		// Temp
		track_temp: mem.Tracking_Allocator
		mem.tracking_allocator_init(&track_temp, context.temp_allocator)
		context.temp_allocator = mem.tracking_allocator(&track_temp)

		// Default
		track: mem.Tracking_Allocator
		mem.tracking_allocator_init(&track, context.allocator)
		context.allocator = mem.tracking_allocator(&track)
		// Log a warning about any memory that was not freed by the end of the program.
		// This could be fine for some global state or it could be a memory leak.
		defer {
			// Temp allocator
			if len(track_temp.bad_free_array) > 0 {
				fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
				for entry in track_temp.bad_free_array {
					fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
				}
				mem.tracking_allocator_destroy(&track_temp)
			}
			// Default allocator
			if len(track.allocation_map) > 0 {
				fmt.eprintf("=== %v allocations not freed - main allocator: ===\n", len(track.allocation_map))
				for _, entry in track.allocation_map {
					fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
				}
			}
			if len(track.bad_free_array) > 0 {
				fmt.eprintf("=== %v incorrect frees - main allocator: ===\n", len(track.bad_free_array))
				for entry in track.bad_free_array {
					fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
				}
			}
			mem.tracking_allocator_destroy(&track)
		}
		// Logger
		context.logger = log.create_console_logger()
		defer log.destroy_console_logger(context.logger)
	}

	args := os.args
	if len(args) < 2 {
		fmt.eprintln("Usage: examples <example_name>")
		fmt.eprintln("Available examples: basic, variety, segment, mask")
		os.exit(1)
	}

	switch args[1] {
	case "basic": basic()
	case "variety": variety()
	case "segment": segment()
	case "mask": mask()
	case:
		fmt.eprintf("Unknown example: %v\n", args[1])
		fmt.eprintln("Available examples: basic, variety, segment, mask")
		os.exit(1)
	}
}

// Creates a single QR Code, then prints it to the console.
basic :: proc() {
	text :: "Hello, world!"
	ecl :: qr.Ecc.Low

	qrcode: [qr.BUFFER_LEN_MAX]u8
	ok := qr.encode_auto(text, qrcode[:], ecl)
	if ok do print_qr(qrcode[:])
}

// Creates a variety of QR Codes that exercise different features of the library.
variety :: proc() {
	qrcode: [qr.BUFFER_LEN_MAX]u8

	{ 	// Numeric mode encoding (3.33 bits per digit)
		ok := qr.encode_auto("314159265358979323846264338327950288419716939937510", qrcode[:], qr.Ecc.Medium)
		if ok do print_qr(qrcode[:])
	}

	{ 	// Alphanumeric mode encoding (5.5 bits per character)
		ok := qr.encode_auto("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", qrcode[:], qr.Ecc.High)
		if ok do print_qr(qrcode[:])
	}

	{ 	// Unicode text as UTF-8
		ok := qr.encode_auto(
			"\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1wa\xE3\x80\x81" +
			"\xE4\xB8\x96\xE7\x95\x8C\xEF\xBC\x81\x20\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4",
			qrcode[:],
			qr.Ecc.Quartile,
		)
		if ok do print_qr(qrcode[:])
	}

	{ 	// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
		ok := qr.encode_auto(
			"Alice was beginning to get very tired of sitting by her sister on the bank, " +
			"and of having nothing to do: once or twice she had peeped into the book her sister was reading, " +
			"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " +
			"'without pictures or conversations?' So she was considering in her own mind (as well as she could, " +
			"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a " +
			"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly " +
			"a White Rabbit with pink eyes ran close by her.",
			qrcode[:],
			qr.Ecc.High,
		)
		if ok do print_qr(qrcode[:])
	}
}

// Creates QR Codes with manually specified segments for better compactness.
segment :: proc() {
	qrcode: [qr.BUFFER_LEN_MAX]u8

	{ 	// Illustration "silver"
		silver0 :: "THE SQUARE ROOT OF 2 IS 1."
		silver1 :: "41421356237309504880168872420969807856967187537694807317667973799"

		// Encode as single text (auto mode selection)
		{
			concat :: silver0 + silver1
			ok := qr.encode_auto(concat, qrcode[:], qr.Ecc.Low)
			if ok do print_qr(qrcode[:])
		}

		// Encode as two manual segments (alphanumeric + numeric) for better compactness
		{
			seg_buf0: [qr.BUFFER_LEN_MAX]u8
			seg_buf1: [qr.BUFFER_LEN_MAX]u8
			segs := [2]qr.Segment{qr.make_alphanumeric(silver0, seg_buf0[:]), qr.make_numeric(silver1, seg_buf1[:])}
			ok := qr.encode_auto(segs[:], qr.Ecc.Low, qrcode[:])
			if ok do print_qr(qrcode[:])
		}
	}

	{ 	// Illustration "golden"
		golden0 :: "Golden ratio \xCF\x86 = 1."
		golden1 :: "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"
		golden2 :: "......"

		// Encode as single text (auto mode selection)
		{
			concat :: golden0 + golden1 + golden2
			ok := qr.encode_auto(concat, qrcode[:], qr.Ecc.Low)
			if ok do print_qr(qrcode[:])
		}

		// Encode as three manual segments (byte + numeric + alphanumeric) for better compactness
		{
			golden0_str: string = golden0
			golden0_bytes := transmute([]u8)golden0_str
			seg_buf0: [qr.BUFFER_LEN_MAX]u8
			seg_buf1: [qr.BUFFER_LEN_MAX]u8
			seg_buf2: [qr.BUFFER_LEN_MAX]u8
			segs := [3]qr.Segment {
				qr.make_bytes(golden0_bytes, seg_buf0[:]),
				qr.make_numeric(golden1, seg_buf1[:]),
				qr.make_alphanumeric(golden2, seg_buf2[:]),
			}
			ok := qr.encode_auto(segs[:], qr.Ecc.Low, qrcode[:])
			if ok do print_qr(qrcode[:])
		}
	}

	{ 	// Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
		// Encode as text (auto mode — byte mode)
		{
			madoka ::
				"\xE3\x80\x8C\xE9\xAD\x94\xE6\xB3\x95\xE5" +
				"\xB0\x91\xE5\xA5\xB3\xE3\x81\xBE\xE3\x81" +
				"\xA9\xE3\x81\x8B\xE2\x98\x86\xE3\x83\x9E" +
				"\xE3\x82\xAE\xE3\x82\xAB\xE3\x80\x8D\xE3" +
				"\x81\xA3\xE3\x81\xA6\xE3\x80\x81\xE3\x80" +
				"\x80\xD0\x98\xD0\x90\xD0\x98\xE3\x80\x80" +
				"\xEF\xBD\x84\xEF\xBD\x85\xEF\xBD\x93\xEF" +
				"\xBD\x95\xE3\x80\x80\xCE\xBA\xCE\xB1\xEF" +
				"\xBC\x9F"
			ok := qr.encode_auto(madoka, qrcode[:], qr.Ecc.Low)
			if ok do print_qr(qrcode[:])
		}

		// Encode with manual kanji mode (13 bits per character)
		{
					//odinfmt: disable
			kanji_chars :: [29]int{
				0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,
				0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,
				0x018D, 0x018A, 0x0036, 0x0141, 0x0144,
				0x0001, 0x0000, 0x0249, 0x0240, 0x0249,
				0x0000, 0x0104, 0x0105, 0x0113, 0x0115,
				0x0000, 0x0208, 0x01FF, 0x0008,
			}
			//odinfmt: enable

			seg_buf: [qr.BUFFER_LEN_MAX]u8
			for &b in seg_buf {
				b = 0
			}

			seg: qr.Segment
			seg.mode = .Kanji
			seg.num_chars = len(kanji_chars)
			seg.bit_length = 0
			for ch in kanji_chars {
				for j := 12; j >= 0; j -= 1 {
					seg_buf[seg.bit_length >> 3] |= u8(((ch >> uint(j)) & 1)) << uint(7 - (seg.bit_length & 7))
					seg.bit_length += 1
				}
			}
			seg.data = seg_buf[:(seg.bit_length + 7) / 8]

			segs := [1]qr.Segment{seg}
			ok := qr.encode_auto(segs[:], qr.Ecc.Low, qrcode[:])
			if ok do print_qr(qrcode[:])
		}
	}
}

// Creates QR Codes with the same size and contents but different mask patterns.
mask :: proc() {
	qrcode: [qr.BUFFER_LEN_MAX]u8

	{ 	// Project Nayuki URL
		ok: bool

		ok = qr.encode_auto("https://www.nayuki.io/", qrcode[:], qr.Ecc.High)
		if ok do print_qr(qrcode[:])

		ok = qr.encode_auto("https://www.nayuki.io/", qrcode[:], qr.Ecc.High, mask = qr.Mask.M3)
		if ok do print_qr(qrcode[:])
	}

	{ 	// Chinese text as UTF-8
		text ::
			"\xE7\xB6\xAD\xE5\x9F\xBA\xE7\x99\xBE\xE7\xA7\x91\xEF\xBC\x88\x57\x69\x6B\x69\x70" +
			"\x65\x64\x69\x61\xEF\xBC\x8C\xE8\x81\x86\xE8\x81\xBD\x69\x2F\xCB\x8C\x77\xC9\xAA" +
			"\x6B\xE1\xB5\xBB\xCB\x88\x70\x69\xCB\x90\x64\x69\x2E\xC9\x99\x2F\xEF\xBC\x89\xE6" +
			"\x98\xAF\xE4\xB8\x80\xE5\x80\x8B\xE8\x87\xAA\xE7\x94\xB1\xE5\x85\xA7\xE5\xAE\xB9" +
			"\xE3\x80\x81\xE5\x85\xAC\xE9\x96\x8B\xE7\xB7\xA8\xE8\xBC\xAF\xE4\xB8\x94\xE5\xA4" +
			"\x9A\xE8\xAA\x9E\xE8\xA8\x80\xE7\x9A\x84\xE7\xB6\xB2\xE8\xB7\xAF\xE7\x99\xBE\xE7" +
			"\xA7\x91\xE5\x85\xA8\xE6\x9B\xB8\xE5\x8D\x94\xE4\xBD\x9C\xE8\xA8\x88\xE7\x95\xAB"

		ok: bool

		ok = qr.encode_auto(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M0)
		if ok do print_qr(qrcode[:])

		ok = qr.encode_auto(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M1)
		if ok do print_qr(qrcode[:])

		ok = qr.encode_auto(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M5)
		if ok do print_qr(qrcode[:])

		ok = qr.encode_auto(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M7)
		if ok do print_qr(qrcode[:])
	}
}

// Prints the given QR Code to the console.
print_qr :: proc(qrcode: []u8) {
	size := qr.get_size(qrcode)
	border :: 4
	for y in -border ..< size + border {
		for x in -border ..< size + border {
			fmt.print("##" if qr.get_module(qrcode, x, y) else "  ")
		}
		fmt.println()
	}
	fmt.println()
}
