package com.irtsaintexupery.fiacre.simulator.msc.generator.popup.actions

import com.irtsaintexupery.fiacre.simulator.msc.msc.MSC
import com.irtsaintexupery.fiacre.simulator.msc.msc.EnterStateStep
import com.irtsaintexupery.fiacre.simulator.msc.msc.SynchronisationStep
import com.irtsaintexupery.fiacre.simulator.msc.msc.SendStep
import com.irtsaintexupery.fiacre.simulator.msc.msc.MicroStep

class MSCGenerator {
	var MSC msc

	new(MSC _msc) {
		msc = _msc
	}

	def String generate() {
		var String generated
		generated = "msc{" + "\n\n\t"
		generated += "# The entities " + "\n" + "\t"

		for (site : msc.sites) {
			if(msc.sites.last != site) {
				generated += site.name + ", "
			} else {
				generated += site.name + ";\n\t"
			}
		}
		generated += "# The relations " + "\n" + "\t"
		generated += "|||;\n"

		for (step : msc.steps) {
			for (micro : step.microSteps) {
				if(step.microSteps.last != micro) {
					generated += generate(micro) + ", "
				} else {
					generated += generate(micro) + ";\n\t"
				}
			}
			generated += "|||;\n"
		}
		generated += "\n}"
		generated
	}
	def generate(MicroStep micro) {
		if(micro instanceof EnterStateStep)
			generate(micro as EnterStateStep)
		else if(micro instanceof SynchronisationStep)
			generate(micro as SynchronisationStep)
		else
			generate(micro as SendStep)
	}

	def String generate(EnterStateStep esst) {
		esst.site.name+" box "+ esst.site.name +"[label=\"" +esst.stateName+"\"]"
	}

	def String generate(SynchronisationStep sst) {
		if(sst.sites.size ==1){
			sst.sites.get(0).name+" abox "+ sst.sites.get(0).name +"[label=\"sync\"]"
		}
		else{
			sst.sites.get(0).name +" :: "+sst.sites.get(1).name
		}
	}

	def String generate(SendStep sst) {
		sst.senderSites.get(0).name+ " => "+ sst.receiverSites.get(0).name	
	}
}