using System;
using System.Collections.Generic;
using Coherence;
using Coherence.Connection;
using Coherence.Toolkit;
using UnityEngine;

namespace Dissonance.Integrations.Coherence
{
    [RequireComponent(typeof(CoherenceSync))]
    [AddComponentMenu("coherence/Voice/Dissonance/Coherence Sync Voice")]
    public class CoherenceSyncVoice : MonoBehaviour
    {
        [SerializeField] private float _staleAudioTimeSeconds = 3;

        private CoherenceSync _sync;
        private readonly PacketSender<CoherenceSyncVoice> _packetSender = new();
        private readonly Dictionary<ClientID, Queue<byte[]>> _receivedData = new();

        [Sync]
        public uint ClientID { get; set; }

        private void Awake()
        {
            var hasSync = TryGetComponent(out _sync);
            Debug.Assert(hasSync, $"Required {nameof(CoherenceSync)} component not found on {nameof(CoherenceSyncVoice)}.", this);
        }

        private void Start()
        {
            if (_sync.HasStateAuthority)
            {
                ClientID = (uint)_sync.CoherenceBridge.ClientID;
            }
        }

        /// <remarks>
        /// Called by <see cref="CoherenceServer"/>.
        /// Sends the voice data through <see cref="CoherenceSync.SendOrderedCommand{TTarget}(string, MessageTarget, object[])"/>.
        /// Data is received by the <see cref="CoherenceSyncVoice"/> that holds state authority.
        /// </remarks>
        public void SendReliableClientDissonanceData(ArraySegment<byte> data)
        {
            _packetSender.SendDissonanceData(_sync, ReceiveReliableDissonanceData, data, true);
        }

        /// <remarks>
        /// Called by <see cref="CoherenceServer"/>.
        /// Sends the voice data to the state authority through <see cref="CoherenceSync.SendCommand{TTarget}(string, MessageTarget, object[])"/>.
        /// Data is received in <see cref="ReceiveReliableDissonanceData"/>.
        /// </remarks>
        public void SendUnreliableClientDissonanceData(ArraySegment<byte> data)
        {
            _packetSender.SendDissonanceData(_sync, ReceiveUnreliableDissonanceData, data, false);
        }

        /// <remarks>
        /// Called by <see cref="SendReliableClientDissonanceData"/>.
        /// </remarks>
        [Command]
        public void ReceiveReliableDissonanceData(byte[] data, ClientID senderId, long sendFrame)
        {
            if (!_receivedData.TryGetValue(senderId, out var queue))
            {
                queue = new Queue<byte[]>();
                _receivedData.Add(senderId, queue);
            }

            queue.Enqueue(data);
        }

        /// <remarks>
        /// Called by <see cref="SendUnreliableClientDissonanceData"/>.
        /// </remarks>
        [Command]
        public void ReceiveUnreliableDissonanceData(byte[] data, ClientID senderId, long sendFrame)
        {
            if (VoiceClock.IsStale(_sync.CoherenceBridge, sendFrame, _staleAudioTimeSeconds))
            {
                return;
            }

            if (!_receivedData.TryGetValue(senderId, out var queue))
            {
                queue = new Queue<byte[]>();
                _receivedData.Add(senderId, queue);
            }

            queue.Enqueue(data);
        }

        /// <remarks>
        /// Called by <see cref="CoherenceClient"/>, to pull any received voice data.
        /// </remarks>
        public void TryGetClientData(Func<ArraySegment<byte>, ushort?> onReceivedData)
        {
            foreach (var receivedData in _receivedData)
            {
                while (receivedData.Value.TryDequeue(out var data))
                {
                    _ = onReceivedData(data);
                }
            }
        }
    }
}
