using System;
using Unity.Collections;

namespace RebootKit.Engine.Network {
    public enum SendMode {
        Reliable,
        Unreliable
    }
    
    public interface INetworkManagerDelegate {
        void OnServerStarted();
        void OnServerStopped();
        void OnClientStarted();
        void OnClientStopped();
        void OnClientConnected(ulong clientID);
        void OnClientDisconnected(ulong clientID);
        void OnMessageReceived(ulong senderID, NativeArray<byte> data);
    }

    public class NetworkManagerStats {
        public ulong ReliableBytesSent { get; set; }
        public ulong UnreliableBytesSent { get; set; }
        public ulong BytesReceived { get; set; }

        public double ReliableBytesSentPerSecond { get; set; }
        public double UnreliableBytesSentPerSecond { get; set; }
        public double BytesReceivedPerSecond { get; set; }

        public int Ping { get; set; }

        public void Reset() {
            ReliableBytesSent = 0;
            UnreliableBytesSent = 0;
            BytesReceived = 0;
            ReliableBytesSentPerSecond = 0.0;
            UnreliableBytesSentPerSecond = 0.0;
            BytesReceivedPerSecond = 0.0;
            Ping = 0;
        }
    }

    public interface INetworkManager : IDisposable {
        INetworkManagerDelegate Delegate { get; set; }
        
        ulong LocalClientID { get; }

        NetworkManagerStats Stats { get; }

        bool IsServer();
        bool StartHost();
        void StopHost();
        
        bool IsClient();
        bool StartClient();
        void Disconnect();

        void Send(ulong clientID, NativeSlice<byte> data, SendMode mode);
        unsafe void Send(ulong clientID, byte* data, int length, SendMode mode);
        
        void Tick();
    }
}