import os

# don't convert fragment_033.bin, always use original
def pack_font_file(output_filename, fragment_count=119, subtract_value=18525):  # 18525 for font0, 19731 for font1
    offsets = []
    data_blocks = []

    current_offset = 0
    for i in range(fragment_count):
        fragment_name = f"fragment_{i:03}.bin"
        if not os.path.exists(fragment_name):
            offsets.append(current_offset)
            continue

        with open(fragment_name, "rb") as f:
            data = f.read()
            data_blocks.append((i, current_offset, data))
            current_offset += len(data)
            offsets.append(current_offset)

    # Determine fixed byte sequence based on output filename
#    if "font0" in output_filename:
#        wides = bytes([
#            0x42, 0x01, 0xB2, 0xFF, 0x12, 0x00, 0xB0, 0x05, 0x01, 0x00, 0xB2, 0xFF, 0x0D, 0x00, 0xB0, 0x05,
#            0x03, 0x05, 0x06, 0x06, 0x05, 0x06, 0x03, 0x05, 0x05, 0x06, 0x05, 0x03, 0x05, 0x03, 0x0F, 0x00,
#            0xB2, 0x05, 0x1E, 0x00, 0xB0, 0x07, 0x06, 0x02, 0x00, 0xB2, 0x05, 0x08, 0x00, 0xB0, 0x07, 0x05,
#            0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x05, 0x03, 0x0A, 0x00, 0xB2, 0xFF, 0x18,
#        ])
    if "font0" in output_filename:
        wides = bytes([
            0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            0xFF, 0xFF, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            0x05, 0x03, 0x05, 0x06, 0x06, 0x05, 0x06, 0x03, 0x05, 0x05, 0x06, 0x05, 0x03, 0x05, 0x07, 0x05,
            0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04, 0x05,
            0x05, 0x07, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x07, 0x05, 0x05, 0x05, 0x05, 0x05, 0x08,
            0x05, 0x06, 0x05, 0x06, 0x05, 0x05, 0x07, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x07, 0x05, 0x05,
            0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
        ])
#    elif "font1" in output_filename:
#        wides = bytes([
#            0x3D, 0x04, 0xB2, 0xFF, 0x20, 0x00, 0xB0, 0x05, 0x03, 0x04, 0x06, 0x05, 0x06, 0x06, 0x03, 0x04,
#            0x04, 0x05, 0x06, 0x03, 0x05, 0x03, 0x06, 0x06, 0x04, 0x12, 0x00, 0xB2, 0x06, 0x17, 0x00, 0xB0,
#            0x04, 0x06, 0x05, 0x05, 0x04, 0x00, 0xB2, 0x06, 0x0E, 0x00, 0xB0, 0x04, 0x06, 0x04, 0x04, 0x06,
#            0x03, 0x06, 0x00, 0xB2, 0xFF, 0x18, 
#        ])
    elif "font1" in output_filename:
        wides = bytes([
            0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            0x05, 0x03, 0x04, 0x06, 0x05, 0x06, 0x06, 0x03, 0x04, 0x04, 0x05, 0x06, 0x03, 0x05, 0x08, 0x06,
            0x06, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x03, 0x06,
            0x06, 0x08, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x08, 0x06, 0x06, 0x06, 0x06, 0x06, 0x09,
            0x06, 0x06, 0x05, 0x07, 0x06, 0x06, 0x06, 0x06, 0x05, 0x06, 0x06, 0x06, 0x06, 0x08, 0x06, 0x06,
            0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
            0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
        ])
    else:
        wides = b''

    with open(output_filename, "wb") as out_f:
        # Write offsets
        for offset in offsets:
            val = offset + subtract_value
            out_f.write(val.to_bytes(2, byteorder='little'))

        # Write fixed byte sequence
        out_f.write(wides)

        # Write fragment data
        for _, _, data in data_blocks:
            out_f.write(data)

    print(f"File assembled: {output_filename}")

if __name__ == "__main__":
    pack_font_file("font0_packed")  # font0_packed, font1_packed