using IFramework.Net;
using IFramework;
using IFramework.Net.Udp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace IFramework.Custom
{

    public class UDPManger : MonoBehaviour
    {
        //#if !TEST

        private int serverPort = 16898;
        private int clientPort = 16897;
        private int buffClientPort = 16896;

        //#elif TEST
        //private int serverPort = 12345;
        //private int clientPort = 12346;
        //#endif

        public static UDPManger Instance;
        //private UDPClientProvider cameraClient;

        private bool initChaos = false;
        private bool alreadySendIp = false;
        private string buffClientIp = "";

        private void Awake()
        {
            Instance = this;
            Debug.Log("Version:" + Application.version);
        }

        void Start()
        {
            //cameraClient = new UDPClientProvider();

            //StartCoroutine(SendSDKData());
        }

        private void ClientBuffCallback(SegmentToken session)
        {
            Debug.Log("(客户端Buff)收到消息：" + Encoding.UTF8.GetString(session.Data.buffer, session.Data.offset,
                        session.Data.size));
        }

        IEnumerator SendSDKData()
        {
            while(true)
            {
                yield return new WaitForEndOfFrame();
                SendCameraData();
            }

        }

        /// 获取时间戳
        public long GetTimeStampSecond()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            try
            {
                return Convert.ToInt64(ts.TotalSeconds);
            }
            catch(Exception ex)
            {
                Debug.Log($"GetTimeStampSecond Error = {ex}");
                return 0;
            }
        }

        private bool SendIp()
        {
            if(!initChaos)
                return false;

            //TextureHelper.initTcp(buffClientIp, buffClientPort);

            return true;
        }

        public void SendCamerBuff(byte[] data)
        {
            Debug.LogError("CameraBuff:" + data.ToString());
            ;
            if(!initChaos)
                return;
            if(data != null)
            {
                //cameraBuffClient.ClientSend(data.ToString());
            }
        }

        public void SendCameraData()
        {
            if(initChaos)
            {
                //DataFromSDK data = GetHDMPoseData();
                //data.timestamp = GetTimeStampSecond();
                //data.virtualkey = -1;
                //data.hand_points = null;
                //string json = JsonUtil.Serialize(data);
                //cameraClient.ClientSend(json);
            }
        }

        public void SendVirtualkey(int virtualkey)
        {
            Debug.LogError("virtualkey:" + virtualkey);
            if(initChaos)
            {
                //DataFromSDK data = GetHDMPoseData();
                //data.timestamp = GetTimeStampSecond();
                //data.virtualkey = virtualkey;
                //data.hand_points = null;
                //string json = JsonUtil.Serialize(data);
                //cameraClient.ClientSend(json);
            }
        }

        public void SendHandPose(string gesture)
        {
            if(initChaos)
            {
                //DataFromSDK data = GetHDMPoseData();
                //data.timestamp = GetTimeStampSecond();
                //data.virtualkey = -1;
                //data.hand_points = gesture;
            }
        }

        //private OriginDataFromSDK GetOriginData()
        //{
        //    OriginDataFromSDK data = new OriginDataFromSDK();
        //    data.headPose = ARFrame.HeadPose;
        //    data.fovs = ARFrame.CameraParams.fov;
        //    data.left = ARFrame.CameraParams.leftProjection;
        //    data.right = ARFrame.CameraParams.rightProjection;
        //    return data;
        //}

        //public DataFromSDK GetHDMPoseData()
        //{
        //    DataFromSDK data = new DataFromSDK();
        //    Vector4 pose_v1 = HMDPoseTracker.Instance.centerCamera.cameraToWorldMatrix.GetColumn(0);
        //    Vector4 pose_v2 = HMDPoseTracker.Instance.centerCamera.cameraToWorldMatrix.GetColumn(1);
        //    Vector4 pose_v3 = HMDPoseTracker.Instance.centerCamera.cameraToWorldMatrix.GetColumn(2);
        //    Vector4 pose_v4 = HMDPoseTracker.Instance.centerCamera.cameraToWorldMatrix.GetColumn(3);
        //    data.camera_pose = new float[,] {
        //    {pose_v1.x,pose_v1.y,pose_v1.z,pose_v1.w },
        //    {pose_v2.x,pose_v2.y,pose_v2.z,pose_v2.w},
        //    {pose_v3.x,pose_v3.y,pose_v3.z,pose_v3.w },
        //    {pose_v4.x,pose_v4.y,pose_v4.z,pose_v4.w }
        //};
        //    Vector4 left_v1 = HMDPoseTracker.Instance.leftCamera.cameraToWorldMatrix.GetColumn(0);
        //    Vector4 left_v2 = HMDPoseTracker.Instance.leftCamera.cameraToWorldMatrix.GetColumn(1);
        //    Vector4 left_v3 = HMDPoseTracker.Instance.leftCamera.cameraToWorldMatrix.GetColumn(2);
        //    Vector4 left_v4 = HMDPoseTracker.Instance.leftCamera.cameraToWorldMatrix.GetColumn(3);
        //    data.left_eye_transform = new float[,]{
        //     { left_v1.x,left_v1.y,left_v1.z,left_v1.w},
        //     { left_v2.x,left_v2.y,left_v2.z,left_v2.w},
        //     { left_v3.x,left_v3.y,left_v3.z,left_v3.w},
        //     { left_v4.x,left_v4.y,left_v4.z,left_v4.w}
        //     };
        //    Vector4 right_v1 = HMDPoseTracker.Instance.rightCamera.cameraToWorldMatrix.GetColumn(0);
        //    Vector4 right_v2 = HMDPoseTracker.Instance.rightCamera.cameraToWorldMatrix.GetColumn(1);
        //    Vector4 right_v3 = HMDPoseTracker.Instance.rightCamera.cameraToWorldMatrix.GetColumn(2);
        //    Vector4 right_v4 = HMDPoseTracker.Instance.rightCamera.cameraToWorldMatrix.GetColumn(3);
        //    data.right_eye_transform = new float[,] {
        //{right_v1.x,right_v1.y,right_v1.z,right_v1.w},
        //{right_v2.x,right_v2.y,right_v2.z,right_v2.w},
        //{right_v3.x,right_v3.y,right_v3.z,right_v3.w },
        //{right_v4.x,right_v4.y,right_v4.z,right_v4.w }
        //};
        //    data.origin_positon = new float[] {
        //    ARFrame.HeadPose.position.x,
        //    ARFrame.HeadPose.position.y,
        //    ARFrame.HeadPose.position.z
        //};
        //    data.origin_rotation = new float[] {
        //    ARFrame.HeadPose.rotation.x,
        //    ARFrame.HeadPose.rotation.y,
        //    ARFrame.HeadPose.rotation.z,
        //    ARFrame.HeadPose.rotation.w
        //};
        //    return data;
        //}

        private void Update()
        {
            if(initChaos)
            {
                if(!alreadySendIp)
                {
                    alreadySendIp = SendIp();
                }

            }
        }

        private void OnApplicationQuit()
        {
            //cameraClient.Dispose();
            //UDPServer.Instance.Dispose();
            //cameraBuffClient.Dispose();
        }

    }
}