// Path: aula33-weather-async/src/main/java/weather/WeatherService.java
// public class WeatherService implements AutoCloseable{
// 
//     private final WeatherWebApi api;
// 
//     public WeatherService(WeatherWebApi api) {
//         this.api = api;
//     }
// 
//     public WeatherService() {
//         api = new WeatherWebApi(new HttpRequest());
//     }
// 
//     public CompletableFuture<Stream<Location>> search(String query) {
//         return api.search(query)
//                 .thenApply(str -> str
//                     .map(this::dtoToLocation));
//     }
// 
//     protected Location dtoToLocation(LocationDto loc) {
//         return new Location(
//                 loc.getCountry(),
//                 loc.getRegion(),
//                 loc.getLatitude(),
//                 loc.getLongitude(),
//                 last30daysWeather(loc.getLatitude(), loc.getLongitude())::join,
//                 (from, to) -> pastWeather(loc.getLatitude(), loc.getLongitude(), from, to).join());
//     }
// 
//     public CompletableFuture<Stream<WeatherInfo>> last30daysWeather(double lat, double log) {
//         return this.pastWeather(lat, log, now().minusDays(30), now().minusDays(1));
//     }
// 
//     private static WeatherInfo dtoToWeatherInfo(WeatherInfoDto dto) {
//         return new WeatherInfo(
//                 dto.getDate(),
//                 dto.getTempC(),
//                 dto.getDescription(),
//                 dto.getPrecipMM(),
//                 dto.getFeelsLikeC());
//     }
// 
//     public CompletableFuture<Stream<WeatherInfo>> pastWeather(double lat, double log, LocalDate from, LocalDate to) {
//         return api
//                 .pastWeather(lat, log, from, to)
//                 .thenApply(str -> str
//                     .map(dto -> dtoToWeatherInfo(dto)));
//     }
// 
//     @Override
//     public void close() throws Exception {
//         api.close();
//     }
// }
// 
// Path: aula19-optional-and-default-methods/src/main/java/weather/model/Location.java
// public class Location {
//     private final String country;
//     private final String region;
//     private final double latitude;
//     private final double longitude;
//     private final Iterable<WeatherInfo> last30daysWeather;
//     private final BiFunction<LocalDate, LocalDate, Iterable<WeatherInfo>> pastWeather;
// 
//     public Location(String country, String region, double latitude, double longitude, Iterable<WeatherInfo> last30daysWeather, BiFunction<LocalDate, LocalDate, Iterable<WeatherInfo>> pastWeather) {
//         this.country = country;
//         this.region = region;
//         this.latitude = latitude;
//         this.longitude = longitude;
//         this.last30daysWeather = last30daysWeather;
//         this.pastWeather = pastWeather;
//     }
// 
//     public String getCountry() {
//         return country;
//     }
// 
//     public String getRegion() {
//         return region;
//     }
// 
//     public double getLatitude() {
//         return latitude;
//     }
// 
//     public double getLongitude() {
//         return longitude;
//     }
// 
//     public Iterable<WeatherInfo> getLast30daysWeather() {
//         return last30daysWeather;
//     }
// 
//     public Iterable<WeatherInfo> getPastWeather(LocalDate from, LocalDate to) {
//         return pastWeather.apply(from, to);
//     }
// 
//     @Override
//     public String toString() {
//         return "Location{" +
//                 "country='" + country + '\'' +
//                 ", region='" + region + '\'' +
//                 ", latitude=" + latitude +
//                 ", longitude=" + longitude +
//                 ", last30daysWeather=" + last30daysWeather +
//                 '}';
//     }
// }


import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static java.lang.ClassLoader.getSystemResource;
import static java.nio.file.Files.lines;
import weather.WeatherService;
import weather.model.Location;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
/*
 * Copyright (c) 2017, Miguel Gamboa
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package weather.controllers;




/**
 * @author Miguel Gamboa
 *         created on 06-06-2017
 */
public class WeatherController {

