package net.mgsx.pd.patch;

import java.util.concurrent.CountDownLatch;

import org.puredata.core.PdListener;

import com.badlogic.gdx.assets.*;
import com.badlogic.gdx.assets.loaders.AsynchronousAssetLoader;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.Array;

import net.mgsx.pd.Pd;
import net.mgsx.pd.utils.PdRuntimeException;

public class PatchLoader extends AsynchronousAssetLoader<PdPatch, AssetLoaderParameters<PdPatch>>
{
	private PdPatch patch;
	private boolean asyncPatches;
	private volatile int loadingPatch;
	private volatile CountDownLatch loadingLatch;
	
	public PatchLoader(FileHandleResolver resolver, boolean enableAsync) {
		super(resolver);
		asyncPatches = enableAsync;
		if(enableAsync) {
			Pd.audio.addListener("patchLoaded", new PdListener.Adapter() {
				@Override
				public void receiveFloat(String source, float x) {
					if(MathUtils.isEqual(loadingPatch, x)) {
						loadingLatch.countDown();
					}
				}
			});
		}
	}

	@Override
	public void loadAsync(AssetManager manager, String fileName, FileHandle file,
			AssetLoaderParameters<PdPatch> parameter) {
		patch = Pd.audio.open(file);
		if(asyncPatches) {
			loadingPatch = patch.getPdHandle();
			loadingLatch = new CountDownLatch(1);
			try {
				loadingLatch.await();
			} catch (InterruptedException e) {
				throw new PdRuntimeException("Error waiting for patch load", e);
			}
		}
	}

	@Override
	public PdPatch loadSync(AssetManager manager, String fileName, FileHandle file,
			AssetLoaderParameters<PdPatch> parameter) {
		return patch;
	}

	@Override
	public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file,
			AssetLoaderParameters<PdPatch> parameter) {
		// no deps
		return null;
	}

}

@@ -516,27 +516,35 @@
 dPatch, 
-Asset
+Patch
 Loader
+.PdPatch
 Paramete
@@ -544,26 +544,16 @@
 arameter
-s%3CPdPatch%3E
 %3E%0A%7B%0A%09pri
@@ -589,28 +589,28 @@
 boolean 
-asyncPatches
+defaultAsync
 ;%0A%09priva
@@ -745,22 +745,23 @@
 boolean 
-enable
+default
 Async) %7B
@@ -786,59 +786,44 @@
 ;%0A%09%09
-asyncPatches = enableAsync;%0A%09%09if(enable
+this.defaultAsync = default
 Async
-) %7B
+;
 %0A%09%09
-%09
 Pd.a
@@ -884,17 +884,16 @@
 () %7B%0A%09%09%09
-%09
 @Overrid
@@ -894,17 +894,16 @@
 verride%0A
-%09
 %09%09%09publi
@@ -952,17 +952,16 @@
 ) %7B%0A%09%09%09%09
-%09
 if(MathU
@@ -998,17 +998,16 @@
  %7B%0A%09%09%09%09%09
-%09
 loadingL
@@ -1024,17 +1024,16 @@
 Down();%0A
-%09
 %09%09%09%09%7D%0A%09%09
@@ -1037,22 +1037,16 @@
 %0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D);%0A
-%09%09%7D%0A
 %09%7D%0A%0A
@@ -1134,35 +1134,43 @@
 le file,%0A%09%09%09
-Asset
+Patch
 Loader
+.PdPatch
 Parameters%3CP
@@ -1162,34 +1162,24 @@
 tchParameter
-s%3CPdPatch%3E
  parameter) 
@@ -1220,20 +1220,64 @@
 %09if(
-asyncPatches
+defaultAsync %7C%7C parameter != null && parameter.loadAsync
 ) %7B%0A
@@ -1602,35 +1602,43 @@
 le file,%0A%09%09%09
-Asset
+Patch
 Loader
+.PdPatch
 Parameters%3CP
@@ -1630,34 +1630,24 @@
 tchParameter
-s%3CPdPatch%3E
  parameter) 
@@ -1763,27 +1763,35 @@
 ile,%0A%09%09%09
-Asset
+Patch
 Loader
+.PdPatch
 Paramete
@@ -1795,18 +1795,8 @@
 eter
-s%3CPdPatch%3E
  par
@@ -1835,12 +1835,124 @@
 null;%0A%09%7D
+%0A%09%0A%09public static class PdPatchParameter extends AssetLoaderParameters%3CPdPatch%3E %7B%0A%09%09public boolean loadAsync;%0A%09%7D
 %0A%0A%7D%0A

allow per file control for async patch loading