import json
import os
import core.generate as generate

def save_world(name, data, items):
    with open(f"worlds/{name}.json", "w") as f:
        json.dump(data, f, indent=4)
    with open(f"world_items/{name}.json", "w") as f:
        json.dump(items, f, indent=4)

def load_world(name):
    with open(f"worlds/{name}.json") as f:
        world = json.load(f)
    if os.path.exists(f"world_items/{name}.json"):
        with open(f"world_items/{name}.json") as f:
            items = json.load(f)
    else:
        save_world(name, world, {})
        items = {}
    return [world, items]

def get_world_names():
    world_names = []
    for name in os.listdir("worlds"):
        world_names.append(name.replace(".json", ""))
    return world_names

def make_world(name):
    save_world(name, generate.generate(), {})