#!/usr/bin/env python3
import os
from pathlib import Path


def load_buffers(filename: str):
    colormem = list()
    charmem = list()
    with open(filename, mode="r") as f:
        lines: list = f.readlines()
        count: int = 0
        for line in lines:
            if line[0].isnumeric():
                data: str = line.strip("\n")
                if count >= 2 and count < 27:
                    charmem.append(data)
                if count >= 27 and count < 53:
                    colormem.append(data)
            count += 1
    print(filename)
    # print(len(colormem))
    # print(len(charmem))
    return charmem,colormem


def extend_buffer(source, append):
    return [x+y for x,y in zip(source[0], append[0])],[x+y for x,y in zip(source[1], append[1])]


current: Path = Path(os.path.dirname(os.path.realpath(__file__)))
root: Path = current.parent.absolute()
graphics_source = root.joinpath("assets/biglogo")
graphics_target = root.joinpath("src")
graphics_target.mkdir(parents=True, exist_ok=True)
print(f"Rendering PETSCII conversion template... {graphics_source}")
output: Path = graphics_target.joinpath("biglogo_include.asm")

charsa = list()
colorsa = list()
charsa,colorsa = load_buffers(graphics_source.joinpath("ons_biglogo1.c"))
charsa,colorsa = extend_buffer((charsa,colorsa),load_buffers(graphics_source.joinpath("ons_biglogo2.c")))
charsa,colorsa = extend_buffer((charsa,colorsa),load_buffers(graphics_source.joinpath("ons_biglogo3.c")))

charsb = list()
colorsb = list()
charsb,colorsb = load_buffers(graphics_source.joinpath("ons_biglogo4.c"))
charsb,colorsb = extend_buffer((charsb,colorsb),load_buffers(graphics_source.joinpath("ons_biglogo5.c")))
charsb,colorsb = extend_buffer((charsb,colorsb),load_buffers(graphics_source.joinpath("ons_biglogo6.c")))

chars = list()
chars.extend(charsa)
chars.extend(charsb)

colors = list()
colors.extend(colorsa)
colors.extend(colorsb)


if len(chars) != len(colors):
    raise ValueError("Dimensions of the color map don't match the charmap. Fatal.")

with open(output, mode="w") as o:
    o.write("""
/*
This file is autogenerated by bin/convert_c_biglogo.py
*/

""")
    for i in range(len(chars)):
        charbytes = chars[i].split(",")
        colorbytes = colors[i].split(",")
        output = list()
        for j in range(128):
            if j < len(charbytes):
                c: str = charbytes[j].strip()
                c = c if c.isnumeric() else 0
                output.append(c)
            else:
                output.append(0)
        for j in range(128):
            if j < len(colorbytes):
                c: str = colorbytes[j].strip()
                c = c if c.isnumeric() else 0
                output.append(c)
            else:
                output.append(0)
        o.write(f".byte ")
        o.write(",".join([str(f).rjust(3,"0") for f in output]))
        o.write("\n")

#!/usr/bin/env python3
import os
from pathlib import Path


def load_buffers(filename: str):
    colormem = list()
    charmem = list()
    with open(filename, mode="r") as f:
        lines: list = f.readlines()
        count: int = 0
        for line in lines:
            if line[0].isnumeric():
                data: str = line.strip("\n")
                if count >= 2 and count < 27:
                    charmem.append(data)
                if count >= 27 and count < 53:
                    colormem.append(data)
            count += 1
    print(filename)
    # print(len(colormem))
    # print(len(charmem))
    return charmem,colormem


def extend_buffer(source, append):
    return [x+y for x,y in zip(source[0], append[0])],[x+y for x,y in zip(source[1], append[1])]


current: Path = Path(os.path.dirname(os.path.realpath(__file__)))
root: Path = current.parent.absolute()
graphics_source = root.joinpath("assets/biglogo")
graphics_target = root.joinpath("src")
graphics_target.mkdir(parents=True, exist_ok=True)
print(f"Rendering PETSCII conversion template... {graphics_source}")
output: Path = graphics_target.joinpath("biglogo_include.asm")

charsa = list()
colorsa = list()
charsa,colorsa = load_buffers(graphics_source.joinpath("ons_biglogo1.c"))
charsa,colorsa = extend_buffer((charsa,colorsa),load_buffers(graphics_source.joinpath("ons_biglogo2.c")))
charsa,colorsa = extend_buffer((charsa,colorsa),load_buffers(graphics_source.joinpath("ons_biglogo3.c")))

charsb = list()
colorsb = list()
charsb,colorsb = load_buffers(graphics_source.joinpath("ons_biglogo4.c"))
charsb,colorsb = extend_buffer((charsb,colorsb),load_buffers(graphics_source.joinpath("ons_biglogo5.c")))
charsb,colorsb = extend_buffer((charsb,colorsb),load_buffers(graphics_source.joinpath("ons_biglogo6.c")))

chars = list()
chars.extend(charsa)
chars.extend(charsb)

colors = list()
colors.extend(colorsa)
colors.extend(colorsb)


if len(chars) != len(colors):
    raise ValueError("Dimensions of the color map don't match the charmap. Fatal.")

with open(output, mode="w") as o:
    o.write("""
/*
This file is autogenerated by bin/convert_c_biglogo.py
*/

""")
    for i in range(len(chars)):
        charbytes = chars[i].split(",")
        colorbytes = colors[i].split(",")
        output = list()
        for j in range(128):
            if j < len(charbytes):
                c: str = charbytes[j].strip()
                c = c if c.isnumeric() else 0
                output.append(c)
            else:
                output.append(0)
        for j in range(128):
            if j < len(colorbytes):
                c: str = colorbytes[j].strip()
                c = c if c.isnumeric() else 0
                output.append(c)
            else:
                output.append(0)
        o.write(f".byte ")
        o.write(",".join([str(f).rjust(3,"0") for f in output]))
        o.write("\n")

#!/usr/bin/env python3 # print(len(colormem)) # print(len(charmem)) /* This file is autogenerated by bin/convert_c_biglogo.py */