import os
from PIL import Image

def add(i, start, end, json):
    elements.append({
        "from": [start, -0.001, 15-i],
        "to": [end, 1, 16-i],
        "light_emission": 15 if "coal" not in json else 0,
        "faces": {
            "down": {
                "texture": "#all",
                "uv": [start, i, end, i+1]
            }
        }
    })
    elements.append({
        "from": [start, 15, i],
        "to": [end, 16.001, i+1],
        "light_emission": 15 if "coal" not in json else 0,
        "faces": {
            "up": {
                "texture": "#all",
                "uv": [start, i, end, i+1]
            }
        }
    })
    elements.append({
        "from": [16-end, 15-i, -0.001],
        "to": [16-start, 16-i, 1],
        "light_emission": 15 if "coal" not in json else 0,
        "faces": {
            "north": {
                "texture": "#all",
                "uv": [start, i, end, i+1]
            }
        }
    })
    elements.append({
        "from": [start, 15-i, 15],
        "to": [end, 16-i, 16.001],
        "light_emission": 15 if "coal" not in json else 0,
        "faces": {
            "south": {
                "texture": "#all",
                "uv": [start, i, end, i+1]
            }
        }
    })
    elements.append({
        "from": [-0.001, 15-i, start],
        "to": [1, 16-i, end],
        "light_emission": 15 if "coal" not in json else 0,
        "faces": {
            "west": {
                "texture": "#all",
                "uv": [start, i, end, i+1]
            }
        }
    })
    elements.append({
        "from": [15, 15-i, 16-end],
        "to": [16.001, 16-i, 16-start],
        "light_emission": 15 if "coal" not in json else 0,
        "faces": {
            "east": {
                "texture": "#all",
                "uv": [start, i, end, i+1]
            }
        }
    })

folder = r"D:\PCL\.minecraft\versions\1.21.5\1.21.5\minecraft\models\block"
for json in os.listdir(folder):
    if "_ore" in json:
        print(json)
        img = Image.open("textures/block/highlight/" + json.replace("json", "png"))
        img = img.convert('RGBA')
        pxl = img.load()
        dic = {
            "parent": "block/block",
            "ambientocclusion": False,
            "textures": {
                "particle": "minecraft:block/"+json[:-5],
                "all": "minecraft:block/"+json[:-5]
            }
        }
        elements = [
            {   "from": [0, 0, 0],
                "to": [16, 16, 16],
                "faces": {
                    "down":  { "texture": "#all" },
                    "up":    { "texture": "#all" },
                    "north": { "texture": "#all" },
                    "south": { "texture": "#all" },
                    "west":  { "texture": "#all" },
                    "east":  { "texture": "#all" }
                }
            }
        ]
        for i in range(16):
            start = 0
            while start < 16:
                while start < 16 and not any(pxl[start, i][:3]):
                    start += 1
                if start == 16:
                    break
                end = start+1
                while end < 16 and any(pxl[end, i][:3]):
                    end += 1
                add(i, start, end, json)
                start = end+1
        dic.update({"elements": elements})
        with open("models/block/" + json, 'w') as txt:
            txt.write(str(dic).replace("'", '"').replace("False", "false"))
