#!/usr/bin/env python
#-*- coding:utf-8 -*-

import json
import yaml


INPUT = "gates.json"
OUTPUT = "gates.yml"


class_key = "=="
class_name = "de.craftinc.gates.Gate"

input_file = open(INPUT, "r")
input_gates = json.loads(input_file.read())
output_gates = []


for gate in input_gates:

	yml_gate = {}
	
	yml_gate[class_key] = class_name

	yml_gate["id"] = gate
	yml_gate["open"] = input_gates[gate]["isOpen"]
	yml_gate["hidden"] = input_gates[gate]["isHidden"]


	json_gate_blocks = input_gates[gate]["gateBlocks"]
	gate_location_world = input_gates[gate]["from"]["world"]
	yml_gate_blocks = []

	for json_block in json_gate_blocks:
		yml_block = {}
		yml_block["x"] = json_block[0]
		yml_block["y"] = json_block[1]
		yml_block["z"] = json_block[2]
		yml_block["world"] = gate_location_world

		yml_gate_blocks.append(yml_block)


	yml_gate["gateBlocks"] = yml_gate_blocks

	json_location = input_gates[gate]["from"]
	yml_location = {}

	yml_location["x"] = json_location["x"]
	yml_location["y"] = json_location["y"]
	yml_location["z"] = json_location["z"]
	yml_location["world"] = json_location["world"]

	yml_gate["location"] = yml_location


	json_exit = input_gates[gate]["to"]
	yml_exit = {}

	yml_exit["x"] = json_exit["x"]
	yml_exit["y"] = json_exit["y"]
	yml_exit["z"] = json_exit["z"]
	yml_exit["world"] = json_exit["world"]

	yml_gate["exit"] = yml_exit


	output_gates.append(yml_gate)


yml_string = yaml.safe_dump({ "gates": output_gates})
output_file = open(OUTPUT, "w")
output_file.write(yml_string)