    private final String root;

// Path: aula33-weather-async/src/main/java/weather/WeatherService.java
// public class WeatherService implements AutoCloseable{
// 
//     private final WeatherWebApi api;
// 
//     public WeatherService(WeatherWebApi api) {
//         this.api = api;
//     }
// 
//     public WeatherService() {
//         api = new WeatherWebApi(new HttpRequest());
//     }
// 
//     public CompletableFuture<Stream<Location>> search(String query) {
//         return api.search(query)
//                 .thenApply(str -> str
//                     .map(this::dtoToLocation));
//     }
// 
//     protected Location dtoToLocation(LocationDto loc) {
//         return new Location(
//                 loc.getCountry(),
//                 loc.getRegion(),
//                 loc.getLatitude(),
//                 loc.getLongitude(),
//                 last30daysWeather(loc.getLatitude(), loc.getLongitude())::join,
//                 (from, to) -> pastWeather(loc.getLatitude(), loc.getLongitude(), from, to).join());
//     }
// 
//     public CompletableFuture<Stream<WeatherInfo>> last30daysWeather(double lat, double log) {
//         return this.pastWeather(lat, log, now().minusDays(30), now().minusDays(1));
//     }
// 
//     private static WeatherInfo dtoToWeatherInfo(WeatherInfoDto dto) {
//         return new WeatherInfo(
//                 dto.getDate(),
//                 dto.getTempC(),
//                 dto.getDescription(),
//                 dto.getPrecipMM(),
//                 dto.getFeelsLikeC());
//     }
// 
//     public CompletableFuture<Stream<WeatherInfo>> pastWeather(double lat, double log, LocalDate from, LocalDate to) {
//         return api
//                 .pastWeather(lat, log, from, to)
//                 .thenApply(str -> str
//                     .map(dto -> dtoToWeatherInfo(dto)));
//     }
// 
//     @Override
//     public void close() throws Exception {
//         api.close();
//     }
// }
// 
// Path: aula19-optional-and-default-methods/src/main/java/weather/model/Location.java
// public class Location {
//     private final String country;
//     private final String region;
//     private final double latitude;
//     private final double longitude;
//     private final Iterable<WeatherInfo> last30daysWeather;
//     private final BiFunction<LocalDate, LocalDate, Iterable<WeatherInfo>> pastWeather;
// 
//     public Location(String country, String region, double latitude, double longitude, Iterable<WeatherInfo> last30daysWeather, BiFunction<LocalDate, LocalDate, Iterable<WeatherInfo>> pastWeather) {
//         this.country = country;
//         this.region = region;
//         this.latitude = latitude;
//         this.longitude = longitude;
//         this.last30daysWeather = last30daysWeather;
//         this.pastWeather = pastWeather;
//     }
// 
//     public String getCountry() {
//         return country;
//     }
// 
//     public String getRegion() {
//         return region;
//     }
// 
//     public double getLatitude() {
//         return latitude;
//     }
// 
//     public double getLongitude() {
//         return longitude;
//     }
// 
//     public Iterable<WeatherInfo> getLast30daysWeather() {
//         return last30daysWeather;
//     }
// 
//     public Iterable<WeatherInfo> getPastWeather(LocalDate from, LocalDate to) {
//         return pastWeather.apply(from, to);
//     }
// 
//     @Override
//     public String toString() {
//         return "Location{" +
//                 "country='" + country + '\'' +
//                 ", region='" + region + '\'' +
//                 ", latitude=" + latitude +
//                 ", longitude=" + longitude +
//                 ", last30daysWeather=" + last30daysWeather +
//                 '}';
//     }
// }


// Path: aula34-weather-webapp/src/main/java/weather/controllers/WeatherController.java
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static java.lang.ClassLoader.getSystemResource;
import static java.nio.file.Files.lines;
import weather.WeatherService;
import weather.model.Location;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;

/*
 * Copyright (c) 2017, Miguel Gamboa
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package weather.controllers;




/**
 * @author Miguel Gamboa
 *         created on 06-06-2017
 */
public class WeatherController {

    private final String root;

    private final WeatherService api;