// 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;
/*
 * 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;



/**
 * @author Miguel Gamboa
 *         created on 07-03-2017
 */
public class WeatherWebApi {

    private static final String WEATHER_TOKEN;
    private static final String WEATHER_HOST = "http://data.worldweatheronline.com";
    private static final String WEATHER_PAST = "/premium/v1/past-weather.ashx";
    private static final String WEATHER_PAST_ARGS =
            "?q=%s&date=%s&enddate=%s&tp=24&format=csv&key=%s";

    static {
        try {
            URL keyFile = ClassLoader.getSystemResource("worldweatheronline-app-key.txt");
            if(keyFile == null) {
               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=*****
     */

// 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: aula02-weather-api/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;

/*
 * 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;



/**
 * @author Miguel Gamboa
 *         created on 07-03-2017
 */
public class WeatherWebApi {

    private static final String WEATHER_TOKEN;
    private static final String WEATHER_HOST = "http://data.worldweatheronline.com";
    private static final String WEATHER_PAST = "/premium/v1/past-weather.ashx";
    private static final String WEATHER_PAST_ARGS =
            "?q=%s&date=%s&enddate=%s&tp=24&format=csv&key=%s";

    static {
        try {
            URL keyFile = ClassLoader.getSystemResource("worldweatheronline-app-key.txt");
            if(keyFile == null) {
               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) {