package ca.blarg.gdx.graphics.atlas;

import ca.blarg.gdx.math.MathHelpers;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;

public abstract class TextureAtlas {
	public static final float TEXCOORD_EDGE_BLEED_OFFSET = 0.02f;

	public final Texture texture;

	protected final float edgeCoordOffset;
	protected Array<TextureRegion> tiles = new Array<TextureRegion>(TextureRegion.class);
	protected ObjectMap<String, AnimationSequence> animations = new ObjectMap<String, AnimationSequence>();

	public TextureAtlas(Texture texture, float edgeCoordOffset) {
		this.texture = texture;
		this.edgeCoordOffset = edgeCoordOffset;
	}

	public TextureRegion get(int index) {
		return tiles.items[index];
	}

	public int count() {
		return tiles.size;
	}

	public boolean hasAnimations() {
		return animations.size > 0;
	}

	public ObjectMap.Entries<String, AnimationSequence> getAnimations() {
		return animations.entries();
	}

	public static void scaleTexCoord(Vector2 texCoord, TextureRegion tile, Vector2 out) {
		out.x = scaleTexCoordU(texCoord.x, tile);
		out.y = scaleTexCoordV(texCoord.y, tile);
	}

	public static void scaleTexCoord(Vector2 texCoord, float srcMin, float srcMax, TextureRegion tile, Vector2 out) {
		out.x = scaleTexCoordU(texCoord.x, srcMin, srcMax, tile);
		out.y = scaleTexCoordV(texCoord.y, srcMin, srcMax, tile);
	}

	public static float scaleTexCoordU(float u, TextureRegion tile) {
		return MathHelpers.scaleRange(u, 0.0f, 1.0f, tile.getU(), tile.getU2());
	}

	public static float scaleTexCoordU(float u, float srcMinU, float srcMaxU, TextureRegion tile) {
		return MathHelpers.scaleRange(u, srcMinU, srcMaxU, tile.getU(), tile.getU2());
	}

	public static float scaleTexCoordV(float v, TextureRegion tile) {
		return MathHelpers.scaleRange(v, 0.0f, 1.0f, tile.getV(), tile.getV2());
	}

	public static float scaleTexCoordV(float v, float srcMinV, float srcMaxV, TextureRegion tile) {
		return MathHelpers.scaleRange(v, srcMinV, srcMaxV, tile.getV(), tile.getV2());
	}
}

@@ -317,16 +317,180 @@
 Atlas %7B%0A
+%09public class Animation %7B%0A%09%09public String name;%0A%09%09public int destTileIndex;%0A%09%09public int start;%0A%09%09public int stop;%0A%09%09public float delay;%0A%09%09public boolean loop;%0A%09%7D%0A%0A
 %09public 
@@ -544,16 +544,16 @@
  0.02f;%0A
-
 %0A%09public
@@ -742,24 +742,16 @@
 nimation
-Sequence
 %3E animat
@@ -788,24 +788,16 @@
 nimation
-Sequence
 %3E();%0A%0A%09p
@@ -1163,16 +1163,8 @@
 tion
-Sequence
 %3E ge
@@ -1210,24 +1210,503 @@
 ries();%0A%09%7D%0A%0A
+%09public void addAnimation(String name, int destTileIndex, int start, int stop, float delay, boolean loop) %7B%0A%09%09if (animations.containsKey(name))%0A%09%09%09throw new UnsupportedOperationException(%22Duplicate animation sequence name.%22);%0A%0A%09%09Animation animation = new Animation();%0A%09%09animation.name = name;%0A%09%09animation.destTileIndex = destTileIndex;%0A%09%09animation.start = start;%0A%09%09animation.stop = stop;%0A%09%09animation.delay = delay;%0A%09%09animation.loop = loop;%0A%09%09animations.put(name, animation);%0A%09%7D%0A%0A
 %09public stat

use separate Animation class for now to hold info. new addAnimation method. TextureAtlasAnimator will read through these and set up it's own data structures from them