package com.backfire.Animation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import com.backfire.arithmetic.CardJudge.CardType;

/**
 * @author wf
 *
 */
public class AnimationManager {
	// Animation List
	private ArrayList<Animation> animationList = new ArrayList<Animation>();
	
	// Animation Event listener
	private HashMap<Animation, IAnimationCallback> listeners = new HashMap<Animation, IAnimationCallback>();
	
	private static AnimationManager animationMgr = null;
	
	// Interfaces
	public interface ListAnimationCallback {
		public boolean onAnimation(int id, Animation a);
	}
	
	public interface IAnimationCallback {
		public void animeWillFinished(Animation anim);
	}
	
	public interface IAnimationSequence {
		// when the anime is added in a animationSequence, after the animation is done, this method will be call
		// only use in AnimationSequence
		public void onSequenceAnimeNotify(Animation anim);
	}
	
	
	// Methods
	
	private AnimationManager() {
		
	}
	
	public static AnimationManager getInstance() {
		if (animationMgr == null) {
			animationMgr = new AnimationManager();
		}
		
		return animationMgr;
	}
	
	public void clearAll() {
		clearAnimation();
	}
	
	private byte[] syncAnimation = new byte[0];
	
	public void ListAnimation(ListAnimationCallback callback) {
		synchronized (syncAnimation) {
			for (int i = 0; i < animationList.size(); i++) {
				if (!callback.onAnimation(i, animationList.get(i)))
					break;
			}
		}
	}
	
	public void addAnimation(Animation a) {
		synchronized (syncAnimation) {
			if (animationList.contains(a))
				return;
		}
		this.addAnimation(a, null);
	}
	
	public void removeAnimation(Animation a) {
		synchronized (syncAnimation) {
			if (a.sequenceCallback != null)
			{
				a.sequenceCallback.onSequenceAnimeNotify(a);
			}
			a.overstatue();
			animationList.remove(a);
			listeners.remove(a);
			a=null;
		}
	}
	
	public void removeAnimationByTag(int tag) {
		ArrayList<Animation> animeToRemove = new ArrayList<Animation>();
		synchronized (syncAnimation) {
			for (Animation anim : animationList) {
				if (anim.tag == tag) {
					animeToRemove.add(anim);
				}
			}
		}
		for (Animation anim : animeToRemove) {
			removeAnimation(anim);
		}
		animeToRemove.clear();
	}
	
	public void addAnimation(Animation a, IAnimationCallback c) {
		synchronized (syncAnimation) {
			if(null==a)return;
			animationList.add(a);
			listeners.put(a, c);
			Collections.sort(animationList);
		}
	}
	
	public ArrayList<Animation> getAnimations() {
		synchronized(syncAnimation) {
			return animationList;
		}
	}
	
	public void deleteAnimationUnuse() {
		synchronized (syncAnimation) {
			Animation a;
			for (int i = 0; i < animationList.size(); i++) {
				a = animationList.get(i);
				if (a.deleteFlag) {
					a.unload();
					
					IAnimationCallback listener = listeners.get(a);
					if (listener != null) {
						listener.animeWillFinished(a);
					}
					
					animationList.remove(a);
				}
			}
		}
	}
	
	private void clearAnimation() {
		synchronized (syncAnimation) {
			animationList.clear();
		}
	}
	
}
