using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
 * ׿ͨ
 * 
 * зֵbool͵Ľӿ
 * trur=ӿڵóɹ
 * false=豸û򿪻ӿ쳣
 * 
 */

public class Mostech_AndroidSerialPort : MonoBehaviour
{

	private AndroidJavaClass unityPlayer;
	private AndroidJavaObject contex;
	private AndroidJavaClass javaClass;
	private AndroidJavaObject gloveCore;

	private SerialPortCallbackHandler serialPortCallbackHandler;

	private void Awake()
	{
		CreatRes();
	}

	/// <summary>
	/// ׿ڿԴ
	/// ȡڿʵ
	/// </summary>
	private void CreatRes()
	{
		try
		{
			unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
			contex = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
			javaClass = new AndroidJavaClass("com.motion.usbserialport.GloveCore");

			//GloveCoreʵ
			gloveCore = javaClass.CallStatic<AndroidJavaObject>("getInstance");
			if (serialPortCallbackHandler == null)
			{
				serialPortCallbackHandler = new SerialPortCallbackHandler();
				gloveCore.Call("registerSerialPortCallback", serialPortCallbackHandler);
			}
			gloveCore.Call("init", contex);
			Debug.Log("ڿԴɹ");
		}
		catch (Exception e)
		{
			Debug.LogError("ڿԴ:" + e);
		}
	}

	/// <summary>
	/// ȡsdk汾
	/// </summary>
	/// <returns></returns>
	public string GetSdkVersion()
	{
		try
		{
			string version = gloveCore.Call<string>("getVersion");
			Debug.Log("sdk汾:" + version);
			return version;
		}
		catch (Exception e)
		{
			Debug.LogError("ȡsdk汾:" + e);
			return null;
		}
	}

	/// <summary>
	/// 豸
	/// </summary>
	public void ConnectDevice()
	{
		try
		{
			Debug.Log("豸");
			gloveCore.Call("resume");
		}
		catch (Exception e)
		{
			Debug.LogError("豸Ӵ:" + e);
		}
	}

