using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;

namespace Rokid.MRC
{
    //相机参数
    public struct CameraParam : INetworkSerializable
    {
        //相机长宽
        public Vector2 pixelXY;
        public Vector2 pixel;

        public float fov;
        public float focalLength;
        public float nearClipPlane;
        public float farClipPlane;

        public int frameRate;
        public int bitRate;

        public CameraParam(Camera cam, int frameRate, int bitRate)
        {
            pixelXY = new Vector2(cam.pixelRect.x, cam.pixelRect.y);
            pixel = new Vector2(cam.pixelRect.width, cam.pixelRect.height);

            fov = cam.fieldOfView;
            focalLength = cam.focalLength;
            nearClipPlane = cam.nearClipPlane;
            farClipPlane = cam.farClipPlane;

            this.frameRate = frameRate;
            this.bitRate = bitRate;
        }

        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
            serializer.SerializeValue(ref pixelXY);
            serializer.SerializeValue(ref pixel);
            serializer.SerializeValue(ref fov);
            serializer.SerializeValue(ref focalLength);
            serializer.SerializeValue(ref nearClipPlane);
            serializer.SerializeValue(ref farClipPlane);
            serializer.SerializeValue(ref frameRate);
            serializer.SerializeValue(ref bitRate);
        }
    }

    public class SyncCameraTransform : NetworkBehaviour
    {
        private NetworkVariable<Vector3> syncPostion;
        private NetworkVariable<Quaternion> syncRotation;
        private NetworkVariable<Vector3> syncScale;

        private NetworkVariable<CameraParam> cameraInfo;

        public float deltaTime = 0.1f;
        private float endTime;

        public float interpolationTime = 0.1f;

        public GameObject clientCamera;

        void Awake()
        {
            DontDestroyOnLoad(gameObject);

            syncPostion = new NetworkVariable<Vector3>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
            syncRotation = new NetworkVariable<Quaternion>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
            syncScale = new NetworkVariable<Vector3>(Vector3.one, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
            cameraInfo = new NetworkVariable<CameraParam>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);

            //syncPostion.OnValueChanged += (oldVal, newVal) =>{
            //    Debug.Log($"LocalTime : {NetworkManager.Singleton.LocalTime.Time}  ServerTime : {NetworkManager.Singleton.ServerTime.Time}");
            //};
        }

        void Update()
        {
            ServerSync();
        }

        public override void OnNetworkSpawn()
        {
            base.OnNetworkSpawn();

            if(NetworkManager.Singleton.IsConnectedClient && IsOwner)
            {
                //手机端，设置参数
                //CameraParam param = new CameraParam(Camera.main);
                //cameraInfo.Value = param;
            }
            else if(IsServer)
            {
                if(clientCamera != null)
                {
                    //眼镜端，设置其下的摄像机参数，或者手动生成一个摄像机，同时设置相机参数
                    cameraInfo.OnValueChanged += (oldParam, newParam) =>
                    {
                        //Rect pRect = new Rect(newParam.pixelXY, newParam.pixelHeightWidth);
                        clientCamera.GetComponent<CameraController>().SetCameraParam(newParam);
                    };
                }
            }
        }

        public override void OnNetworkDespawn()
        {
            base.OnNetworkDespawn();
        }
        private void ServerSync()
        {
            if(NetworkManager.Singleton.IsConnectedClient)
            {
                //客户端
                if(CheckDeltaTime())
                {
                    //syncPostion.Value = ClientCoordinateManager.Instance.GetSyncCameraPos();
                    //syncRotation.Value = ClientCoordinateManager.Instance.GetSyncCameraRot();
                    //syncScale.Value = Vector3.one;
                }
            }
            else if(IsServer)
            {
                //服务端
                transform.position = Vector3.Lerp(transform.position, syncPostion.Value, interpolationTime);
                transform.localScale = Vector3.Lerp(transform.localScale, syncScale.Value, interpolationTime);
                transform.rotation = Quaternion.Slerp(transform.rotation, syncRotation.Value, interpolationTime);
            }
        }

        bool CheckDeltaTime()
        {
            if(Time.time >= endTime)
            {
                endTime = Time.time + deltaTime;
                return true;
            }
            return false;
        }

    }
}