/**
  *
  *                      OOAS Compiler
  *
  *       Copyright 2015, AIT Austrian Institute of Technology.
  * This code is based on the C# Version of the OOAS Compiler, which is
  * copyright 2015 by the Institute of Software Technology, Graz University
  * of Technology with portions copyright by the AIT Austrian Institute of
  * Technology. All rights reserved.
  *
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
  *
  * If you modify the file please update the list of contributors below to in-
  * clude your name. Please also stick to the coding convention of using TABs
  * to do the basic (block-level) indentation and spaces for anything after
  * that. (Enable the display of special chars and it should be pretty obvious
  * what this means.) Also, remove all trailing whitespace.
  *
  * Contributors:
  *               Willibald Krenn (AIT)
  *               Stephan Zimmerer (AIT)
  *               Markus Demetz (AIT)
  *               Christoph Czurda (AIT)
  *
  */


package org.momut.ooas;

import java.io.InputStream;
import java.io.OutputStream;

import org.momut.ooas.codegen.ast.IAstDuplicator;
import org.momut.ooas.utils.exceptions.OoasCompilerRuntimeException;


public final class CompilerConfiguration {
	public enum LabelCompression { NoCompression, ActionsMethods, ActionsMethodsObjects, All }
	public enum Backend {Prolog, PrologSymbolic, Printer, CADP, CopyAST}
	private boolean configWriteable = true;
	private int searchDepth = 35;
	private String nameSpace = "as";
	private boolean quiet = false;
	private String namedTypePrefix = "";

	private final boolean streams;
	// options for non streaming interface
	private final Backend backend;
	private final String fileToParse;
	private final String outputToFile;
	// options for stream
	private final InputStream inputStream;
	private final OutputStream outputStream;
	private final String unitName;

	// compress options
	private LabelCompression compression;
	private IAstDuplicator<?> m_duplicator;


	private void checkWriteable() {
		if (!configWriteable)
			throw new OoasCompilerRuntimeException("CompilerConfig read-only.");
	}
	public boolean isWriteable() {return configWriteable;}
	void setWriteOnly(){
		configWriteable = false;
	}

	public boolean useStreams() {return streams;}
	public String getUnitName() {return unitName;}

	public String getFileToParse() {
		if (streams)
			throw new IllegalArgumentException("OOAS compiler was configured with input/output streams.");
		return fileToParse;
	}
	public String getFileToOutput() {
		if (streams)
			throw new IllegalArgumentException("OOAS compiler was configured with input/output streams.");
		return outputToFile;
	}

	public InputStream getInputStream() {
		if (!streams)
			throw new IllegalArgumentException("OOAS compiler was configured with input/output files.");
		return inputStream;
	}
	public OutputStream getOutputStream() {
		if (!streams)
			throw new IllegalArgumentException("OOAS compiler was configured with input/output files.");
		return outputStream;
	}

	public Backend getBackend() {return backend;}

	public int getMaxSearchDepth() {return searchDepth;}
	public CompilerConfiguration withMaxSearchDepth(int maxDepth) {
		checkWriteable();
		searchDepth = maxDepth;
		return this;
	}

	public String getNameSpace() {return nameSpace;}
	public CompilerConfiguration withNameSpace(String ns) {
		checkWriteable();
		nameSpace = ns;
		return this;
	}

	public boolean getQuiet() {return quiet;}
	public CompilerConfiguration withQuiet(boolean isQuiet){
		checkWriteable();
		quiet = isQuiet;
		return this;
	}

	public String getNamedTypePrefix() {return namedTypePrefix;}
	public CompilerConfiguration withNamedTypePrefix(String prfx) {
		checkWriteable();
		namedTypePrefix = prfx;
		return this;
	}

	public LabelCompression getLabelCompression() { return compression; }
	public CompilerConfiguration withLabelCompression(LabelCompression compression) {
		checkWriteable();
		this.compression = compression;
		return this;
	}

	public IAstDuplicator<?> getAstTarget() { return m_duplicator; }
	public CompilerConfiguration withAstTarget(IAstDuplicator<?> visitor) {
		m_duplicator = visitor;
		return this;
	}


	public CompilerConfiguration (String unitName, String parseFile, String outputFileName, Backend be) {
		fileToParse = parseFile;
		outputToFile = outputFileName;
		backend = be;
		streams = false;
		inputStream = null;
		outputStream = null;
		this.unitName = unitName;
		compression = LabelCompression.NoCompression;
	}

	public CompilerConfiguration (String unitName, InputStream input, OutputStream out, Backend be) {
		fileToParse = unitName;
		outputToFile = "";
		backend = be;
		streams = true;
		inputStream = input;
		outputStream = out;
		this.unitName = unitName;
		compression = LabelCompression.NoCompression;
	}

}
