package com.backfire.doudizhu.common;

import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * @author wf
 *
 */
public class BitmapManager {
	
	private byte[] sync = new byte[0];
	private Bitmap nullBmp = null;
	private HashMap<Integer, SoftReference<Bitmap>> bitmaps = new HashMap<Integer, SoftReference<Bitmap>>();
	
	
	private void createNull() {
		if (null == nullBmp) {
			nullBmp = Bitmap.createBitmap(1, 1, Config.RGB_565);
		}
	}
	
	private Bitmap getSoftBitmap(int resID){
		createNull();
		Bitmap bitmap = null;
		SoftReference<Bitmap> ref = bitmaps.get(resID);
		if (null != ref) {
			bitmap = ref.get();
			if (null != bitmap) {
				return bitmap;
			}
			bitmaps.remove(resID);
		}
		
		return bitmap;
	}
	
	
	public Bitmap getBitmap(int resID, int scaleToWidth, int scaleToHeight) {
		if(resID<=0) return null;
		synchronized (sync) {
			Bitmap bitmap =  getSoftBitmap(resID);
			if (null == bitmap) {
				bitmap =  getBitmapFromRaw(resID);
				if (null == bitmap) {
					return nullBmp;
				}
			}else{
				return bitmap;
			}
			if (scaleToWidth > 0 && scaleToHeight > 0) {
				try {
					bitmap = Bitmap.createScaledBitmap(bitmap, scaleToWidth, scaleToHeight, true);
				}
				catch (final Throwable e) {
					System.gc();
				}
			}
			
			bitmaps.put(resID, new SoftReference<Bitmap>(bitmap));
			return bitmap;
		}
	}
	
	public Bitmap getBitmap(int resID, Matrix mat) {
		if(resID<=0) return null;
		synchronized (sync) {
			Bitmap bitmap =  getSoftBitmap(resID);
			if (null == bitmap) {
				bitmap =  getBitmapFromRaw(resID);
				if (null == bitmap) {
					return nullBmp;
				}
			}else{
				return bitmap;
			}
			if (null != mat) {
				bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);
			}
			bitmaps.put(resID, new SoftReference<Bitmap>(bitmap));
			return bitmap;
		}
	}
	//openRawResource既可以读取RAW下文件，也可以读取drawable下文件
	private Bitmap getBitmapFromRaw(int resID){
		try {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig=Bitmap.Config.RGB_565;//表示16位位图 565代表对应三原色占的位数
			opt.inInputShareable=true;
			opt.inPurgeable=true;//设置图片可以被回收
			InputStream is = DdzApplication.getApplication().getApplicationContext().getResources().openRawResource(resID);
			return BitmapFactory.decodeStream(is, null, opt);
		}
		catch (final Throwable e) {
			System.gc();
			return nullBmp;
		}
	}
	private Bitmap getBitmap(int resID){
		createNull();
		Bitmap bitmap = null;
		SoftReference<Bitmap> ref = bitmaps.get(resID);
		if (null != ref) {
			bitmap = ref.get();
			if (null != bitmap) {
				return bitmap;
			}
			bitmaps.remove(resID);
		}
		
		return bitmap;
	}
	public Bitmap getBitmap(Bitmap image, int scaleToWidth, int scaleToHeight){
		Bitmap bitmap = null;
		if (image!=null && scaleToWidth > 0 && scaleToHeight > 0) {
			try {
				bitmap = Bitmap.createScaledBitmap(image, scaleToWidth, scaleToHeight, true);
			}
			catch (final Throwable e) {
				System.gc();
			}
		}
		
		return bitmap;
	}
	public void release() {
		synchronized (sync) {
			System.gc();
			Bitmap bitmap;
			for (int resID : bitmaps.keySet()) {
				SoftReference<Bitmap> ref = bitmaps.get(resID);
				if (null == ref) {
					continue;
				}
				
				bitmap = ref.get();
				if (null == bitmap) {
					continue;
				}
				if (false == bitmap.isRecycled()) {
					bitmap.recycle();
				}
				bitmap = null;
			}
			bitmaps.clear();
		}
	}
	public void clear() {
		synchronized (sync) {
			System.gc();
			Bitmap bitmap;
			for (int resID : bitmaps.keySet()) {
				SoftReference<Bitmap> ref = bitmaps.get(resID);
				if (null == ref) {
					continue;
				}
				
				bitmap = ref.get();
				if (null == bitmap) {
					continue;
				}
				bitmap = null;
			}
			bitmaps.clear();
		}
	}
}
