package ca.blarg.gdx.loaders.textureatlas;

import ca.blarg.gdx.graphics.atlas.CustomGridTextureAtlas;
import ca.blarg.gdx.graphics.atlas.TextureAtlas;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Json;

public class TextureAtlasJsonLoader {
	public static JsonTextureAtlasDefinition load(FileHandle file) {
		Json json = new Json();
		return json.fromJson(JsonTextureAtlasDefinition.class, file);
	}

	public static TextureAtlas create(JsonTextureAtlasDefinition definition) {
		return create(definition, null);
	}

	public static TextureAtlas create(JsonTextureAtlasDefinition definition, AssetManager assetManager) {
		if (definition.texture == null)
			throw new RuntimeException("No texture specified.");
		if (definition.tiles == null || definition.tiles.size() == 0)
			throw new RuntimeException("No tiles defined.");

		Texture texture;
		if (assetManager != null)
			texture = assetManager.get(definition.texture, Texture.class);
		else
			texture = new Texture(Gdx.files.internal(definition.texture));

		CustomGridTextureAtlas atlas = new CustomGridTextureAtlas(texture);
		for (int i = 0; i < definition.tiles.size(); ++i) {
			JsonTextureAtlasTile tile = definition.tiles.get(i);
			// TODO: parameter value error checking
			if (tile.autogrid)
				atlas.addGrid(tile.x, tile.y, tile.tileWidth, tile.tileHeight, tile.numTilesX, tile.numTilesY, tile.border);
			else
				atlas.add(tile.x, tile.y, tile.width, tile.height);
		}

		if (definition.animations != null && definition.animations.size() > 0) {
			for (int i = 0; i < definition.animations.size(); ++i) {
				JsonTextureAtlasAnimation animation = definition.animations.get(i);
				atlas.addAnimation(animation.name, animation.tileIndex, animation.startIndex, animation.endIndex, animation.delay, animation.loop);
			}
		}

		return atlas;
	}

	public static TextureAtlas loadAndCreate(FileHandle file) {
		JsonTextureAtlasDefinition definition = load(file);
		return create(definition);
	}

	public static TextureAtlas loadAndCreate(FileHandle file, AssetManager assetManager) {
		JsonTextureAtlasDefinition definition = load(file);
		return create(definition, assetManager);
	}
}

@@ -149,37 +149,8 @@
 as;%0A
-import com.badlogic.gdx.Gdx;%0A
 impo
@@ -190,16 +190,16 @@
 anager;%0A
+
 import c
@@ -513,123 +513,8 @@
 %09%7D%0A%0A
-%09public static TextureAtlas create(JsonTextureAtlasDefinition definition) %7B%0A%09%09return create(definition, null);%0A%09%7D%0A%0A
 %09pub
@@ -833,48 +833,8 @@
 ure 
-texture;%0A%09%09if (assetManager != null)%0A%09%09%09
 text
@@ -895,81 +895,8 @@
 ss);
-%0A%09%09else%0A%09%09%09texture = new Texture(Gdx.files.internal(definition.texture));
 %0A%0A%09%09
@@ -1673,16 +1673,16 @@
 %09%7D%0A%09%09%7D%0A%0A
+
 %09%09return
@@ -1697,156 +1697,8 @@
 %09%7D%0A%0A
-%09public static TextureAtlas loadAndCreate(FileHandle file) %7B%0A%09%09JsonTextureAtlasDefinition definition = load(file);%0A%09%09return create(definition);%0A%09%7D%0A%0A
 %09pub

remove support for loading via json file without an AssetManager