using PlantBox.Shared.Communication.Commands;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace PlantBox.Shared.Communication
{
    /// <summary>
    /// Wrap a <see cref="NetworkStream"/> to send <see cref="CommandPacket"/> easily, see Wiki > Protocol
    /// </summary>
    public class CommandStream
    {
        private NetworkStream _stream;

        /// <summary>
        /// Create a new <see cref="CommandStream"/>
        /// </summary>
        /// <param name="networkStream">A connected and valid <see cref="NetworkStream"/></param>
        public CommandStream(NetworkStream networkStream)
        {
            _stream = networkStream;
        }

        /// <summary>
        /// Read a <see cref="CommandPacket"/> from the stream
        /// </summary>
        /// <returns></returns>
        public CommandPacket Receive()
        {
            // Length
            byte[] buffer = new byte[4];
            _stream.Read(buffer, 0, buffer.Length);

            int length = BitConverter.ToInt32(buffer, 0);

            // Data
            buffer = new byte[length];
            _stream.Read(buffer, 0, buffer.Length);

            string[] packet = Encoding.UTF8.GetString(buffer).Split('\n');

            if (!Enum.TryParse(packet[0], true, out Command command))
            {
                command = Command.Invalid;
            }

            return new CommandPacket(command, packet[1].Split(';'));
        }

        /// <summary>
        /// Read a <see cref="CommandPacket"/> from the stream and deserialize data to a <see cref="CommandSerializable{T}"/>
        /// </summary>
        /// <typeparam name="T">A valid <see cref="CommandSerializable{T}"/></typeparam>
        /// <returns></returns>
        public (CommandPacket, T) Receive<T>() where T : CommandSerializable<T>, new()
        {
            var packet = Receive();

            return (packet, new T().Deserialize(packet.Arguments));
        }

        /// <summary>
        /// Read a <see cref="CommandPacket"/> from the stream asynchronously
        /// </summary>
        /// <returns></returns>
        public async Task<CommandPacket> ReceiveAsync()
        {
            // Length
            byte[] buffer = new byte[4];
            await _stream.ReadAsync(buffer, 0, buffer.Length);

            int length = BitConverter.ToInt32(buffer, 0);

            // Data
            buffer = new byte[length];
            await _stream.ReadAsync(buffer, 0, buffer.Length);

            string[] packet = Encoding.UTF8.GetString(buffer).Split('\n');

            if (!Enum.TryParse(packet[0], true, out Command command))
            {
                command = Command.Invalid;
            }

            return new CommandPacket(command, packet[1].Split(';'));
        }

        /// <summary>
        /// Read a <see cref="CommandPacket"/> from the stream and deserialize data to a <see cref="CommandSerializable{T}"/> asynchronously
        /// </summary>
        /// <typeparam name="T">A valid <see cref="CommandSerializable{T}"/></typeparam>
        /// <returns></returns>
        public async Task<(CommandPacket, T)> ReceiveAsync<T>() where T : CommandSerializable<T>, new()
        {
            var packet = await ReceiveAsync();

            return (packet, new T().Deserialize(packet.Arguments));
        }

        /// <summary>
        /// Write a <see cref="CommandPacket"/> in the stream
        /// </summary>
        /// <param name="command"></param>
        public void Send(CommandPacket command)
        {
            string packet = command.ToString();
            byte[] data = Encoding.UTF8.GetBytes(packet);

            _stream.Write(BitConverter.GetBytes(data.Length), 0, 4);
            _stream.Write(data, 0, data.Length);
        }

        /// <summary>
        /// Write a <see cref="CommandPacket"/> in the stream asynchronously
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task SendAsync(CommandPacket command)
        {
            string packet = command.ToString();
            byte[] data = Encoding.UTF8.GetBytes(packet);

            await _stream.WriteAsync(BitConverter.GetBytes(data.Length), 0, 4);
            await _stream.WriteAsync(data, 0, data.Length);
        }
    }
}
