package cn.net.caoyao.cpzshandset.common.bitmap.core;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import cn.net.caoyao.cpzshandset.common.bitmap.download.Downloader;

public class BitmapProcess {
    private Downloader mDownloader;
    private BitmapCache mCache;
    private static final int BYTESBUFFE_POOL_SIZE = 4;
    private static final int BYTESBUFFER_SIZE = 204800;
    private static final BytesBufferPool sMicroThumbBufferPool = new BytesBufferPool(BYTESBUFFE_POOL_SIZE, BYTESBUFFER_SIZE);

    public BitmapProcess(Downloader downloader, BitmapCache cache) {
        this.mDownloader = downloader;
        this.mCache = cache;
    }

    public Bitmap getBitmap(String url, BitmapDisplayConfig config) {
        Bitmap bitmap = getFromDisk(url, config);
//        LogUtil.v(TConfig.Echo, "getFromDisk:" + bitmap);
        if (bitmap == null) {
            byte[] data = this.mDownloader.download(url);
            if ((data != null) && (data.length > 0)) {
                if (config != null) {
                    //decodeSampledBitmapFromByteArray  此处有图片的压缩方法
                    bitmap = BitmapDecoder.decodeSampledBitmapFromByteArray(data, 0, data.length,
                            config.getBitmapWidth(), config.getBitmapHeight(),config.getScreenWidth(),config.getScreenHeight());
                } else {

                    return BitmapFactory.decodeByteArray(data, 0, data.length);
                }
                this.mCache.addToDiskCache(url, data);
            }
        }
        return bitmap;
    }

    public Bitmap getFromDisk(String key, BitmapDisplayConfig config) {
        BytesBufferPool.BytesBuffer buffer = sMicroThumbBufferPool.get();
        Bitmap b = null;

        label102:
        try {
            boolean found = this.mCache.getImageData(key, buffer);
            if ((found) && (buffer.length - buffer.offset > 0)) {
                if (config != null) {
                    b = BitmapDecoder.decodeSampledBitmapFromByteArray(buffer.data, buffer.offset, buffer.length,
                            config.getBitmapWidth(), config.getBitmapHeight(),config.getScreenWidth(),config.getScreenHeight());
                    break label102;
                }
                b = BitmapFactory.decodeByteArray(buffer.data, buffer.offset, buffer.length);
            }
        } finally {
            sMicroThumbBufferPool.recycle(buffer);
        }
        return b;
    }
}