using PlantBox.Client.Extensions;
using PlantBox.Shared.Communication.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlantBox.Client.Models
{
    public class HistoricEntries
    {
        public DateTime LastTime { get; }
        public  TimeSpan TimeInterval { get; }

        public HistoricEntry[] Humidities { get; }
        public HistoricEntry[] Luminosities { get; }
        public HistoricEntry[] Temperatures { get; }

        public DateTime EndTime => LastTime - new TimeSpan(TimeInterval.Ticks * Count);
        public int Count => Humidities.Length;

        public HistoricEntries(DateTime lastTime, TimeSpan timeInterval, double[] humidities, double[] luminosities, double[] temperatures)
        {
            LastTime = lastTime;
            TimeInterval = timeInterval;
            Humidities = ValuesToEntries(humidities);
            Luminosities = ValuesToEntries(luminosities);
            Temperatures = ValuesToEntries(temperatures);
        }
        public HistoricEntries(HistoricResponse response, TimeSpan time) : this(DateTime.Now - response.Time, time, response.Humidities, response.Luminosities, response.Temperatures)
        {

        }

        private HistoricEntry[] ValuesToEntries(double[] values)
        {
            HistoricEntry[] entries = new HistoricEntry[values.Length];
            var oldestDate = LastTime - TimeInterval.Multiply(values.Length - 1);

            for (int i = 0; i < values.Length; i++)
            {
                entries[i] = new HistoricEntry(oldestDate + TimeInterval.Multiply(i), values[i]);
            }

            return entries;
        }
    }
}
