/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package th.or.nectec.tanrabad.survey.service;

import com.bluelinelabs.logansquare.LoganSquare;
import org.joda.time.DateTime;
import th.or.nectec.tanrabad.entity.Place;
import th.or.nectec.tanrabad.survey.TanrabadApp;
import th.or.nectec.tanrabad.survey.presenter.AccountUtils;
import th.or.nectec.tanrabad.survey.service.json.JsonEntomology;

import java.io.IOException;
import java.util.List;

public class EntomologyRestService extends AbsRestService<JsonEntomology> {

    public static final String PATH = "/entomology";
    private Place place;

    public EntomologyRestService(Place place) {
        this(BASE_API, new ServiceLastUpdatePreference(TanrabadApp.getInstance(), PATH), place);
    }

    public EntomologyRestService(String apiBaseUrl, ServiceLastUpdate serviceLastUpdate, Place place) {
        super(apiBaseUrl, serviceLastUpdate);
        this.place = place;
    }

    @Override
    public String getDefaultParams() {
        return "geostd=4326&place_id=" + place.getId() + getWeekIntervalParam()
                + getOrgIdParam();
    }

    private String getOrgIdParam() {
        return "&org_id=" + AccountUtils.getUser().getOrganizationId();
    }

    public String getWeekIntervalParam() {
        DateTime dateTime = new DateTime();
        return "&startdate=" + getUnixTime(dateTime.minusDays(7)) + "&enddate=" + getUnixTime(dateTime);
    }

    public long getUnixTime(DateTime dateTime) {
        return dateTime.getMillis() / 1000;
    }

    @Override
    protected String getPath() {
        return PATH;
    }

    @Override
    protected List<JsonEntomology> jsonToEntityList(String responseBody) throws IOException {
        return LoganSquare.parseList(responseBody, JsonEntomology.class);
    }
}

@@ -1193,16 +1193,22 @@
 private 
+final 
 Place pl
@@ -1573,102 +1573,438 @@
    p
-ublic String getDefaultParams() %7B%0A        return %22geostd=4326&place_id=%22 + place.getId() + get
+rotected String getPath() %7B%0A        return PATH;%0A    %7D%0A%0A    @Override%0A    protected List%3CJsonEntomology%3E jsonToEntityList(String responseBody) throws IOException %7B%0A        return LoganSquare.parseList(responseBody, JsonEntomology.class);%0A    %7D%0A%0A    @Override%0A    public String getDefaultParams() %7B%0A        return %22geostd=4326%22%0A                + %22&%22 + placeIdParam()%0A                + %22&%22 + OrgIdParam()%0A                + %22&%22 + one
 Week
@@ -2022,40 +2022,93 @@
 am()
+;
 %0A    
+%7D%0A%0A
     
-        + getOrgIdParam
+private String placeIdParam() %7B%0A        return %22place_id=%22 + place.getId
 ();%0A
@@ -2133,19 +2133,16 @@
  String 
-get
 OrgIdPar
@@ -2164,17 +2164,16 @@
 return %22
-&
 org_id=%22
@@ -2231,29 +2231,30 @@
 %7D%0A%0A    p
-ublic
+rivate
  String 
 getWeekI
@@ -2245,19 +2245,19 @@
  String 
-get
+one
 WeekInte
@@ -2330,17 +2330,16 @@
 return %22
-&
 startdat
@@ -2336,36 +2336,33 @@
  %22startdate=%22 + 
-getU
+u
 nixTime(dateTime
@@ -2392,20 +2392,17 @@
 ate=%22 + 
-getU
+u
 nixTime(
@@ -2428,23 +2428,21 @@
    p
-ublic
+rivate
  long 
-getU
+u
 nixT
@@ -2519,271 +2519,7 @@
    %7D
-%0A%0A    @Override%0A    protected String getPath() %7B%0A        return PATH;%0A    %7D%0A%0A    @Override%0A    protected List%3CJsonEntomology%3E jsonToEntityList(String responseBody) throws IOException %7B%0A        return LoganSquare.parseList(responseBody, JsonEntomology.class);%0A    %7D
 %0A%7D%0A

Refactor EntomologyRestService