package edu.isi.karma.controller.command.alignment;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.isi.karma.controller.command.Command;
import edu.isi.karma.controller.command.CommandException;
import edu.isi.karma.controller.update.ErrorUpdate;
import edu.isi.karma.controller.update.SemanticTypesUpdate;
import edu.isi.karma.controller.update.TagsUpdate;
import edu.isi.karma.controller.update.UpdateContainer;
import edu.isi.karma.modeling.alignment.AlignToOntology;
import edu.isi.karma.modeling.alignment.Alignment;
import edu.isi.karma.modeling.alignment.AlignmentManager;
import edu.isi.karma.modeling.semantictypes.crfmodelhandler.CRFModelHandler;
import edu.isi.karma.rep.Node;
import edu.isi.karma.rep.Row;
import edu.isi.karma.rep.Worksheet;
import edu.isi.karma.rep.metadata.TagsContainer.TagName;
import edu.isi.karma.rep.semantictypes.SemanticTypes;
import edu.isi.karma.view.VWorkspace;

public class ResetModelCommand extends Command {

	private final String vWorksheetId;
	private SemanticTypes oldTypes;
	private Alignment oldAlignment;
	private String alignmentId;
	
	private static Logger logger = LoggerFactory
			.getLogger(ResetModelCommand.class);

	public ResetModelCommand(String id, String vWorksheetId) {
		super(id);
		this.vWorksheetId = vWorksheetId;
	}

	@Override
	public String getCommandName() {
		return this.getClass().getSimpleName();
	}

	@Override
	public String getTitle() {
		return "Reset Model";
	}

	@Override
	public String getDescription() {
		return null;
	}

	@Override
	public CommandType getCommandType() {
		return CommandType.undoable;
	}

	@Override
	public UpdateContainer doIt(VWorkspace vWorkspace) throws CommandException {
		Worksheet worksheet = vWorkspace.getViewFactory()
				.getVWorksheet(vWorksheetId).getWorksheet();

		//reset CRF model
		//CRFModelHandler.removeAllLabels();
		
		// Save the old SemanticType object for undo
		SemanticTypes types = worksheet.getSemanticTypes();
		oldTypes = types;
		worksheet.clearSemanticTypes();
		System.out.println("OLD TYPES=" + oldTypes.getTypes());
		
		//save old alignment for undo operation
		alignmentId = vWorkspace.getWorkspace().getId() + ":" + vWorksheetId + "AL";
		oldAlignment = AlignmentManager.Instance().getAlignment(alignmentId);
		
		// Remove the nodes (if any) from the outlier tag
		ArrayList<Row> rows = worksheet.getDataTable().getRows(0, worksheet.getDataTable().getNumRows());
		Set<String> nodeIds = new HashSet<String>();
		for(Row r:rows){
			//for each node in the row
			//logger.debug("Process ROW="+i++);
			for (Node n : r.getNodes()) {
				nodeIds.add(n.getId());
			}
		}
		vWorkspace.getWorkspace().getTagsContainer().getTag(TagName.Outlier)
				.removeNodeIds(nodeIds);

		// Update the container
		UpdateContainer c = new UpdateContainer();
		c.add(new SemanticTypesUpdate(worksheet, vWorksheetId));

		// Update the alignment
		AlignToOntology align = new AlignToOntology(worksheet, vWorkspace,
				vWorksheetId);
		try {
			align.update(c, true);
		} catch (Exception e) {
			logger.error("Error occured while resetting model!",
					e);
			return new UpdateContainer(new ErrorUpdate(
					"Error occured while resetting model!"));
		}
		c.add(new TagsUpdate());
		
		return c;
	}

	@Override
	public UpdateContainer undoIt(VWorkspace vWorkspace) {
		Worksheet worksheet = vWorkspace.getViewFactory()
		.getVWorksheet(vWorksheetId).getWorksheet();

		//reset old semantic types
		worksheet.setSemanticTypes(oldTypes);

		//set the old alignment
		AlignmentManager.Instance().addAlignmentToMap(alignmentId, oldAlignment);
		
		// Update the container
		UpdateContainer c = new UpdateContainer();
		c.add(new SemanticTypesUpdate(worksheet, vWorksheetId));

		// Update the alignment
		AlignToOntology align = new AlignToOntology(worksheet, vWorkspace,
				vWorksheetId);
		try {
			align.update(c, true);
		} catch (Exception e) {
			logger.error("Error occured while undoing alignment!",
					e);
			return new UpdateContainer(new ErrorUpdate(
			"Error occured while undoing alignment!"));
		}
		return c;

	}

}

@@ -995,16 +995,63 @@
 rkspace;
+%0Aimport edu.isi.mediator.gav.util.MediatorUtil;
 %0A%0Apublic
@@ -1225,16 +1225,45 @@
 mentId;%0A
+%09private String oldCRFModel;%0A
 %09%0A%09priva
@@ -1946,32 +1946,225 @@
 etWorksheet();%0A%0A
+%09%09//save CRF model for undo%0A%09%09try%7B%0A%09%09%09oldCRFModel=MediatorUtil.getFileAsString(%22./CRF_Model.txt%22);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09return new UpdateContainer(new ErrorUpdate(e.getMessage()));%0A%09%09%7D%0A
 %09%09//reset CRF mo
@@ -2169,18 +2169,16 @@
 model%0A%09%09
-//
 CRFModel
@@ -2364,16 +2364,18 @@
 es();%0A%09%09
+//
 System.o
@@ -3756,24 +3756,268 @@
 rksheet();%0A%0A
+%09%09//reset CRF model%0A%09%09try%7B%0A%09%09%09MediatorUtil.saveStringToFile(oldCRFModel, %22./CRF_Model.txt%22);%0A%09%09%09CRFModelHandler.readModelFromFile(%22./CRF_Model.txt%22);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09return new UpdateContainer(new ErrorUpdate(e.getMessage()));%0A%09%09%7D%0A
 %09%09//reset ol

reset CRF model