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

//服务端空间坐标系控制
public class ServerSpaceCoordController : UnitySingleton<ServerSpaceCoordController>
{
    SpaceCoordinateBehavior coordinateBehavior;

    public bool UseCustomMsg;

    //客户端id——相机物体
    public Dictionary<ulong, GameObject> clientCameraAnchor;
    public GameObject curCameraAnchor;

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

        coordinateBehavior = GetComponent<SpaceCoordinateBehavior>();
        coordinateBehavior.OnNetworkSpawnHook += OnNetworkSpawn;
        coordinateBehavior.OnNetworkDespawnHook += OnNetworkDespawn;
    }

    private void OnNetworkSpawn()
    {
        if(UseCustomMsg)
        {
            ServerMessageDispatcher.Instance.Init();
        }

        if(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost)
        {
            ////只有服务器才能拥有该物体，目前已经加入登录验证，验证过的客户端不会生成PlayerPrefab
            //if(!coordinateBehavior.NetworkObject.IsOwnedByServer && !coordinateBehavior.NetworkObject.IsOwner)
            //{
            //    coordinateBehavior.NetworkObject.Despawn();
            //    Destroy(gameObject);
            //    return;
            //}
        }
        else
        {
            enabled = false;
        }
    }

    private void Init()
    {
        Debug.Log("ServerSpaceCoordController Init");
    }

    private void OnNetworkDespawn()
    {
        if(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost)
        {
            NetworkManager.Singleton.SceneManager.OnSceneEvent -= OnSceneEvent;
        }
    }

    //某个客户端切换了场景
    void OnSceneEvent(SceneEvent sceneEvent)
    {
        if(sceneEvent.SceneEventType != SceneEventType.LoadComplete)
            return;

        //发送相关数据给该客户端
    }

    //客户端校准完成，更新了自己的SpacePose，通知服务端
    //服务端修改所有NetworkList数据，同步给客户端，客户端收到数据后，打开录制界面
    public void ClientConfirmSpacePose(ulong clientId, SpacePose spacePose)
    {
        //更新客户端Pose列表
        coordinateBehavior.UpdateClientSpacePose(clientId, spacePose);

        SpacePose serverSpacePose = default(SpacePose);
        if(spacePose.CalibrationMode == ARCalibrationMode.FaceTrack)
        {
            //更新服务端Pose列表
            serverSpacePose = new SpacePose(clientId, Camera.main.transform.position, Camera.main.transform.rotation, spacePose.CalibrationMode);
        }
        else if(spacePose.CalibrationMode == ARCalibrationMode.SelfCalibration)
        {
            serverSpacePose = new SpacePose(clientId, MRCDriver.Instance.SelfCalibrationPhone.position, MRCDriver.Instance.SelfCalibrationPhone.rotation, spacePose.CalibrationMode);
        }
       
        coordinateBehavior.UpdateServerSpacePose(clientId, serverSpacePose);

        //通知构建提示界面关闭
        MessageCenter.SendMessage(GlobalDefine.SpaceBuildEnd, null);
    }

    public void ClientStartBuild(ulong clientID, ARCalibrationMode calibrationMode)
    {
        if(calibrationMode == ARCalibrationMode.FaceTrack)
        {
            //TODO，暂时先用Confirm界面来提示
            UIManager.Instance.ShowConfirm(
                LocalizationMgr.Instance.GetTextByKey("BuildTitle"),
                string.Format(LocalizationMgr.Instance.GetTextByKey("BuildReq"), PlayerManager.Instance.GetPlayerData(clientID)?.PlayerName),
                ConfirmType.None);
        }
        else if(calibrationMode == ARCalibrationMode.SelfCalibration)
        {
            //自构建，创建手机模型
            ResourceManager.Instance.CreatePhoneAnchor();
        }
    }

    public void ClientCancelBuild()
    {
        UIManager.Instance.ClosePanel(UIType.Confirm);
    }

    //某个客户端同步相机Pose
    private void OnClientUpdateCameraPose(ulong clientId, SpacePose spacePose)
    {
        //curCameraAnchor.transform
    }

}
