// Path: aula33-weather-async/src/main/java/util/IRequest.java
// public interface IRequest extends AutoCloseable{
//     CompletableFuture<Stream<String>> getContent(String path);
// 
//     @Override
//     public default void close() {
//     }
// }
// 
// Path: aula09-composition-and-decorator/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;
// 
//     public Location(String country, String region, double latitude, double longitude) {
//         this.country = country;
//         this.region = region;
//         this.latitude = latitude;
//         this.longitude = longitude;
//     }
// 
//     public String getCountry() {
//         return country;
//     }
// 
//     public String getRegion() {
//         return region;
//     }
// 
//     public double getLatitude() {
//         return latitude;
//     }
// 
//     public double getLongitude() {
//         return longitude;
//     }
// 
//     public static Location valueOf(String line) {
//         String[] data = line.split("\t");
//         return new Location(
//                 data[1],
//                 data[2],
//                 Double.parseDouble(data[3]),
//                 Double.parseDouble(data[4]));
//     }
// 
//     @Override
//     public String toString() {
//         return "Location{" +
//                 "country='" + country + '\'' +
//                 ", region='" + region + '\'' +
//                 ", latitude=" + latitude +
//                 ", longitude=" + longitude +
//                 '}';
//     }
// }
// 
// Path: aula09-composition-and-decorator/src/main/java/weather/model/WeatherInfo.java
// public class WeatherInfo {
//     private final LocalDate date;     // index 0
//     private final int tempC;          // index 2
//     private final String description; // index 10
//     private final double  precipMM;   // index 11
//     private final int feelsLikeC;     // index 24
// 
//     public WeatherInfo(LocalDate date, int tempC, String description, double precipMM, int feelsLikeC) {
//         this.date = date;
//         this.tempC = tempC;
//         this.description = description;
//         this.precipMM = precipMM;
//         this.feelsLikeC = feelsLikeC;
//     }
// 
//     public LocalDate getDate() {
//         return date;
//     }
// 
//     public int getTempC() {
//         return tempC;
//     }
// 
//     public String getDescription() {
//         return description;
//     }
// 
//     public double getPrecipMM() {
//         return precipMM;
//     }
// 
//     public int getFeelsLikeC() {
//         return feelsLikeC;
//     }
// 
//     @Override
//     public String toString() {
//         return "WeatherInfo{" +
//                 date +
//                 ", tempC=" + tempC +
//                 ", '" + description + '\'' +
//                 ", precipMM=" + precipMM +
//                 ", feelsLikeC=" + feelsLikeC +
//                 '}';
//     }
// 
//     /**
//      * Hourly information follows below the day according to the format of
//      * /past weather resource of the World Weather Online API
//      */
//     public static WeatherInfo valueOf(String line) {
//         String[] data = line.split(",");
//         return new WeatherInfo(
//                 LocalDate.parse(data[0]),
//                 Integer.parseInt(data[2]),
//                 data[10],
//                 Double.parseDouble(data[11]),
//                 Integer.parseInt(data[24]));
//     }
// }


