#! /usr/bin/env python3
from sys import argv
import xmltodict


def convert_to_string(filename, doors, entities):
    output_filename, extension = filename.split('.')
    char_list = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ "

    with open(filename, "r") as file:
        data = file.read()

    if extension == "tmx":
        data = xmltodict.parse(data)["map"]["layer"]["data"]["#text"].splitlines()
        data = [[int(char_id) - 1 for char_id in line.split(",") if char_id] for line in data]
    elif extension == "csv":
        data = data.splitlines()
        data = [[int(char_id) for char_id in line.split(",") if char_id] for line in data]
    
    doors_coords = []
    entities_data = []
    output = r""
    for line_index, line in enumerate(data):
        for char_index, char_id in enumerate(line):
            
            if char_list[char_id] not in entities:
                output += char_list[char_id]
            else:
                output += " "
                entities_data.append(f"[None, '{char_list[char_id]}', , {char_index}, {line_index}, 'stand_by'],")
                

            if char_list[char_id] in doors:
                doors_coords.append(f"\t({char_index}, {line_index}, , 0, 0),")

        output += "\n"

    doors_coords = "\n".join(doors_coords)
    entities_data = "(\n\t" + "\n\t".join(entities_data) + "\n)"

    with open(f"{output_filename}.py", "w") as file:
        file.write(f"{output_filename} = (r\"\"\"\n{output[:-1]}\"\"\",\n{doors_coords}\n)\n\n{output_filename}_entities = {entities_data}")


filename, doors, misc = argv[1], "", ""
for arg in argv[2:]:
    if arg.startswith("door"):
        doors = arg.split("=", 1)[1]
    elif arg.startswith("entities"):
        entities = arg.split("=", 1)[1]

convert_to_string(filename, doors, entities)