package i5.las2peer.persistency.helper;

import java.io.Serializable;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import i5.las2peer.api.StorageCollisionHandler;
import i5.las2peer.api.StorageExceptionHandler;
import i5.las2peer.api.StorageStoreResultHandler;
import i5.las2peer.api.exceptions.EnvelopeAlreadyExistsException;
import i5.las2peer.api.exceptions.StopMergingException;
import i5.las2peer.persistency.Envelope;

public class StoreProcessHelper implements StorageStoreResultHandler, StorageCollisionHandler, StorageExceptionHandler {

	private Exception exception;
	private int successfulOperations = -1;

	@Override
	public void onResult(Serializable serializable, int successfulOperations) {
		synchronized (this) {
			this.successfulOperations = successfulOperations;
		}
	}

	@Override
	public Serializable onCollision(Envelope toStore, Envelope inNetwork, long numberOfCollisions)
			throws StopMergingException {
		synchronized (this) {
			exception = new EnvelopeAlreadyExistsException(
					"Envelope with this version already exists in network! Use a collision handler or increment version.");
			// stop merging process
			throw new StopMergingException();
		}
	}

	@Override
	public List<PublicKey> mergeReaders(HashMap<PublicKey, byte[]> toStoreReaders,
			HashMap<PublicKey, byte[]> inNetworkReaders) {
		// nothing to merge here in store operations there should be nothing to merge
		return new ArrayList<>();
	}

	@Override
	public void onException(Exception e) {
		synchronized (this) {
			if (exception == null) {
				exception = e;
			}
		}
	}

	public int getResult() throws Exception {
		synchronized (this) {
			if (exception != null) {
				throw exception;
			}
			return successfulOperations;
		}
	}

}

@@ -1067,14 +1067,8 @@
 ion(
-%0A%09%09%09%09%09
 %22Env
@@ -1077,25 +1077,95 @@
 ope 
-with this version
+'%22 + toStore.getIdentifier() + %22' with version (%22%0A%09%09%09%09%09+ toStore.getVersion()%0A%09%09%09%09%09+ %22)
  alr
@@ -1219,25 +1219,43 @@
  or 
-increment version
+fetch latest version before storing
 .%22);

improve EnvelopeAlreadyExistsException message in store process