import java.util.List;
import util.IRequest;
import weather.dto.Location;
import weather.dto.WeatherInfo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
               throw new IllegalStateException(
                       "YOU MUST GOT a KEY in developer.worldweatheronline.com and place it in src/main/resources/worldweatheronline-app-key.txt");
            } else {
                InputStream keyStream = keyFile.openStream();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(keyStream))) {
                    WEATHER_TOKEN = reader.readLine();
                }
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private final IRequest req;

    public WeatherWebApi(IRequest req) {
        this.req = req;
    }

    /**
     * E.g. http://api.worldweatheronline.com/free/v2/search.ashx?query=oporto&format=tab&key=*****
     */

    public Iterable<Location> search(String query) {
        String url=WEATHER_HOST + WEATHER_SEARCH + WEATHER_SEARCH_ARGS;
        url = String.format(url, query, WEATHER_TOKEN);
        List<Location> locations= new ArrayList<>();
        Iterator<String> iteratorString= req.getContent(url).iterator();
        while(iteratorString.hasNext()) {
            String line = iteratorString.next();
// Path: aula33-weather-async/src/main/java/util/IRequest.java
// public interface IRequest extends AutoCloseable{
//     CompletableFuture<Stream<String>> getContent(String path);
// 
//     @Override
//     public default void close() {
//     }
// }
// 
// Path: aula09-composition-and-decorator/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;
// 
//     public Location(String country, String region, double latitude, double longitude) {
//         this.country = country;
//         this.region = region;
//         this.latitude = latitude;
//         this.longitude = longitude;
//     }
// 
//     public String getCountry() {
//         return country;
//     }
// 
//     public String getRegion() {
//         return region;
//     }
// 
//     public double getLatitude() {
//         return latitude;
//     }
// 
//     public double getLongitude() {
//         return longitude;
//     }
// 
//     public static Location valueOf(String line) {
//         String[] data = line.split("\t");
//         return new Location(
//                 data[1],
//                 data[2],
//                 Double.parseDouble(data[3]),
//                 Double.parseDouble(data[4]));
//     }
// 
//     @Override
//     public String toString() {
//         return "Location{" +
//                 "country='" + country + '\'' +
//                 ", region='" + region + '\'' +
//                 ", latitude=" + latitude +
//                 ", longitude=" + longitude +
//                 '}';
//     }
// }
// 
// Path: aula09-composition-and-decorator/src/main/java/weather/model/WeatherInfo.java
// public class WeatherInfo {
//     private final LocalDate date;     // index 0
//     private final int tempC;          // index 2
//     private final String description; // index 10
//     private final double  precipMM;   // index 11
//     private final int feelsLikeC;     // index 24
// 
//     public WeatherInfo(LocalDate date, int tempC, String description, double precipMM, int feelsLikeC) {
//         this.date = date;
//         this.tempC = tempC;
//         this.description = description;
//         this.precipMM = precipMM;
//         this.feelsLikeC = feelsLikeC;
//     }
// 
//     public LocalDate getDate() {
//         return date;
//     }
// 
//     public int getTempC() {
//         return tempC;
//     }
// 
//     public String getDescription() {
//         return description;
//     }
// 
//     public double getPrecipMM() {
//         return precipMM;
//     }
// 
//     public int getFeelsLikeC() {
//         return feelsLikeC;
//     }
// 
//     @Override
//     public String toString() {
//         return "WeatherInfo{" +
//                 date +
//                 ", tempC=" + tempC +
//                 ", '" + description + '\'' +
//                 ", precipMM=" + precipMM +
//                 ", feelsLikeC=" + feelsLikeC +
//                 '}';
//     }
// 
//     /**
//      * Hourly information follows below the day according to the format of
//      * /past weather resource of the World Weather Online API
//      */
//     public static WeatherInfo valueOf(String line) {
//         String[] data = line.split(",");
//         return new WeatherInfo(
//                 LocalDate.parse(data[0]),
//                 Integer.parseInt(data[2]),
//                 data[10],
//                 Double.parseDouble(data[11]),
//                 Integer.parseInt(data[24]));
//     }
// }


// Path: aula05-generic-methods/src/main/java/weather/WeatherWebApi.java
import java.util.List;
import util.IRequest;
import weather.dto.Location;
import weather.dto.WeatherInfo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;

               throw new IllegalStateException(
                       "YOU MUST GOT a KEY in developer.worldweatheronline.com and place it in src/main/resources/worldweatheronline-app-key.txt");
            } else {
                InputStream keyStream = keyFile.openStream();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(keyStream))) {
                    WEATHER_TOKEN = reader.readLine();
                }
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private final IRequest req;

    public WeatherWebApi(IRequest req) {
        this.req = req;
    }

    /**
     * E.g. http://api.worldweatheronline.com/free/v2/search.ashx?query=oporto&format=tab&key=*****
     */

    public Iterable<Location> search(String query) {
        String url=WEATHER_HOST + WEATHER_SEARCH + WEATHER_SEARCH_ARGS;
        url = String.format(url, query, WEATHER_TOKEN);
        List<Location> locations= new ArrayList<>();
        Iterator<String> iteratorString= req.getContent(url).iterator();
        while(iteratorString.hasNext()) {
            String line = iteratorString.next();
            if(!line.startsWith("#")) locations.add(Location.valueOf(line));