import os
import json

#Safely gets json code that might contain comments
#Credit: https://stackoverflow.com/questions/29959191/how-to-parse-json-file-with-c-style-comments
def get_json_from_file(fh):
    contents = ""
    for line in fh:
        cleanedLine = line.split("//", 1)[0]
        if len(cleanedLine) > 0 and line.endswith("\n") and "\n" not in cleanedLine:
            cleanedLine += "\n"
        contents += cleanedLine
    fh.close
    while "/*" in contents:
        preComment, postComment = contents.split("/*", 1)
        contents = preComment + postComment.split("*/", 1)[1]
    return json.loads(contents)

def main():
    for fname in os.listdir(os.getcwd()):
        if fname.endswith(".json"):
            fpath = os.path.join(os.getcwd(), fname)

            with open(fpath) as json_file:
                json_decoded = get_json_from_file(json_file)

            for render_controller in json_decoded.get("render_controllers", {}).items():
                
                json_decoded.get("render_controllers", {})[render_controller[0]]["is_hurt_color"] = {
                        "r": "0.5",
                        "g": "0.0",
                        "b": "0.7",
                        "a": "0.5"
                    }

                json_decoded.get("render_controllers", {})[render_controller[0]]["on_fire_color"] = {
                        "r": "0.5",
                        "g": "0.0",
                        "b": "0.7",
                        "a": "0.5"
                    }

            with open(fpath, 'w') as json_file:
                json.dump(json_decoded, json_file, indent=4)
        
main()

