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

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.os.StatFs;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;

public class Utils {
	private static final String TAG = "BitmapCommonUtils";
	private static final long POLY64REV = -7661587058870466123L;
	private static final long INITIALCRC = -1L;
	private static long[] sCrcTable = new long[256];

	static {
		for (int i = 0; i < 256; i++) {
			long part = i;
			for (int j = 0; j < 8; j++) {
				long x = ((int) part & 0x1) != 0 ? -7661587058870466123L : 0L;
				part = part >> 1 ^ x;
			}
			sCrcTable[i] = part;
		}
	}

	public static File getDiskCacheDir(Context context, String uniqueName) {
		String cachePath = "mounted".equals(Environment.getExternalStorageState())
				? getExternalCacheDir(context).getPath() : context.getCacheDir().getPath();

		return new File(cachePath + File.separator + uniqueName);
	}

	public static int getBitmapSize(Bitmap bitmap) {
		return bitmap.getRowBytes() * bitmap.getHeight();
	}

	public static File getExternalCacheDir(Context context) {
		String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
		return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
	}

	public static long getUsableSpace(File path) {
		try {
			StatFs stats = new StatFs(path.getPath());
			return stats.getBlockSize() * stats.getAvailableBlocks();
		} catch (Exception e) {
			LogUtil.e("BitmapCommonUtils", "获取 sdcard 缓存大小 出错，请查看AndroidManifest.xml 是否添加了sdcard的访问权限");
			e.printStackTrace();
		}
		return -1L;
	}

	public static byte[] getBytes(String in) {
		byte[] result = new byte[in.length() * 2];
		int output = 0;
		for (char ch : in.toCharArray()) {
			result[(output++)] = (byte) (ch & 0xFF);
			result[(output++)] = (byte) (ch >> '\b');
		}
		return result;
	}

	public static boolean isSameKey(byte[] key, byte[] buffer) {
		int n = key.length;
		if (buffer.length < n) {
			return false;
		}
		for (int i = 0; i < n; i++) {
			if (key[i] != buffer[i]) {
				return false;
			}
		}
		return true;
	}

	public static byte[] copyOfRange(byte[] original, int from, int to) {
		int newLength = to - from;
		if (newLength < 0)
			throw new IllegalArgumentException(from + " > " + to);
		byte[] copy = new byte[newLength];
		System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
		return copy;
	}

	public static byte[] makeKey(String httpUrl) {
		return getBytes(httpUrl);
	}

	public static final long crc64Long(String in) {
		if ((in == null) || (in.length() == 0)) {
			return 0L;
		}
		return crc64Long(getBytes(in));
	}

	public static final long crc64Long(byte[] buffer) {
		long crc = -1L;
		int k = 0;
		for (int n = buffer.length; k < n; k++) {
			crc = sCrcTable[(((int) crc ^ buffer[k]) & 0xFF)] ^ crc >> 8;
		}
		return crc;
	}
}