using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Threading.Tasks;
using BattSim.Models;
using Microsoft.AspNetCore.Components.Forms;

namespace BattSim.Services
{
    public class EnergyCostCalculator
    {
        // Base cost
        public double BasePayment { get; set; } = 35;
        public double DataManagementCost { get; set; } = 18.77;
        public double FlemishEnergyTariff { get; set; } = 118.56;
        // Energy cost
        public double EnergyCost { get; set; } = 0.1364;
        public double ReturnCost { get; set; } = 0.0266;
        // Green energy cost
        public double GreenCertificateCost { get; set; } = 0.01172;
        public double HeatingCertificateCost { get; set; } = 0.00415;
        // Net and distributino ocsts
        public double CapacityCost { get; set; } = 58.20;
        public double UsageTariff { get; set; } = 0.0637;
        // Tax
        public double EnergyContribution { get; set; } = 0.00205;
        public double SpecialTariffs { get; set; } = 0.0502;

        public double CalculateCostOfEnergyUsage(EnergyData[] energyData, bool print = false) {
            int amountOfDays = energyData.Last().Time.Subtract(energyData.First().Time).Days;
            double yearfactor = amountOfDays / 365.0;
            double energyConsumption = energyData.Sum(e => e.Consumption);
            double energyProduction = energyData.Sum(e => e.Production);

            double baseCost = yearfactor * BasePayment;
            if(print) Console.WriteLine($"Base Cost: €{baseCost}");

            double energyCost = energyConsumption * EnergyCost;
            if (print) Console.WriteLine($"Energy Cost: €{energyCost}");

            double returnCost = energyProduction * ReturnCost;
            if (print) Console.WriteLine($"Return Cost: € -{returnCost}");

            double greenCertificateCost = energyConsumption * GreenCertificateCost;
            if (print) Console.WriteLine($"Green Certificate Cost: €{greenCertificateCost}");

            double heatingCertificateCost = energyConsumption * HeatingCertificateCost;
            if (print) Console.WriteLine($"Heating Certificate Cost: €{heatingCertificateCost}");

            double dataManagmentCost = yearfactor * DataManagementCost;
            if (print) Console.WriteLine($"Data Management Cost: €{dataManagmentCost}");

            double capacityCost = CalculateYearCapacity(energyData) * yearfactor;
            if (print) Console.WriteLine($"Capacity Cost: €{capacityCost}");

            double usageTariffCost = energyConsumption * UsageTariff;
            if (print) Console.WriteLine($"Usage Tariff Cost: €{usageTariffCost}");

            double energyContributionCost = energyConsumption * EnergyContribution;
            if (print) Console.WriteLine($"Energy Contribution Cost: €{energyContributionCost}");

            double specialTariffsCost = energyConsumption * SpecialTariffs;
            if (print) Console.WriteLine($"Special Tariffs Cost: €{specialTariffsCost}");

            double flemishEnergyTarif = yearfactor * FlemishEnergyTariff;
            if (print) Console.WriteLine($"Flemish Tariff: €{specialTariffsCost}");

            return baseCost + energyCost - returnCost + greenCertificateCost + heatingCertificateCost + dataManagmentCost + 
                capacityCost + usageTariffCost + energyContributionCost + specialTariffsCost + flemishEnergyTarif;
        }

        
        private double CalculateYearCapacity(EnergyData[] energyData)
        {
            double peakSum = 0;
            int months = 0;
            for(int i = 0; i<12; i++)
            {
                var monthData = energyData.Where(e => e.Time.Month == i);
                if (monthData.Count() == 0) continue;
                peakSum += monthData.Max(e => e.Consumption) * 4;
                months++;
            }
            return (peakSum / months) * CapacityCost;
        }
    }

}