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

namespace PlantBox.Broker
{
    class ServerManager : TcpManager
    {
        public ServerManager(Broker broker) : base(broker)
        {

        }

        protected override string LogPrefix => "Server";
        protected override int ListeningPort => Connection.TCP_SERVER_PORT;

        protected override void InfoCommand(CommandStream commandStream, CommandPacket packet)
        {
            InfoResponse infoResponse = new InfoResponse().Deserialize(packet.Arguments);
            ulong id = packet.ID;
            PlantBox plantBox = Broker.PlantBoxesManager[id];

            if (plantBox == null)
            {
                plantBox = new PlantBox
                {
                    HistoricManager = new HistoricManager()
                };
            }
            plantBox.ID = id;
            plantBox.Name = infoResponse.Name;
            plantBox.Type = infoResponse.Type;
            plantBox.State = infoResponse.State;
            plantBox.Humidity = new CaptorValue(infoResponse.HumidityMin, infoResponse.HumidityMax, plantBox.Humidity?.Value ?? 0);
            plantBox.Luminosity = new CaptorValue(infoResponse.LuminosityMin, infoResponse.LuminosityMax, plantBox.Luminosity?.Value ?? 0);
            plantBox.Temperature = new CaptorValue(infoResponse.TemperatureMin, infoResponse.TemperatureMax, plantBox.Temperature?.Value ?? 0);

            plantBox.UpdateState();

            Broker.PlantBoxesManager.Add(plantBox);
        }

        protected override void CaptorsCommand(CommandStream commandStream, CommandPacket packet)
        {
            CaptorsResponse captorsResponse = new CaptorsResponse().Deserialize(packet.Arguments);
            ulong id = packet.ID;
            PlantBox plantBox = Broker.PlantBoxesManager[id];

            if (plantBox == null)
            {
                Log($"Received captors info from non-registered PlantBox ({id}), ignoring it");
                return;
            }

            plantBox.LastMeasureDate = DateTime.Now;
            plantBox.Humidity.Value = captorsResponse.Humidity;
            plantBox.Luminosity.Value = captorsResponse.Luminosity;
            plantBox.Temperature.Value = captorsResponse.Temperature;
            plantBox.TankLevel = captorsResponse.Tank;

            plantBox.UpdateState();

            plantBox.HistoricManager.Add(new CaptorsValue(plantBox.Humidity.Value, plantBox.Luminosity.Value, plantBox.Temperature.Value));
        }

        protected override void HistoricCommand(CommandStream commandStream, CommandPacket packet)
        {
            throw new NotImplementedException();
        }

        protected override void PingCommand(CommandStream commandStream, CommandPacket packet)
        {
            var ping = new PingCommand().Deserialize(packet.Arguments);

            commandStream.Send(ping.ToCommandPacket(packet.ID));
        }
    }
}
