package tw.kewang.cwb;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tw.kewang.cwb.datalist.FD0047;
import tw.kewang.cwb.dictionary.Location;
import tw.kewang.cwb.pretty.FutureWeatherByCity;
import tw.kewang.cwb.pretty.FutureWeatherByTown;
import tw.kewang.cwb.utils.JsonUtils;

import java.io.IOException;
import java.net.Proxy;
import java.util.Date;

public class CwbSender {
    private static final Logger LOG = LoggerFactory.getLogger(CwbSender.class);

    private OkHttpClient client;

    public CwbSender(Cwb cwb, Proxy proxy) {
        this.client = new OkHttpClient.Builder().proxy(proxy).addInterceptor(new TokenInterceptor(cwb)).build();
    }

    public FutureWeatherByCity sendFutureWeatherByCity(FD0047.ByCity city) {
        Request req = new Request.Builder().url("http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-" + city.getDataId()).build();

        try {
            Response res = client.newCall(req).execute();

            String body = res.body().string();

            FD0047 rawData = JsonUtils.fromJson(body, FD0047.class);

            return new FutureWeatherByCity(rawData);
        } catch (IOException e) {
            LOG.error("Caught Exception: ", e);

            return new FutureWeatherByCity();
        }
    }

    public FutureWeatherByTown sendFutureWeatherByTown(FD0047.ByCity city, Geocode geocode, Date date) {
        Request req = new Request.Builder().url("http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-" + city.getDataId()).build();

        try {
            Response res = client.newCall(req).execute();

            String body = res.body().string();

            FD0047 rawData = JsonUtils.fromJson(body, FD0047.class);

            for (Location location : rawData.getRecords().getLocations().get(0).getLocation()) {
                if (location.getGeocode().equalsIgnoreCase(geocode.getCode())) {
                    return new FutureWeatherByTown(location, date);
                }
            }

            return new FutureWeatherByTown();
        } catch (IOException e) {
            LOG.error("Caught Exception: ", e);

            return new FutureWeatherByTown();
        }
    }
}
@@ -74,33 +74,8 @@
 st;%0A
-import okhttp3.Response;%0A
 impo
@@ -519,24 +519,199 @@
 nder.class);
+%0A    private static final String ERROR_PREFIX = %22Caught Exception: %22;%0A    private static final String URL_FD0047 = %22http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-%22;
 %0A%0A    privat
@@ -1028,67 +1028,18 @@
 url(
-%22http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-
+URL_F
 D0047
--%22
  + c
@@ -1095,169 +1095,47 @@
     
-Response res = client.newCall(req).execute();%0A%0A            String body = res.body().string();%0A%0A            FD0047 rawData = JsonUtils.fromJson(body, FD0047.class
+FD0047 rawData = buildFD0047RawData(req
 );%0A%0A
@@ -1243,36 +1243,28 @@
 G.error(
-%22Caught Exception: %22
+ERROR_PREFIX
 , e);%0A%0A 
@@ -1482,67 +1482,18 @@
 url(
-%22http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-
+URL_F
 D0047
--%22
  + c
@@ -1549,169 +1549,47 @@
     
-Response res = client.newCall(req).execute();%0A%0A            String body = res.body().string();%0A%0A            FD0047 rawData = JsonUtils.fromJson(body, FD0047.class
+FD0047 rawData = buildFD0047RawData(req
 );%0A%0A
@@ -1969,36 +1969,28 @@
 G.error(
-%22Caught Exception: %22
+ERROR_PREFIX
 , e);%0A%0A 
@@ -2049,10 +2049,214 @@
  %7D%0A    %7D
+%0A%0A    private FD0047 buildFD0047RawData(Request req) throws IOException %7B%0A        String body = client.newCall(req).execute().body().string();%0A%0A        return JsonUtils.fromJson(body, FD0047.class);%0A    %7D
 %0A%7D

Refactor CwbSender