from graphviz import Digraph

from cbd.src.CBD import ConstantBlock, CBD


def draw(cbd):
	'''
	Output CBD as a dot digraph

	:param cbd: the model
	:return: a dot digraph
	'''

	dot = Digraph(comment='The model')

	def writeBlock(block):
		if isinstance(block, ConstantBlock):
			label = block.getBlockType() + " (" + block.getBlockName() + ")\\n" + str(block.getValue())
		else:
			label = block.getBlockType() + " (" + block.getBlockName() + ")"

		shape = ""
		if isinstance(block, CBD):
			shape="Msquare"

		dot.node(block.getBlockName(), label, {'shape': shape})


	for block in cbd.getBlocks():
		writeBlock(block)
		for (name, other) in  block.getLinksIn().items():
			label = ""

			if not name.startswith("IN"):
				label = name

			if not other.output_port.startswith("OUT"):
				label = label + " / " + other.output_port

			dot.edge(other.block.getBlockName(), block.getBlockName(), label)

	return dot