#nullable enable
using System;
using System.Collections.Generic;

namespace IGP.Multiplayer.Network
{
    /// <summary>
    /// 玩家的唯一标识
    /// </summary>
    [Serializable]
    internal struct IGPPlayerID : IEquatable<IGPPlayerID>
    {
        public string id;

        public IGPPlayerID(string id)
        {
            this.id = id;
        }

        public static implicit operator string(IGPPlayerID playerID)
        {
            return playerID.id;
        }

        public static implicit operator IGPPlayerID(string id)
        {
            return new IGPPlayerID(id);
        }

        public override string ToString()
        {
            return id;
        }

        public bool Equals(IGPPlayerID other)
        {
            return id == other.id;
        }

        public override bool Equals(object? obj)
        {
            return obj is IGPPlayerID other && Equals(other);
        }

        public override int GetHashCode()
        {
            return id != null ? id.GetHashCode() : 0;
        }

        public static bool operator ==(IGPPlayerID left, IGPPlayerID right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(IGPPlayerID left, IGPPlayerID right)
        {
            return !left.Equals(right);
        }
    }

    /// <summary>
    /// 网络操作结果
    /// </summary>
    internal enum IGPNetworkResult
    {
        kSuccess = 0,
        kErrorUnknown = 1,
        kErrorInvalidParam = 2,
        kErrorInvalidState = 3,
        kErrorServiceNotAvailable = 4,
        kErrorNetworkUnReachable = 5,
        kErrorNetworkRemotePeerOffline = 6,
        kErrorNetworkServerUnavailable = 7,
        kErrorNetworkConnectionDenied = 8,
        kErrorNetworkConnectionClosed = 9,
        kErrorNetworkConnectionReset = 10,
        kErrorNetworkError = 11,
        kErrorWouldBlock = 12
    }

    /// <summary>
    /// 会话状态
    /// </summary>
    internal struct IGPNetworkSessionState
    {
        public IGPPlayerID remote_peer;
        public bool is_connected;
        public bool is_writable;
        public uint packets_in_flight;
        public uint bytes_in_flight;
    }
    
    [Serializable]
    internal sealed class IGPDataReceived
    {
        public IGPPlayerID remote_peer;
        public byte[] data = Array.Empty<byte>();
        public uint message_type;
        public IGPRealtimeTransport Transport { get; set; }
    }

    [Serializable]
    internal sealed class IGPPeerActivity
    {
        public IGPPlayerID remote_peer;
        public uint message_type;
        public string transport_channel = string.Empty;
        public bool is_reliable_chunk;
        public int reliable_chunk_index = -1;
        public int reliable_chunk_count = -1;
    }

    internal static class IGPP2PTransportChannels
    {
        public const string Control = "control";
        public const string Data = "data";

        public static string Resolve(string? channel, bool isReliableChunk)
        {
            if (string.Equals(channel, Control, StringComparison.Ordinal))
            {
                return Control;
            }

            return Data;
        }
    }

    [Serializable]
    internal sealed class P2PMessagePayload
    {
        public string senderId = string.Empty;
        public string targetId = string.Empty;
        public string data = string.Empty; // Base64
        // Owned by this message; never backed by a transport's reusable buffer.
        [NonSerialized] [Newtonsoft.Json.JsonIgnore] internal byte[]? binaryData;
        internal byte[] ReadBytes() => binaryData ?? Convert.FromBase64String(data);
        public uint messageType;
        public string transportChannel = string.Empty;
        public bool reliable;
    }

}
