package org.thg.logic.factorys;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;

import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Texture;

public class ResourceFactory {
	/**
	 * 在进入次日时清空缓存
	 */
	public static void clearPool() {
		Iterator<Entry<Integer, Texture>> cpIter = cpPool.entrySet().iterator();
		while(cpIter.hasNext())
			cpIter.next().getValue().dispose();
		cpPool.clear();
		Iterator<Entry<Integer, Texture>> bgIter = bgPool.entrySet().iterator();
		while(bgIter.hasNext())
			bgIter.next().getValue().dispose();
		bgPool.clear();
		
		Iterator<Texture> iter = bufferTextureList.iterator();
		while(iter.hasNext()) {
			iter.next().dispose();
		}
		bufferTextureList.clear();
		//TODO
	}
	
	private static LinkedList<Texture> bufferTextureList = new LinkedList<Texture>();
	/** <p>将texture对象压入缓存顶，之后无需关系其销毁 <p><b>背景专用 */
	public static void putBgBufferTexture(Texture bufferTexture) {
		bufferTextureList.add(bufferTexture);
	}
	/** <p>从缓存取用顶上的texture对象，但是不能取出 <p><b>背景专用 */
	public static Texture getBgBufferTexture() {
		if(bufferTextureList.isEmpty()) return null;
		else return bufferTextureList.getLast();
	}
	
	
	public static Music getBgm(int num) {
		
		//0返回null
		//TODO
		return null;
	}
	
	private static HashMap<Integer, Texture> bgPool = new HashMap<Integer, Texture>();
	/**
	 * <p>向仓库获取背景资源
	 * <p>若不是普通的资源，则自动向缓存索取
	 */
	public static Texture getBg(int num) {
		if(bgPool.get(num) != null) return bgPool.get(num);
		try {
			Texture t = new Texture("images/resources/bg/" + Integer.toString(10000 + num) + ".png");
			bgPool.put(num, t);
			return t;
		}catch(Exception e) {
			return getBgBufferTexture();
		}
	}
	
	
	private static HashMap<Integer, Texture> cpPool = new HashMap<Integer, Texture>();
	public static Texture getCharactorPic(int num) {
		if(num == 0) return null;
		if(cpPool.get(num) != null) return cpPool.get(num);
		
		try {
			Texture t = new Texture("images/resources/charactorpic/" + Integer.toString(num + 10000) + ".png");
			cpPool.put(num, t);
			return t;
			
		}catch(Exception e) { 
			System.err.println("ResourceFactory.getCharactorPic() error");
			return null;
		}
	}
	public static Music getVoice(int num) {
//		TODO
		
		return null;
	}
	
	public static final int PRE_INT = -1;
}

@@ -627,153 +627,8 @@
 ();%0A
-%09%09%0A%09%09Iterator%3CTexture%3E iter = bufferTextureList.iterator();%0A%09%09while(iter.hasNext()) %7B%0A%09%09%09iter.next().dispose();%0A%09%09%7D%0A%09%09bufferTextureList.clear();%0A
 %09%09//
@@ -657,29 +657,19 @@
 tic 
-LinkedList%3C
 Texture
-%3E
  b
+gB
 uffe
@@ -680,40 +680,15 @@
 ture
-List = new LinkedList%3CTexture%3E()
+ = null
 ;%0A%09/
@@ -799,17 +799,22 @@
 re) %7B%0A%09%09
-b
+if(bgB
 ufferTex
@@ -821,31 +821,78 @@
 ture
-List.add(bufferTexture)
+ != null) bgBufferTexture.dispose();%0A%09%09bgBufferTexture = bufferTexture
 ;%0A%09%7D
@@ -991,68 +991,16 @@
 %7B%0A%09%09
-if(bufferTextureList.isEmpty()) return null;%0A%09%09else 
 return b
 uffe
@@ -995,16 +995,18 @@
 return b
+gB
 ufferTex
@@ -1013,22 +1013,8 @@
 ture
-List.getLast()
 ;%0A%09%7D

修改了一下缓存实现， 该背景缓存不应该被onpause清理