using System;
using UnityEngine;

namespace FastArena
{
    public class GameObjectId : MonoBehaviour
    {
        public int Id { get; private set; } = -1;

        internal void _SetId(int packetGameObjectId)
        {
            if (Id != -1)
                throw new Exception($"GameObjectId is already set to {Id}");
            Id = packetGameObjectId;
        }
    }
    
    /// <summary>
    /// Component fetching GameObject's Transform from the server every tick.
    /// </summary>
    public class SyncTransform : MonoBehaviour
    {
        public GameObjectId IdRef { get; private set; }
        
        private void Start()
        {
            IdRef = GetComponent<GameObjectId>();
        }
        
#if UNITY_SERVER
        private void FixedUpdate()
        {
            Server.SendTransformUpdate(IdRef.Id, transform);
        }
#else
        internal void UpdateTransform(ref TransformUpdatePacket data)
        {
            transform.position = data.position;
            transform.rotation = data.rotation;
        }
#endif
    }
}
