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

namespace Rokid.MRC
{
    //资源管理器
    public class ResourceManager : UnitySingleton<ResourceManager>
    {
        public GameObject ClientAnchorPrefab;

        public GameObject CameraPrefab;

        public GameObject AnchorPrefab;

        public GameObject PhoneAnchorPrefab;

        private GameObject GlobalRenderCamera;

        public GameObject GlassCameraSyncPrefab;

        [HideInInspector]
        public GameObject CurRenderCamera;

        private void Start()
        {
            CreateGlobalRenderCam();

            //创建眼镜跟踪同步物体
            StartCoroutine(CreateGlassCameraAnchor());
        }

        private void CreateGlobalRenderCam()
        {
            if(MRCDriver.Instance.UseGlobalRenderCamera)
            {
                GlobalRenderCamera = transform.Find("RenderCamera").gameObject;
                if(GlobalRenderCamera == null)
                {
                    GlobalRenderCamera = Instantiate(CameraPrefab, transform);
                }
                GlobalRenderCamera?.SetActive(false);
            }
        }

        public void CreatePhoneAnchor()
        {
            if(MRCDriver.Instance.SelfCalibrationPhone != null)
            {
                return;
            }

            if(PhoneAnchorPrefab != null)
            {
                GameObject phoneModel = Instantiate(PhoneAnchorPrefab);
                phoneModel.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 0.5f + new Vector3(0, -0.05f, 0);

                MRCDriver.Instance.SelfCalibrationPhone = phoneModel.transform;
            }
        }

        public GameObject CreateClientAnchor(PlayerData playerData)
        {
            if(playerData.Anchor != null)
            {
                return playerData.Anchor;
            }

            if(ClientAnchorPrefab != null)
            {
                GameObject anchorParent = Instantiate(ClientAnchorPrefab);

                //生成坐标轴
                GameObject anchorAxis = CreateAnchorObject(anchorParent.transform);
                anchorAxis.SetActive(MRCDriver.Instance.ShowClientAnchor);
                //float pos = Random.Range(0.1f, 0.15f);
                //anchorAxis.transform.localPosition = new Vector3(pos, pos, pos);

                //生成相机
                if(playerData.IsVideoClient)
                {
                    if(!MRCDriver.Instance.UseGlobalRenderCamera)
                    {
                        CurRenderCamera = Instantiate(CameraPrefab, anchorParent.transform);
                    }
                    else
                    {
                        //全局只有一个相机
                        if(GlobalRenderCamera != null)
                        {
                            CurRenderCamera = GlobalRenderCamera;
                            CurRenderCamera.SetActive(true);

                            //跟随物体
                            CurRenderCamera.GetComponent<CameraController>().FollowParent = anchorParent.transform.Find("FollowTarget");
                        }
                        else
                        {
                            Debug.LogError("Please Configure UMRVirtualCamera");
                        }
                    }

                    CurRenderCamera.transform.position = Vector3.zero;

                    anchorParent.GetComponent<SyncCameraTransform>().clientCamera = CurRenderCamera;
                }

                //Spawn网络物体
                NetworkObject networkObject = anchorParent.GetComponent<NetworkObject>();
                if(networkObject)
                {
                    networkObject.SpawnWithOwnership(playerData.ClientId);
                }

                playerData.Anchor = anchorParent;

                return anchorParent;
            }

            return null;
        }

        private GameObject CreateAnchorObject(Transform parent)
        {
            //GameObject anchorObj = MRCDriver.Instance.ShowAnchorAdjustPanel? Instantiate(AnchorControllerPrefab, parent): Instantiate(AnchorPrefab, parent);
            GameObject anchorObj = Instantiate(AnchorPrefab, parent);
            anchorObj.transform.localPosition = Vector3.zero;
            anchorObj.transform.localRotation = Quaternion.identity;

            return anchorObj;
        }

        //创建相机同步锚点
        public IEnumerator CreateGlassCameraAnchor()
        {
            yield return new WaitForSeconds(2);
            GameObject anchorObj = Instantiate(GlassCameraSyncPrefab);
            SpawnNetworkObject(anchorObj);
        }

        private void SpawnNetworkObject(GameObject obj)
        {
            NetworkObject networkObject = obj.GetComponent<NetworkObject>();
            if(networkObject)
            {
                networkObject.Spawn();
            }
        }

        public void ShowRenderCamera(bool active)
        {
            CurRenderCamera?.SetActive(active);
        }

        //以下为测试代码

        public void CreatePrefab()
        {
            GameObject netPlayer = new GameObject("Player");
            NetworkObject networkObject = netPlayer.AddComponent<NetworkObject>();

            networkObject.Spawn();
            for(int i = 0;i < NetworkManager.Singleton.ConnectedClientsList.Count;i++)
            {
                ulong clientId = NetworkManager.Singleton.ConnectedClientsList[i].ClientId;
                if(clientId != NetworkManager.ServerClientId)
                {
                    networkObject.NetworkShow(clientId);
                }
            }
        }

        public GameObject CreateSyncObj(GameObject syncPrefab)
        {
            if(syncPrefab)
            {
                NetworkObject networkObject = Object.Instantiate(syncPrefab).GetComponent<NetworkObject>();
                if(networkObject)
                {
                    networkObject.Spawn();
                    Debug.Log("ServerController CreateSyncObj");
                }

                return networkObject.gameObject;
                //coordinateBehavior.InstantiateSyncObjectClientRpc();
            }

            return null;
        }


        //public static void MakeNetworkObjectAsPrefab(NetworkObject networkObject, uint globalObjectIdHash = default)
        //{
        //    if(globalObjectIdHash != default)
        //    {
        //        networkObject.GlobalObjectIdHash = globalObjectIdHash;
        //    }
        //    networkObject.IsSceneObject = false;
        //}
    }
}
