using System.Globalization;

namespace Electricity.Migrator.Entities
{
    public partial class ElectricityLog
    {
        public string Date { get; set; } = null!;
        public string TimeUtc { get; set; } = null!;
        public double CurrentPowerUsage { get; set; }
        public double TotalPowerConsumptionDay { get; set; }
        public double TotalPowerConsumptionNight { get; set; }
        public double CurrentPowerReturn { get; set; }
        public double TotalPowerReturnDay { get; set; }
        public double TotalPowerReturnNight { get; set; }
        public long DayTarifEnabled { get; set; }
        public double GasConsumptionInCubicMeters { get; set; }

        public DateTime GetDateTime() => DateTime
            .Parse(
                $"{Date}T{TimeUtc}Z",
                null,
                System.Globalization.DateTimeStyles.AssumeUniversal)
            .ToUniversalTime();

        public static ElectricityLog CreateFromLine(string line)
        {
            // Example
            // 0                   1    2     3     4 5      6      7         8             9
            // 2018-03-30T10:25:08,0.08,69.47,52.62,0,20.519,56.158,day-tarif,180330102013S,188.593
            var parts = line.Split(',');
            var dateTimeParts = parts[0].Split('T');
            return new ElectricityLog
            {
                Date = dateTimeParts[0],
                TimeUtc = dateTimeParts[1],
                CurrentPowerUsage = double.Parse(parts[1], CultureInfo.InvariantCulture),
                TotalPowerConsumptionDay = double.Parse(parts[2], CultureInfo.InvariantCulture),
                TotalPowerConsumptionNight = double.Parse(parts[3], CultureInfo.InvariantCulture),
                CurrentPowerReturn = double.Parse(parts[4], CultureInfo.InvariantCulture),
                TotalPowerReturnDay = double.Parse(parts[5], CultureInfo.InvariantCulture),
                TotalPowerReturnNight = double.Parse(parts[6], CultureInfo.InvariantCulture),
                DayTarifEnabled = parts[7] == "day-tarif" ? 1 : 0,
                GasConsumptionInCubicMeters = double.Parse(parts[9], CultureInfo.InvariantCulture),
            };
        }
    }
}
