{"size": 2471, "ext": "js", "max_stars_repo_path": "app/weather/weather.js", "max_stars_repo_name": "LEhyphenM/weather-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/weather/weather.js", "max_issues_repo_name": "LEhyphenM/weather-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/weather/weather.js", "max_forks_repo_name": "LEhyphenM/weather-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.5333333333, "max_line_length": 115, "alphanum_fraction": 0.6386078511}
'use strict';

angular.module('weatherApp.index', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/', {
    templateUrl: 'weather/weather.html',
    controller: 'WeatherCtrl'
  });
}])

.controller('WeatherCtrl', ['$scope', '$http', function($scope, $http) {
  
  // Set the API Key for OpenWeatherMap
  const apiKey = 'dc5fcd401da48ff99506f885c0bc0c14';

  // Set the forecast API URL
  const forecastUrl = `http://api.openweathermap.org/data/2.5/forecast/daily?cnt=6&appid=${apiKey}&units=imperial`;

  // Get date & time 
  $scope.date = new Date();
  
  // Scope data
  $scope.data = {
    loading: false,
    error: false,
    weather: {},
    zipcode: null,
    coordinates: {
      lat: null,
      lon: null
    }
  }

  // Asks user for browser location
  const getUserLocation = () => {
    $scope.data.loading = true;
    navigator
      .geolocation
      .getCurrentPosition(setUserCoordinates, setDefaultZipcode);
  }

  // Set the browser-granted geolocation coordinates
  const setUserCoordinates = (result) => {
    $scope.data.coordinates.lat = result.coords.latitude;
    $scope.data.coordinates.lon = result.coords.longitude;

    getWeatherByCoordinates();
  }

  // Get weather by coordinates
  const getWeatherByCoordinates = () => {
    let lat = $scope.data.coordinates.lat;
    let lon = $scope.data.coordinates.lon;
    let url = `&lat=${lat}&lon=${lon}`;

    getWeather(url);
  }

  //  Get the weather by default zipcode
  const getWeatherByZipcode = () => {
    let zip = $scope.data.zipcode;
    let url = `&zip=${zip},us`;

    getWeather(url);
  }

  // Use the default zip code
  const setDefaultZipcode = () => {
    $scope.data.zipcode = 60661;

    getWeatherByZipcode();
  }

  // Get the weather by URL
  const getWeather = (url) => {
    $scope.data.loading = true;
    $http
      .get(`${forecastUrl}${url}`)

    .then(result => {
      $scope.data.error = false;
      $scope.data.weather = result.data;
      $scope.data.loading = false;
    })
    .catch(handleWeatherFailure);
  }

  //  Set the weather data on the scope
  const setWeatherData = (result) => {
    $scope.data.weather = result.data;
  }

  // Handle the API failure
  const handleWeatherFailure = (error) => {
    $scope.data.error = true;
    $scope.data.loading = false;
  }

  // Scope function declarations
  $scope.getUserLocation = getUserLocation;
  $scope.getWeatherByZipcode = getWeatherByZipcode;

}]);
