package com.tuoheng.admin.service.airportline.query.inspection;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.code.AriportCodeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.tzhl.response.FlyLinePointResult;
import com.tuoheng.admin.tzhl.response.TZHLAirportLineResponse;
import com.tuoheng.admin.tzhl.service.airportline.TZHLGetAirportLineListService;
import com.tuoheng.admin.vo.airport.AirPortLineLocationVo;
import com.tuoheng.admin.vo.airport.AirPortStatusVO;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.HttpUtils;
import com.tuoheng.common.core.utils.JacksonUtil;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 根据任务ID获取航线，从机场平台获取
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-04-26
 */
@Slf4j
@Service
public class QueryAirportLineByAirportService {

    @Autowired
    private TZHLGetAirportLineListService tzhlGetAirportLineListService;

    /**
     * 根据任务ID获取航线，从机场平台获取
     *
     * @return
     */
    public JsonResult getAirportLineList(Inspection inspection) {
        log.info("进入根据任务ID获取航线，从机场平台获取");
        Long inspectionLine = inspection.getInspectionLine();
        JsonResult result = this.checkParam(inspectionLine);
        if (0 != result.getCode()) {
            log.info("根据任务ID获取航线：校验参数失败：{}", result.getMsg());
            return result;
        }

        TZHLAirportLineResponse tzhlAirportLineResponse = tzhlGetAirportLineListService.getAirportLineById(inspection.getInspectionLine());
        if (ObjectUtil.isEmpty(tzhlAirportLineResponse)) {
            log.info("根据任务ID获取航线：航线不存在：{}", inspection.getInspectionLine());
            return JsonResult.error("航线不存在");
        }

        List<FlyLinePointResult> points = tzhlAirportLineResponse.getPoints();

        if (CollectionUtil.isEmpty(points)) {
            log.info("根据任务ID获取航线：航线点集合为空");
            return JsonResult.error("航线点集合为空");
        }
        List<AirPortLineLocationVo> airPortLineLocationVoList = new ArrayList<>();
        AirPortLineLocationVo airPortLineLocationVo;
        for (FlyLinePointResult flyLinePointResult : points) {
            airPortLineLocationVo = new AirPortLineLocationVo();
            airPortLineLocationVo.setLat(flyLinePointResult.getLatitude());
            airPortLineLocationVo.setLon(flyLinePointResult.getLongitude());
            airPortLineLocationVoList.add(airPortLineLocationVo);
        }
        return JsonResult.success(airPortLineLocationVoList);
    }

    private JsonResult checkParam(Long inspectionLine) {
        if (ObjectUtil.isEmpty(inspectionLine)) {
            log.info("进入根据任务ID获取航线，从机场平台获取业务，航线ID为空，inspectionLine={}", inspectionLine);
            throw new ServiceException("航线ID为空");
        }
        return JsonResult.success();
    }

}
