using Microsoft.AspNetCore.Components;
using Portfolio.Domain.Features.TemperatureDay;
using Portfolio.Domain.Features.TemperatureRange;
using Portfolio.WebUI.Server.Components.Component.Crochet_Components;
using static Portfolio.WebUI.Server.Components.Component.Crochet_Components.TemperatureRangeEditor;

namespace Portfolio.WebUI.Server.Components.Pages.Crochet_Pages
{
    public partial class CrochetHome
    {
        public List<TemperatureDay> temperatureDays { get; set; }
        public int YEAR = 2025;

        protected override async Task OnInitializedAsync()
        {
            // Placeholder for loading temperature data
            // Replace with actual API call 
            //temperatureDays = Enumerable.Range(0, 365).Select(i => new TemperatureDay
            //{
            //    Date = new DateTime(DateTime.Now.Year - 1, 1, 1).AddDays(i),
            //    AvgTemp = Random.Shared.Next(10, 95)
            //}).ToList();

            temperatureDays = GenerateRealisticTemperatureDays(YEAR);



        }

        public static List<TemperatureDay> GenerateRealisticTemperatureDays(int year)
        {
            var temperatureDays = new List<TemperatureDay>();
            var startDate = new DateTime(year, 1, 1);
            int daysInYear = DateTime.IsLeapYear(year) ? 366 : 365;

            // Adjusted parameters for desired range
            double minAvgTemp = 20.0;
            double maxAvgTemp = 84.0;
            double amplitude = (maxAvgTemp - minAvgTemp) / 2.0;  // 32.5
            double avgAnnualTemp = (maxAvgTemp + minAvgTemp) / 2.0;  // 52.5
            double noiseMax = 3.0; // Small daily variation

            for (int i = 0; i < daysInYear; i++)
            {
                var date = startDate.AddDays(i);
                double dayOfYearRatio = (2 * Math.PI * i) / daysInYear;

                // Peak is mid-summer, trough is mid-winter
                double seasonalTemp = avgAnnualTemp + amplitude * Math.Sin(dayOfYearRatio - Math.PI / 2);

                // Add gentle noise
                double dailyNoise = Random.Shared.NextDouble() * noiseMax * 2 - noiseMax;

                // Final temperature, clamped to min/max
                double finalTemp = Math.Round(seasonalTemp + dailyNoise, 1);
                finalTemp = Math.Clamp(finalTemp, minAvgTemp, maxAvgTemp);

                temperatureDays.Add(new TemperatureDay
                {
                    Date = date,
                    AvgTemp = finalTemp
                });
            }

            return temperatureDays;
        }

    }
}
