package de.tu_dresden.lat.proofs.json;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

import de.tu_dresden.lat.proofs.data.FormattingException;
import de.tu_dresden.lat.proofs.interfaces.IProof;
import de.tu_dresden.lat.proofs.interfaces.IProofWriter;

/**
 * Write proof objects into JSon files.
 *
 * @author Christian Alrabbaa
 *
 */
public class JsonProofWriter implements IProofWriter {

	public final static String FILE_ENDING = ".json";

	public JsonProofWriter() {
	}

	private static class LazyHolder {
		static JsonProofWriter instance = new JsonProofWriter();
	}

	public static JsonProofWriter getInstance() {
		return LazyHolder.instance;
	}

	@Override
	public void writeToFile(Collection<IProof> proofs, String filePrefix) throws IOException {

		ObjectMapper mapper = new ObjectMapper();
		ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());

		writer.writeValue(new File(filePrefix + FILE_ENDING), proofs);

	}

	@Override
	public void writeToFile(IProof proof, String filePrefix) throws IOException {
		ObjectMapper mapper = new ObjectMapper();
		ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());

		writer.writeValue(new File(filePrefix + FILE_ENDING), proof);
	}

	@Override
	public String toString(IProof proof) throws FormattingException {
		ObjectMapper mapper = new ObjectMapper();
		ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());

		ByteArrayOutputStream output = new ByteArrayOutputStream();

		try {
			writer.writeValue(output, proof);
		} catch (IOException e) {
			throw new FormattingException(e);
		}

		return output.toString();
	}
}
