package com.xixun.unique;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


import android.os.SystemProperties;
import android.util.Log;

/**
 * 根据读到的卡设备判断不同的CardType型号
 * @author xixun
 *
 */
public class BoardCard {
	private static final String TAG = "CardSystemAPP";

	public byte[] readFile(String fileName) {
		FileInputStream fis = null;
		byte[] content = null;

		try {
			fis = new FileInputStream(fileName);
			content = new byte[fis.available()];
			fis.read(content);
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return content;
	}

	public CardType getCardType() {
		CardType type = CardType.UNKNOWN;
		String product = android.os.Build.PRODUCT;
		String device = android.os.Build.DEVICE;
		String model = android.os.Build.MODEL;
		String display = android.os.Build.DISPLAY;

		if (product.equals("sugar_ref001") && device.equals("sugar-ref001") && model.equals("QY-A20")) {
			type = CardType.B;
		} else if (product.equals("full_mini210") && device.equals("mini210") && model.equals("Full Android on MINI210")) {
			type = CardType.Y;
			File file = new File("/data/joey/recovery/devicepara");
			if (file.exists()) {
				byte[] readBytes = readFile(file.getAbsolutePath());
				String content = new String(readBytes);
				if (content.contains("framerate = 30")
						&& content.contains("width = 1280")
						&& content.contains("height = 1024")) {
					type = CardType.Z;
				}
			}
		} else if (product.equals("simcom72_cwet_kk") && device.equals("simcom72_cwet_kk") && model.equals("simcom72_cwet_kk")) {
			type = display.startsWith("ALPS.KK1.MP7.SIM5000A")? CardType.F: CardType.E;
		} else if (product.equals("sugar_ref001") && device.equals("sugar-ref001") && model.equals("M2-XIXUN")) {
			type = CardType.L;
		} else if (product.equals("octopus_f1") && device.equals("octopus-f1")) {
			if (model.equals("M6") || model.equals("M7")) {
				type = CardType.N;
			}
		} else if ((product.equals("full_instk8735_6t") && device.equals("instk8735_6t") && model.equals("instk8735_6t"))
				|| (product.equals("full_instk8735_3t") && device.equals("instk8735_3t") && model.equals("instk8735_3t"))) {
			if ("MRA58K test-keys".equals(display) || "SHXX_540".equals(display)) {
				type = CardType.Q;
			} else if ("SHXX_540_SPI".equals(display) || "full_instk8735_6t-eng 6.0 MRA58K 1536144349 test-keys".equals(display)) {
				type = CardType.P;
			}
		} else if (product.equals("aosp_nanopi2") && device.equals("nanopi2") && model.equals("AOSP on NanoPi 2")) {
			String rilSo = "/system/lib/libhuawei-ril.so";
			type = rilSo.equals(SystemProperties.get("rild.libpath", rilSo))? CardType.X: CardType.W;
//			type = CardType.X;
		} else if (product.equals("full_instk8735_32t") && device.equals("instk8735_32t") && model.equals("instk8735_32t")) {
			type = CardType.M;
		} else if (product.equals("full_instk8735_32t_f") && device.equals("instk8735_32t") && model.equals("instk8735_32t")) {
			type = CardType.M_F;//19.6
		}else if (product.equalsIgnoreCase("full_instk8735_32t_ei") && device.equals("instk8735_32t") && model.equals("instk8735_32t")) {
			type = CardType.M_EI;
		}else if (product.equalsIgnoreCase("full_instk8735_32t_s") && device.equals("instk8735_32t") && model.equals("instk8735_32t")) {
			type = CardType.M_S;//7.1
		}else if (product.equals("rk3288") && device.equals("rk3288") && model.equals("rk3288")) {
			type = CardType.D;
		}else if (product.equals("rk3288") && device.equals("rk3288") && model.equals("rk3288_1")) {
			type = CardType.D_1;
		} else if (product.equals("rk3288") && device.equals("rk3288") && model.equals("ZC-328")) {
			type = CardType.D_ZC;
		}else if (product.equals("rk3399_all") && device.equals("rk3399_all") && model.equals("ZC-339")) {
			type = CardType.D_ZD;
		} else if (product.equals("mooncell_mid") && device.equals("mooncell_mid") && model.equals("mooncell_mid")) {
			type = CardType.R;
		}else if (product.equals("mooncell_box") && device.equals("mooncell_box") && model.equals("mooncell_box")) {
			type = CardType.R_1;
		}else if (product.equals("rk3326_evb") && device.equals("rk3326_evb") && model.equals("rk3326_evb")) {
			type = CardType.C;
		}
//		MyLog.i("type:"+type+";product:"+product+";device:"+device+";model:"+model+";display:"+display);
		if (type == CardType.UNKNOWN) {
			Log.d(TAG, "Product: " + product);
			Log.d(TAG, "Device: " + device);
			Log.d(TAG, "Model: " + model);
			Log.d(TAG, "Display: " + display);
		}

		return type;
	}
}
