package com.mt.lib.mtblesdk;

import android.bluetooth.BluetoothDevice;

public class MTBeacon {
	private static final String CLOCK_UUID = "5368656E-5A68-656E-545A-E28094E28094";
	private BluetoothDevice device;
	private int averagerssi = 0;
	private int rssi;
	private byte[] scanRecord;
	private int search_count = 0;

	private int major;
	private int minor;
	private int Txpower;
	private String UUID;
	private int battery = 0;
	private String ClockID = "";
	private boolean IsClock = false;

	private int Ibeancon_Offset = 0; // 偏移
	private int MT_inf_offset = 0; // 偏移

	public MTBeacon(BluetoothDevice device, int rssi, byte[] scanRecord) {
		this.device = device;
		this.rssi = rssi;
		this.scanRecord = scanRecord;
		this.averagerssi = rssi;

		GetOffset(this.scanRecord); // 分析提取数据
	}

	// 获取设备信息
	public BluetoothDevice GetDevice() {
		return device;
	}

	// 防抖
	public int CheckSearchcount() {
		search_count++;
		return search_count;
	}

	// 刷新信息
	public boolean ReflashInf(BluetoothDevice device, int rssi,
			byte[] scanRecord) {
		this.device = device;
		this.rssi = rssi;
		this.scanRecord = scanRecord;

		averagerssi = (averagerssi + rssi) / 2;

		search_count = 0; // 防抖

		GetOffset(this.scanRecord);

		return true;
	}

	// 获取major、minor、TXpower、UUID、电量、锁ID号
	public int GetMajor() {
		return major;
	}

	public int GetMinor() {
		return minor;
	}

	public int GetTxpower() {
		return Txpower;
	}

	public String GetUUID() {
		return UUID;
	}

	public int GetEnergy() {
		return scanRecord[MT_inf_offset + 3];
	}

	public String getClockID() {
		return device.getAddress();
//		return ClockID;
	}

	// 获取当前rssi值
	public int GetCurrentRssi() {
		return rssi;
	}

	// 获取防抖后rssi值
	public int GetAveragerssi() {
		return averagerssi;
	}

	// 获取当前距离
	public double GetCurrentDistance() {
		return CalculateDistance(rssi);
	}

	// 获取防抖距离
	public double GetAveragerDistance() {
		return CalculateDistance(averagerssi);
	}

	// 获取电池电量信息
	public int GetBattery() {
		return battery;
	}

	/************************* 辅助方法 *******************************/
	// rssi换算成距离
	private double CalculateDistance(int rssi) {
		double distance = 0;
		double ratio = rssi * 1.0 / Txpower;
		if (ratio < 1.0) {
			distance = Math.pow(ratio, 10);
		} else {
			distance = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
		}

		return distance;
	}

	// 分析数据
	private void GetOffset(byte[] scanRecord) {
		for (int i = 0; i < scanRecord.length;) {
			if ((scanRecord[i] == 26) && (scanRecord[i + 1] == -1)
					&& (scanRecord[i + 2] == 76) && (scanRecord[i + 3] == 0)
					&& (scanRecord[i + 4] == 2) && (scanRecord[i + 5] == 21)) {
				Ibeancon_Offset = i;

				major = ((0xFF & scanRecord[i + 22]) * 256 + (0xFF & scanRecord[i + 23]));
				minor = ((0xFF & scanRecord[i + 24]) * 256 + (0xFF & scanRecord[i + 25]));
				Txpower = scanRecord[i + 26];

				UUID = "";
				for (int j = i + 6; j < i + 22; j++) { // uuid
					String hex = Integer.toHexString(scanRecord[j] & 0xFF);
					if (hex.length() == 1) {
						hex = '0' + hex;
					}
					if ((j == (i + 10)) || (j == (i + 12)) || (j == (i + 14))
							|| (j == (i + 16)))
						UUID += '-';
					UUID += hex;
				}
				UUID = UUID.toUpperCase();
				if(UUID.equals(CLOCK_UUID)){
					IsClock = true;
				}

			}

			if ((scanRecord[i] == 3) && (scanRecord[i + 1] == -86)) {
				MT_inf_offset = i;
				battery = scanRecord[i + 3];
			}

			// 解析锁的ID
			if (scanRecord[i + 1] == (0x06 & 0xff)) {
				ClockID = "";
				for (int j = 0; j < scanRecord[i] - 2; j++) {
					char hex = (char) (scanRecord[i + j + 2] & 0xFF);
					ClockID += hex;
				}
			}

			i += (scanRecord[i] + 1);
			if ((i >= (scanRecord.length)) || (0x00 == scanRecord[i])) {
				break;
			}
		}

	}

	// 判断是否为锁
	public boolean isIsClock() {
		return IsClock;
	}

}
