package com.tuoheng.admin.service.index;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.vo.AirportMsgVo;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.HttpUtils;
import com.tuoheng.common.core.utils.JacksonUtil;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Author ChengWang
 * @Date 2023/3/16
 */
@Service
@Slf4j
public class IndexServiceImpl implements IndexService{


    @Override
    public JsonResult getAirportMsg(Integer airportId) {
        String url = CommonConfig.airportURL + SystemConstant.API_AIRPORT_STATUS;
        String param = "airportId=" + airportId;
        String result = HttpUtils.sendGet(url, param);
        JsonResult jsonResult = JacksonUtil.json2pojo(result, JsonResult.class);
        AirportMsgVo airportMsgVo = new AirportMsgVo();
        JSONObject dataObject = (JSONObject) JSONObject.toJSON(jsonResult.getData());
        JSONObject wthJson = dataObject.getJSONObject("WTH");
        if(Objects.nonNull(wthJson)){
            JSONObject parmJson = wthJson.getJSONObject("parmNew");
            BigDecimal hum = parmJson.getBigDecimal("Hum").divide(new BigDecimal(10), 1, BigDecimal.ROUND_HALF_UP);
            airportMsgVo.setHum(hum + "rh");
            BigDecimal tmp = parmJson.getBigDecimal("Tmp").divide(new BigDecimal(10), 1, BigDecimal.ROUND_HALF_UP);
            airportMsgVo.setTmp(tmp + "℃");

            airportMsgVo.setWspd(parmJson.getString("WSPD") + "m/s")
                    .setWdir(parmJson.getString("WDIR") + "度")
                    .setRainfull(parmJson.getString("Rainfull") + "mm")
                    .setWidrName(parmJson.getString("WDIRNAME"));
            //用角度表示风向，是把圆周分成360度，北风(N)是0度(即360度)，东风(E)是90度，南风(S)是180度，西风(W)是270度
            BigDecimal hpa = parmJson.getBigDecimal("Hpa");
            if(hpa != null){
                BigDecimal mpa = hpa.divide(new BigDecimal(100000), 1, BigDecimal.ROUND_HALF_UP);
                airportMsgVo.setHpa(mpa + "Mpa");
                airportMsgVo.setHpa(hpa + "Mpa");
            }
        }
        JSONObject tahJson = dataObject.getJSONObject("TAH");
        if(Objects.nonNull(tahJson)){
            JSONObject parmJson = tahJson.getJSONObject("parmNew");
            BigDecimal hum = parmJson.getBigDecimal("Hum").divide(new BigDecimal(10), 1, BigDecimal.ROUND_HALF_UP);
            airportMsgVo.setHum(hum + "rh");
            BigDecimal tmp = parmJson.getBigDecimal("Tmp").divide(new BigDecimal(10), 1, BigDecimal.ROUND_HALF_UP);
            airportMsgVo.setTmp(tmp + "℃");
        }
        JSONObject mountJson = dataObject.getJSONObject("mount");
        if(Objects.nonNull(mountJson)){
            airportMsgVo.setMountName(mountJson.getString("cameraName") + "、" +
                    mountJson.getString("megaphoneName") + "、" +
                    mountJson.getString("searchlightName")
            );
        }
        airportMsgVo.setStatus(dataObject.getString("status"));

        return JsonResult.success(airportMsgVo);
    }
}
