using PlantBox.Shared.Extensions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace PlantBox.Shared.Communication.Commands
{
    public class InfoResponse : CommandSerializable<InfoResponse>
    {
        public override Command Command => Command.Info;

        public string Name { get; set; }
        public PlantType Type { get; set; }
        public PlantState State { get; set; }
        public double HumidityMin { get; set; }
        public double HumidityMax { get; set; }
        public double LuminosityMin { get; set; }
        public double LuminosityMax { get; set; }
        public double TemperatureMin { get; set; }
        public double TemperatureMax { get; set; }

        public InfoResponse()
        {

        }

        public InfoResponse(string name, PlantType type, PlantState state, double humidityMin, double humidityMax, double luminosityMin, double luminosityMax, double temperatureMin, double temperatureMax)
        {
            Name = name;
            Type = type;
            State = state;
            HumidityMin = humidityMin;
            HumidityMax = humidityMax;
            LuminosityMin = luminosityMin;
            LuminosityMax = luminosityMax;
            TemperatureMin = temperatureMin;
            TemperatureMax = temperatureMax;
        }

        public override InfoResponse Deserialize(string[] arguments)
        {
            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }
            if (arguments.Length < 6)
            {
                throw new ArgumentException($"Excepted 6 arguments, got {arguments.Length}");
            }

            Name = arguments[0];
            Type = arguments[1].ToEnumValue<PlantType>();
            State = arguments[2].ToEnumValue<PlantState>();
            (HumidityMin, HumidityMax) = arguments[3].Split('/').Select(x => x.ToDouble()).ToArray().ToTuple();
            (LuminosityMin, LuminosityMax) = arguments[4].Split('/').Select(x => x.ToDouble()).ToArray().ToTuple();
            (TemperatureMin, TemperatureMax) = arguments[5].Split('/').Select(x => x.ToDouble()).ToArray().ToTuple();

            return this;
        }

        public override string[] Serialize()
        {
            return new[]
            {
                Name,
                Type.ToString(),
                State.ToString(),
                $"{HumidityMin}/{HumidityMax}",
                $"{LuminosityMin}/{LuminosityMax}",
                $"{TemperatureMin}/{TemperatureMax}"
            };
        }
    }
}
