/*
 * 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 org.tanrabad.survey.service;

import com.bluelinelabs.logansquare.LoganSquare;

import org.joda.time.DateTime;
import org.tanrabad.survey.BuildConfig;
import org.tanrabad.survey.TanrabadApp;
import org.tanrabad.survey.entity.Place;
import org.tanrabad.survey.service.json.JsonEntomology;
import org.tanrabad.survey.utils.http.QueryStringBuilder;

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

public class EntomologyRestService extends AbsRestService<JsonEntomology> {

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

    public EntomologyRestService(Place place) {
        this(ImpRestServiceConfig.getInstance().getApiBaseUrl(),
                new ApiSyncInfoPreference(TanrabadApp.getInstance(), PATH), place);
    }

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

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

    @Override
    public String getQueryString() {
        return new QueryStringBuilder()
                .add("geostd=4326")
                .add(placeIdParam())
                .add(oneWeekIntervalParam())
                .build();
    }

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

    private String placeIdParam() {
        return "place_id=" + place.getId();
    }

    private String oneWeekIntervalParam() {
        DateTime dateTime = new DateTime();
        return "startdate=" + unixTime(dateTime.minusDays(BuildConfig.SURVEY_RANGE_DAY));
    }

    private long unixTime(DateTime dateTime) {
        return dateTime.getMillis() / 1000;
    }
}

@@ -741,17 +741,16 @@
 Square;%0A
-%0A
 import o
@@ -893,16 +893,67 @@
 .Place;%0A
+import org.tanrabad.survey.presenter.AccountUtils;%0A
 import o
@@ -1938,32 +1938,67 @@
 ntervalParam())%0A
+                .add(orgIdParam())%0A
                 
@@ -2571,11 +2571,123 @@
 0;%0A    %7D
+%0A%0A    private String orgIdParam() %7B%0A        return %22org_id=%22 + AccountUtils.getUser().getOrganizationId();%0A    %7D
 %0A%7D%0A

Revert "Don't care org_id on entomology result service"