public async Task <List <Forecast> > OpenWeatherWeatherorecast(LoactionCoord location)
        {
            // var url = $"http://api.openweathermap.org/data/2.5/weather?lat={location.Latitude}&lon={location.Longitude}&appid={apiKey}&units=metric ";

            var apiKey               = "cfdc9335f6a03abf829ab28b3249154b";
            var forecastWeatherurl   = $"http://api.openweathermap.org/data/2.5/forecast?lat={location.Latitude}&lon={location.Longitude}&appid={apiKey}&units=metric ";
            var forecastOWjsonString = await _http.Get(forecastWeatherurl);

            var forecastOWrRO = JsonConvert.DeserializeObject <OpenWeatherForecast.Rootobject>(forecastOWjsonString);

            List <Forecast> weatherForecast = new List <Forecast>();


            for (int n = 0; n < 3; n++)
            {
                var temp1 = decimal.Parse(forecastOWrRO.list[n + 1].main.temp.ToString());
                var tempA = float.Parse(Decimal.Round(temp1, 1).ToString());

                weatherForecast.Add(new Models.Forecast
                {
                    Temperatur = tempA,
                    ImgIcon    = $"/img/weathericons/{forecastOWrRO.list[n+1].weather[0].icon}.png",
                    Time       = forecastOWrRO.list[n + 1].dt_txt.Split(" ")[1].Remove(2),
                });
            }
            return(weatherForecast);
        }
        public async Task <IActionResult> GetWeather(decimal lat, decimal lon)
        {
            string lons = Decimal.Round(lon, 3).ToString(new CultureInfo("en"));
            string lats = Decimal.Round(lat, 3).ToString(new CultureInfo("en"));

            var locname = await _locationService.LocationName(lats, lons);

            var weatherList = _context.Weathers.ToList();
            var position    = new LoactionCoord
            {
                CityName  = locname,
                Latitude  = lats,
                Longitude = lons
            };

            if (weatherList.Any(x => x.Updated <= DateTime.UtcNow.AddHours(2).AddMinutes(10)) || weatherList.Count == 0)
            {
                _context.Forecasts.RemoveRange(_context.Forecasts);
                _context.Weathers.RemoveRange(_context.Weathers);



                var openWeatherWeather = await _weatherService.OpenWeatherWeather(position);

                var yrWeather = await _weatherService.YRWeather(position);

                var smhiWeather = await _weatherService.SMHIWeather(position);

                weatherList = new List <Weather> {
                    openWeatherWeather, yrWeather, smhiWeather
                };

                foreach (var weather in weatherList)
                {
                    _context.Weathers.Add(weather);
                }
                _context.SaveChanges();
            }
            else
            {
                weatherList = _context.Weathers.ToList();
            }
            var sortedWeather = SortWeathers(weatherList);

            foreach (var item in sortedWeather)
            {
                item.Updated = DateTime.UtcNow.AddHours(2);
            }

            allWeathers = new WeatherVM
            {
                Weathers = sortedWeather,
                City     = position.CityName,
                Date     = DateTime.UtcNow.AddHours(2).ToString("dddd, dd MMMM HH:mm")
            };

            return(View("Like", allWeathers));
        }
        public async Task <Models.Weather> OpenWeatherWeather(LoactionCoord location)
        {
            //var url = $"http://api.openweathermap.org/data/2.5/forecast?lat={location.Latitude}&lon={location.Longitude}&appid={apiKey}";

            var apiKey              = "cfdc9335f6a03abf829ab28b3249154b";
            var currentWeatherurl   = $"http://api.openweathermap.org/data/2.5/weather?lat={location.Latitude}&lon={location.Longitude}&appid={apiKey}&units=metric ";
            var currentOWjsonString = await _http.Get(currentWeatherurl);

            var currentOWrRO = JsonConvert.DeserializeObject <OpenWeatherCurrent.Rootobject>(currentOWjsonString);

            var   temp1 = decimal.Parse(currentOWrRO.main.temp.ToString());
            var   temp  = float.Parse(Decimal.Round(temp1, 1).ToString());
            var   descr = currentOWrRO.weather[0].description;
            var   hum   = currentOWrRO.main.humidity;
            var   wind  = currentOWrRO.wind.speed;
            float?prec  = 0;

            if (currentOWrRO.rain != null)
            {
                var rain = currentOWrRO.rain._3h == null ? 0 : currentOWrRO.rain._3h;
                var snow = currentOWrRO.snow._3h == null ? 0 : currentOWrRO.snow._3h;
                prec = rain == 0 ? snow == 0 ? 0 : snow : rain;
            }
            var forec = await OpenWeatherWeatherorecast(location);

            return(new Models.Weather
            {
                Temperatur = temp,
                Loc = location,
                Description = descr.First().ToString().ToUpper() + descr.Substring(1),
                Humidity = hum,
                Precipitation = (float)prec,
                Wind = wind,
                ImgIcon = $"/img/weathericons/{currentOWrRO.weather[0].icon}.png",
                Supplier = new WeatherSupplier {
                    Name = "Open Weather"
                },
                Updated = DateTime.UtcNow.AddHours(2),
                Forecasts = forec
            });
        }
        public async Task <Weather> SMHIWeather(LoactionCoord location)
        {
            var url = $"https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/{location.Longitude}/lat/{location.Latitude}/data.json";


            var SMHIjsonString = await _http.Get(url);

            System.IO.File.AppendAllLines("looog.txt", new[] { $"{DateTime.UtcNow.AddHours(2)} {SMHIjsonString}", "", "", "" });

            var      smhiRootObject = JsonConvert.DeserializeObject <SMHI.Rootobjectsmhi>(SMHIjsonString);
            DateTime now            = new DateTime(DateTime.UtcNow.AddHours(2).Year, DateTime.UtcNow.AddHours(2).Month, DateTime.UtcNow.AddHours(2).Day, DateTime.UtcNow.AddHours(2).Hour, 0, 0);

            var temp       = GetSmhiValue("t", now, smhiRootObject);
            var hum        = GetSmhiValue("r", now, smhiRootObject);
            var prec       = GetSmhiValue("pmean", now, smhiRootObject);
            var wind       = GetSmhiValue("ws", now, smhiRootObject);
            var descr      = GetDescSMHI(GetSmhiValue("Wsymb2", now, smhiRootObject));
            var imgIconUrl = GetSmhiValue("Wsymb2", now, smhiRootObject);

            var forecasts = GetSMHIForecast(smhiRootObject);

            return(new Weather
            {
                Temperatur = temp,
                Loc = location,
                Description = descr,
                Humidity = hum,
                Precipitation = (float)prec,
                Wind = wind,
                ImgIcon = GetImgIcon(imgIconUrl, now),
                Supplier = new WeatherSupplier {
                    Name = "SMHI"
                },
                Updated = DateTime.UtcNow.AddHours(2),
                Forecasts = forecasts
            });
        }
        public async Task <Models.Weather> YRWeather(LoactionCoord location)
        {
            var correctComma = new CultureInfo("en-US");

            XmlDocument doc = new XmlDocument();

            var yrroot = new Models.Rootobject();

            var url         = $"https://api.met.no/weatherapi/locationforecast/1.9/?lat={location.Latitude}&lon={location.Longitude}";
            var weatherInfo = await _http.Get(url);

            doc.LoadXml(weatherInfo);

            string data = JsonConvert.SerializeXmlNode(doc);

            data = data.Replace("@", "");

            var      a    = DateTime.UtcNow.AddHours(2);
            DateTime to   = new DateTime(a.Year, a.Month, a.Day, a.Hour, 0, 0, 0);
            DateTime from = new DateTime(a.Year, a.Month, a.Day, a.Hour + 1, 0, 0, 0);

            yrroot = JsonConvert.DeserializeObject <Models.Rootobject>(data);
            var rootNow       = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from == to).location;
            var rootToGetIcon = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from == to && x.to == from).location.symbol;

            var forecastTemp = new List <Forecast>();
            var forecast     = new List <Forecast>();

            var prog00t = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 00 && x.to.Hour == 00).location.temperature.value;
            var prog00s = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 00 && x.to.Hour == 01).location.symbol.number;

            forecastTemp.Add(new Forecast {
                Temperatur = float.Parse(prog00t, correctComma.NumberFormat), ImgIcon = prog00s, Time = "00"
            });

            var prog03t = float.Parse(yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 03 && x.to.Hour == 03).location.temperature.value, correctComma.NumberFormat);
            var prog03s = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 03 && x.to.Hour == 04).location.symbol.number;

            forecastTemp.Add(new Forecast {
                Temperatur = prog03t, ImgIcon = prog03s, Time = "03"
            });


            var prog06t = float.Parse(yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 06 && x.to.Hour == 06).location.temperature.value, correctComma.NumberFormat);
            var prog06s = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 06 && x.to.Hour == 07).location.symbol.number;

            forecastTemp.Add(new Forecast {
                Temperatur = prog06t, ImgIcon = prog06s, Time = "06"
            });


            var prog09t = float.Parse(yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 09 && x.to.Hour == 09).location.temperature.value, correctComma.NumberFormat);
            var prog09s = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 09 && x.to.Hour == 10).location.symbol.number;

            forecastTemp.Add(new Forecast {
                Temperatur = prog09t, ImgIcon = prog09s, Time = "09"
            });


            var prog12t = float.Parse(yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 12 && x.to.Hour == 12).location.temperature.value, correctComma.NumberFormat);
            var prog12s = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 12 && x.to.Hour == 13).location.symbol.number;

            forecastTemp.Add(new Forecast {
                Temperatur = prog12t, ImgIcon = prog12s, Time = "12"
            });


            var prog15t = float.Parse(yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 15 && x.to.Hour == 15).location.temperature.value, correctComma.NumberFormat);
            var prog15s = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 15 && x.to.Hour == 16).location.symbol.number;

            forecastTemp.Add(new Forecast {
                Temperatur = prog15t, ImgIcon = prog15s, Time = "15"
            });


            var prog18t = float.Parse(yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 18 && x.to.Hour == 18).location.temperature.value, correctComma.NumberFormat);
            var prog18s = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 18 && x.to.Hour == 19).location.symbol.number;

            forecastTemp.Add(new Forecast {
                Temperatur = prog18t, ImgIcon = prog18s, Time = "18"
            });


            var prog21t = float.Parse(yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 21 && x.to.Hour == 21).location.temperature.value, correctComma.NumberFormat);
            var prog21s = yrroot.weatherdata.product.time.FirstOrDefault(x => x.from.Hour == 21 && x.to.Hour == 22).location.symbol.number;

            forecastTemp.Add(new Forecast {
                Temperatur = prog21t, ImgIcon = prog21s, Time = "21"
            });

            foreach (var item in forecastTemp)
            {
                var t = new DateTime(a.Year, a.Month, a.Day, int.Parse(item.Time), 0, 0, 0);

                item.ImgIcon = GetImgIcon(float.Parse(item.ImgIcon, correctComma.NumberFormat), t);
                forecast.Add(item);
            }

            var nått = forecast.Where(x => int.Parse(x.Time) > a.Hour).ToList();

            nått.RemoveRange(3, nått.Count - 3);



            var    unformatedDesc = rootToGetIcon.id;
            var    imgIconUrl     = float.Parse(rootToGetIcon.number);
            string pattern        = $"([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))";
            var    desc           = Regex.Replace(unformatedDesc, pattern, "$1 ");

            var img   = rootToGetIcon.number;
            var temp  = rootNow.temperature.value;
            var humid = rootNow.humidity.value;
            var wind  = rootNow.windSpeed.mps;

            string prec;

            if (rootNow.precipitation == null)
            {
                prec = "0";
            }
            else
            {
                prec = rootNow.precipitation.value;
            }


            return(new Models.Weather
            {
                Temperatur = float.Parse(temp, correctComma.NumberFormat),
                Loc = location,
                Description = desc,
                Humidity = float.Parse(humid, correctComma.NumberFormat),
                Wind = float.Parse(wind, correctComma.NumberFormat),
                ImgIcon = GetImgIcon(imgIconUrl, a),
                Precipitation = float.Parse(prec, correctComma.NumberFormat),
                Supplier = new WeatherSupplier {
                    Name = "YR.no"
                },
                Forecasts = nått.ToList(),
                Updated = DateTime.UtcNow.AddHours(2)
            });
        }