package com.example.crystalgame.datawarehouse;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import android.util.Log;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;

import com.example.crystalgame.library.data.HasID;
import com.example.crystalgame.library.datawarehouse.DataWarehouse;
import com.example.crystalgame.library.datawarehouse.DataWarehouseException;
import com.example.crystalgame.library.datawarehouse.DataWrapper;
import com.example.crystalgame.library.datawarehouse.Synchronizer;
import com.example.crystalgame.library.events.InstructionEventListener;
import com.example.crystalgame.library.instructions.DataSynchronisationInstruction;
import com.example.crystalgame.library.util.RandomID;

/**
 * The client version of the DB4OInterface
 * @author Balazs Pete, Allen Thomas Varghese
 *
 */
public class ClientDataWarehouse extends DataWarehouse {

	public static String DB_PATH, myID, groupID;
	public static boolean isNull = true;
	
	protected ClientDataWarehouse(ObjectContainer store, Synchronizer synchroniser) {
		super(store, synchroniser);
	}

	private static ClientDataWarehouse instance;
	
	/**
	 * Get the instance of the Data warehouse
	 * <i>You must set the myID variable prior to accessing the instance</i>
	 * @return The data warehouse
	 */
	public static ClientDataWarehouse getInstance() {
		if(instance == null) {
			return new NullDataWarehouse();
		}
		
		isNull = false;
		return instance;
	}

	/**
	 * An empty CLientDataWarehouse
	 * @author Balazs Pete, Allen Thomas Warghese
	 *
	 */
	protected static class NullDataWarehouse extends ClientDataWarehouse {

		protected NullDataWarehouse() {
			super(null, null);
		}
		
		@Override
		public HasID put(@SuppressWarnings("rawtypes") Class type, HasID value) throws DataWarehouseException {
			throw DataWarehouseException.NULL_WAREHOUSE;
		}
		
		@Override
		public List<HasID> putList(@SuppressWarnings("rawtypes") Class type, List<HasID> value) throws DataWarehouseException {
			throw DataWarehouseException.NULL_WAREHOUSE;
		}

		@Override
		public HasID get(@SuppressWarnings("rawtypes") Class type, String id) throws DataWarehouseException {
			throw DataWarehouseException.NULL_WAREHOUSE;
		}

		@Override
		public boolean delete(@SuppressWarnings("rawtypes") Class type, String id) throws DataWarehouseException {
			throw DataWarehouseException.NULL_WAREHOUSE;
		}
		
		@Override
		public boolean deleteList(@SuppressWarnings("rawtypes") Class type, List<String> ids) throws DataWarehouseException {
			throw DataWarehouseException.NULL_WAREHOUSE;
		}

		@Override
		public List<HasID> getList(@SuppressWarnings("rawtypes") Class type) throws DataWarehouseException {
			throw DataWarehouseException.NULL_WAREHOUSE;
		}
		
		@Override
		public void passInstruction(DataSynchronisationInstruction instruction) {}
		
		@Override
		public void addInstructionEventListener(InstructionEventListener listener) {}
		
		@Override
		public void removeInstructionEventListener(InstructionEventListener listener) {}
		
	}
	
	public static void create() throws DataWarehouseException {
		if (DB_PATH == null) {
			throw DataWarehouseException.initialisationException("You must set the `DB_PATH` variable prior to using the DW");
		}
		
		if (myID == null) {
			throw DataWarehouseException.initialisationException("You must set the `myID` variable prior to using the DW");
		}
		
		File f = new File(DB_PATH + "/" + groupID);
		if (f.exists()) {
			f.delete();
		}
		
		ObjectContainer c = Db4oEmbedded.openFile(DB_PATH + "/" + groupID);
		instance = new ClientDataWarehouse(c, new ClientSynchronizer(c, myID));
	}
	
	
	public static void createFromWrappers(Serializable[] data) throws DataWarehouseException {
		create();
		
		if (data.length > 0) {
			instance.updateWithWrappers(data);
		}
		System.out.println("DW UPDATED");
	}
	
	@SuppressWarnings("unchecked")
	public void updateWithWrappers(Serializable[] data) {
		for (Serializable wrapper : data) {
			try {
				db.store(new DataWrapper<HasID>((DataWrapper<HasID>) wrapper));
			} catch (ClassCastException e) {
				System.out.println(((DataWrapper<HasID>)wrapper).getType());
			}
		}
		
		db.commit();
	}
	
}

@@ -113,16 +113,56 @@
 il.List;
+%0Aimport java.util.concurrent.FutureTask;
 %0A%0Aimport
@@ -1772,188 +1772,588 @@
 sID 
-put(@SuppressWarnings(%22rawtypes%22) Class type, HasID value) throws DataWarehouseException %7B%0A%09%09%09throw DataWarehouseException.NULL_WAREHOUSE;%0A%09%09%7D%0A%09%09%0A%09%09@Override%0A%09%09public List%3CHasID%3E p
+blockingPut(@SuppressWarnings(%22rawtypes%22) Class type, HasID value) throws DataWarehouseException %7B%0A%09%09%09throw DataWarehouseException.NULL_WAREHOUSE;%0A%09%09%7D%0A%09%09%0A%09%09@Override%0A%09%09public FutureTask%3CBoolean%3E put(@SuppressWarnings(%22rawtypes%22) Class type, HasID value) throws DataWarehouseException %7B%0A%09%09%09throw DataWarehouseException.NULL_WAREHOUSE;%0A%09%09%7D%0A%09%09%0A%09%09@Override%0A%09%09public FutureTask%3CBoolean%3E putList(@SuppressWarnings(%22rawtypes%22) Class type, List%3CHasID%3E value) throws DataWarehouseException %7B%0A%09%09%09throw DataWarehouseException.NULL_WAREHOUSE;%0A%09%09%7D%0A%09%09%0A%09%09@Override%0A%09%09public List%3CHasID%3E blockingP
 utLi
@@ -2687,31 +2687,43 @@
 de%0A%09%09public 
-b
+FutureTask%3CB
 oolean
+%3E
  delete(@Sup
@@ -2886,16 +2886,212 @@
  boolean
+ blockingDelete(@SuppressWarnings(%22rawtypes%22) Class type, String id) throws DataWarehouseException %7B%0A%09%09%09throw DataWarehouseException.NULL_WAREHOUSE;%0A%09%09%7D%0A%09%09%0A%09%09@Override%0A%09%09public FutureTask%3CBoolean%3E
  deleteL

Fix errors in ClientDataWarehouse