package com.sagacloud.endpoints;



import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sagacloud.endpoints.mng.Project;
import com.sagacloud.utils.Const;
import com.sagacloud.utils.DpUtils;
import com.sagacloud.utils.SunriseSunset;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;


@Path("sunrise_sunset")
public class SunRiseAndSet {


    private  String GetSunrise(double lat, double lon, String date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date currDate = sdf.parse(date);
        SunriseSunset ss1 = new SunriseSunset(lat, lon, currDate, 0);
        if (ss1.getSunrise() == null) {
            return null;
        } else {
            return sdf_.format(ss1.getSunrise());
        }
    }

    private String GetSunset(double lat, double lon, String date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date currDate = sdf.parse(date);
        SunriseSunset ss2 = new SunriseSunset(lat, lon, new Date(currDate.getTime() + 1000L * 60 * 60 * 24), 0);
        if (ss2.getSunset() == null) {
            return null;
        } else {
            return sdf_.format(ss2.getSunset());
        }
    }

    @Context
    private ResourceContext resourceContext;

    @Path("/query")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String query(@QueryParam(Const.PROJECT_ID) String projectId, String jsonStr) throws Exception {
        JSONArray content = JSONObject.parseObject(resourceContext.getResource(Project.class).query(projectId, "false")).getJSONArray("Content");
        if(content.size() == 0){
            return DpUtils.errorJsonMsg("Project not existed.");
        }
        JSONObject infos = content.getJSONObject(0).getJSONObject("infos");
        Long lat = infos.getLong("Latitude");
        Long lon = infos.getLong("Longitude");
        if(lat == null || lon == null){
            return DpUtils.errorJsonMsg("Project not have Latitude or Longitude set.");
        }
        String dateStr = JSONObject.parseObject(jsonStr).getString("date");
        if(dateStr == null || dateStr.isEmpty()){
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            dateStr = LocalDateTime.now().format(format);
        }
        JSONObject item = new JSONObject().fluentPut("sunrise", GetSunrise(lat, lon, dateStr)).fluentPut("sunset", GetSunset(lat, lon, dateStr));
        return DpUtils.successJsonResp("", new JSONArray().fluentAdd(item)).toString();
    }


}