	/// <summary>
	/// ȡ״̬
	/// </summary>
	/// <returns></returns>
	public bool GetSerialPortState()
	{
		if (serialPortCallbackHandler != null)
		{
			if (serialPortCallbackHandler.IsConnect && serialPortCallbackHandler.IsMostectDevice)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	/// <summary>
	/// 豸֤
	/// </summary>
	public bool VerifyDevice()
	{
		try
		{
			Debug.Log("豸֤");
			return gloveCore.Call<bool>("vertifyDevice");
		}
		catch (Exception e)
		{
			Debug.LogError("豸֤:" + e);
			return false;
		}
	}

	/// <summary>
	/// ȡ豸ID
	/// </summary>
	public bool GetDeviceID()
	{
		try
		{
			Debug.Log("ȡ豸ID");
			return gloveCore.Call<bool>("getDeviceId");
		}
		catch (Exception e)
		{
			Debug.LogError("ȡ豸ID:" + e);
			return false;
		}
	}

	/// <summary>
	/// ȡ״̬
	/// </summary>
	public bool GetConnectState()
	{
		try
		{
			Debug.Log("ȡ״̬");
			return gloveCore.Call<bool>("queryConnectState");
		}
		catch (Exception e)
		{
			Debug.LogError("ȡ״̬:" + e);
			return false;
		}
	}

	/// <summary>
	/// ȡ
	/// </summary>
	public bool GetBatteryRemain()
	{
		try
		{
			Debug.Log("ȡ");
			return gloveCore.Call<bool>("getBattery");
		}
		catch (Exception e)
		{
			Debug.LogError("ȡ:" + e);
			return false;
		}
	}

	/// <summary>
	/// ʼɼԪ
	/// </summary>
	public bool StartDataReceiving()
	{
		try
		{
			Debug.Log("ʼɼ");
			return gloveCore.Call<bool>("receiveData");
		}
		catch (Exception e)
		{
			Debug.LogError("ɼݴ:" + e);
			return false;
		}
	}

	/// <summary>
	/// ȡ̬
	/// </summary>
	/// <returns></returns>
	public double[] GetOutputData()
	{
		try
		{
			Debug.Log("ȡ̬");
			return gloveCore.Call<double[]>("getOutputData");
		}
		catch(Exception e)
		{
			Debug.LogError("ȡ̬ݴ:" + e);
			return null;
		}
	}



	/// <summary>
	/// У׼
	/// </summary>
	/// <param name="caliType">6</param>
	public bool PostureCalibration(int caliType)
	{
		try
		{
			if (GetSerialPortState())
			{
				Debug.Log("У׼");
				gloveCore.Call("calibrationHand", 6);
				return true;
			}
			else
			{
				return false;
			}
		}
		catch (Exception e)
		{
			Debug.LogError("У׼:" + e);
			return false;
		}
	}

	/// <summary>
	/// ͷԴ
	/// </summary>
	public void ReleaseRes()
	{
		try
		{
			if (gloveCore != null)
			{
				gloveCore.Call("destroy");
				gloveCore = null;
			}

			if (javaClass != null)
			{
				javaClass.Dispose();
				javaClass = null;
			}

			if (contex != null)
			{
				contex.Dispose();
				contex = null;
			}

			if (unityPlayer != null)
			{
				unityPlayer.Dispose();
				unityPlayer = null;
			}
			Debug.Log("ͷԴɹ");
		}
		catch (Exception e)
		{
			Debug.LogError("ͷԴʧ:" + e);
		}
	}


	private void OnApplicationPause(bool pause)
	{
		Debug.Log("OnApplicationPause:" + pause);
		if (pause)
		{
			ConnectDevice();
		}
	}

	private void OnApplicationQuit()
	{
		ReleaseRes();
	}


	private void OnDestroy()
	{
		ReleaseRes();
	}
}


#region ׿sdkлص(߳)
public class SerialPortCallbackHandler : AndroidJavaProxy
{

	private bool m_isConnect;
	private bool isMostectDevice;
	private bool[] connectState = new bool[2];
	private int[] batteryRemain = new int[2];
	private string deviceId;


	public bool IsConnect
	{
		get { return m_isConnect; }
		set { m_isConnect = value; }
	}

	public bool IsMostectDevice
	{
		get { return isMostectDevice; }
		set { isMostectDevice = value; }
	}

	public bool[] ConnectState
	{
		get { return connectState; }
		set { connectState = value; }
	}

	public int[] BatterRemain
	{
		get { return batteryRemain; }
		set { batteryRemain = value; }
	}

	public string DeviceId
	{
		get { return deviceId; }
		set { deviceId = value; }
	}



	public int count;

	public SerialPortCallbackHandler() : base("com.motion.usbserialport.callback.SerialPortCallBack") { }


	/// <summary>
	/// 豸ӻص
	/// </summary>
	/// <param name="isConnect"></param>
	public void isConnect(bool isConnect)
	{
		Debug.Log("豸ӽ:" + isConnect);
		m_isConnect = isConnect;
	}

	/// <summary>
	/// 豸֤ص
	/// </summary>
	/// <param name="isVertify"></param>
	public void isVertify(bool isVertify)
	{
		Debug.Log("豸֤:" + isVertify);
		isMostectDevice = isVertify;
	}

	/// <summary>
	/// 豸״̬ص
	/// </summary>
	/// <param name="leftHandConnectState"></param>
	/// <param name="rightHandConnectState"></param>
	public void deviceConnect(bool leftHandConnectState, bool rightHandConnectState)
	{
		Debug.Log("豸״̬:" + leftHandConnectState + "  " + rightHandConnectState);
		connectState[0] = leftHandConnectState;
		connectState[1] = rightHandConnectState;
	}

	/// <summary>
	/// 豸ص
	/// </summary>
	/// <param name="node1Battery"></param>
	/// <param name="node2Battery"></param>
	public void queryBattery(int node1Battery, int node2Battery)
	{
		Debug.Log("豸:" + node1Battery + "  " + node2Battery);
		batteryRemain[0] = node1Battery;
		batteryRemain[1] = node2Battery;
	}

	/// <summary>
	/// 豸idص
	/// </summary>
	/// <param name="id"></param>
	public void queryId(string id)
	{
		Debug.Log("豸id:" + id);
		deviceId = id;
	}

	/// <summary>
	/// ðʻص
	/// </summary>
	/// <param name="left"></param>
	/// <param name="right"></param>
	public void packageRate(int left, int right)
	{
		Debug.Log("ֵð:" + left + "  ֵð:" + right);
		Mostech_Loom.GetInstance().AddEventOnMainThread(() => Mostech_MotionDriver.Instance.UpDateDeviceInfo(left, right));
	}
}
#endregion



