text: 'use strict';
  
  !(function() {
    var data = {};
    let lat;
    let lon;
  
    navigator.geolocation.getCurrentPosition(function(position) {
      lat = position.coords.latitude;
      lon = position.coords.longitude;
      var databaseURL = 'https://api.openweathermap.org/data/2.5/onecall?lang' + navigator.language + '&lat=' + (localStorage.getItem('lat') == null ? lat.toFixed(2) : localStorage.getItem('lat')) + '&lon=' + (localStorage.getItem('lon') == null ? lon.toFixed(2) : localStorage.getItem('lon')) + '&exclude=current,minutely&appid=41afd1424d58cb17007e745f8e0c798b';
      fetch(databaseURL)
        .then((response) => response.json())
        .catch((e) => {
          console.error(e);
          alert("Can't download database\nPlease reload the page.");
        })
        .then((response) => {
          data = response;
          console.log(databaseURL);
          if (document.readyState === "complete" || document.readyState === "interactive") {
            init();
          } else {
            window.addEventListener("DOMContentLoaded", () => {
              init();
            });
          }
        });
    });
  
    function init() {
      var forecastHourly = document.getElementById('weather-forecast-hourly');
      var forecastDaily = document.getElementById('weather-forecast-daily');
  
      data.hourly.forEach(function(item) {
        var info = document.createElement('div');
        var infoIcon = document.createElement('div');
        var infoTemperature = document.createElement('span');
  
        infoIcon.classList.add('icon');
        switch(item.weather['0'].icon) {
          case '01d':
            infoIcon.dataset.state = 'sunny';
            break;
          case '01n':
            infoIcon.dataset.state = 'night';
            break;
          case '02d':
            infoIcon.dataset.state = 'sunny-cloudy';
            break;
          case '02n':
            infoIcon.dataset.state = 'night-cloudy';
            break;
          case '03d':
          case '03n':
            infoIcon.dataset.state = 'cloudy';
            break;
          case '04d':
          case '04n':
            infoIcon.dataset.state = 'cloudy-broken';
            break;
          case '9d':
          case '9n':
            infoIcon.dataset.state = 'shower-rain';
            break;
          case '10d':
          case '10n':
            infoIcon.dataset.state = 'raining';
            break;
          case '11d':
          case '11n':
            infoIcon.dataset.state = 'thunderstorm';
            break;
          case '50d':
          case '50n':
            infoIcon.dataset.state = 'mist';
            break;
        }
  
        infoTemperature.innerText = parseInt(item.temp - 273.15).toLocaleString() + '°C';
  
        info.appendChild(infoIcon);
        info.appendChild(infoTemperature);
        forecastHourly.appendChild(info);
      });
  
      data.daily.forEach(function(item) {
        var info = document.createElement('div');
        var infoIcon = document.createElement('div');
        var infoTemperatureDay = document.createElement('span');
        var infoTemperatureNight = document.createElement('span');
  
        infoIcon.classList.add('icon');
        switch(item.weather['0'].icon) {
          case '01d':
            infoIcon.dataset.state = 'sunny';
            break;
          case '01n':
            infoIcon.dataset.state = 'night';
            break;
          case '02d':
            infoIcon.dataset.state = 'sunny-cloudy';
            break;
          case '02n':
            infoIcon.dataset.state = 'night-cloudy';
            break;
          case '03d':
          case '03n':
            infoIcon.dataset.state = 'cloudy';
            break;
          case '04d':
          case '04n':
            infoIcon.dataset.state = 'cloudy-broken';
            break;
          case '9d':
          case '9n':
            infoIcon.dataset.state = 'shower-rain';
            break;
          case '10d':
          case '10n':
            infoIcon.dataset.state = 'raining';
            break;
          case '11d':
          case '11n':
            infoIcon.dataset.state = 'thunderstorm';
            break;
          case '50d':
          case '50n':
            infoIcon.dataset.state = 'mist';
            break;
        }
  
        infoTemperatureDay.innerText = parseInt(item.temp.day - 273.15).toLocaleString() + '°C';
        infoTemperatureNight.innerText = parseInt(item.temp.night - 273.15).toLocaleString() + '°C';
  
        info.appendChild(infoIcon);
        info.appendChild(infoTemperatureDay);
        info.appendChild(infoTemperatureNight);
        forecastDaily.appendChild(info);
      });
    }
  })();
  