package com.tuoheng.miniprogram.service.impl;

import cn.hutool.core.util.ObjectUtil;
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 com.tuoheng.common.core.utils.StringUtils;
import com.tuoheng.miniprogram.dao.TenantMapper;
import com.tuoheng.miniprogram.entity.Tenant;
import com.tuoheng.miniprogram.enums.AriportCodeEnum;
import com.tuoheng.miniprogram.service.AirportService;
import com.tuoheng.miniprogram.utils.ShiroUtils;
import com.tuoheng.miniprogram.vo.AirLineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class AirportServiceImpl implements AirportService {

    private final static String API_AIRPORT_LIST = "/api/airportInterface/airportList";
    private final static String API_AIRPORT_LINE_LIST = "/api/airportInterface/taskByDroneId";

    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public JsonResult getAirportList() {
        //读取不同租户的机场平台url
        Tenant tenant = tenantMapper.selectById(ShiroUtils.getTenantId());

        if (ObjectUtil.isEmpty(tenant)) {
            return JsonResult.error(AriportCodeEnum.GET_NO_TENANT.getCode(), AriportCodeEnum.GET_NO_TENANT.getMsg());
        }

        if (StringUtils.isEmpty(CommonConfig.airportURL)) {
            return JsonResult.error(AriportCodeEnum.GET_NO_TENANT.getCode(), AriportCodeEnum.GET_NO_TENANT.getMsg());
        }

        String url = CommonConfig.airportURL + API_AIRPORT_LIST;
        String param = "page=1&limit=100&tenantCode=" + tenant.getCode();
        String airPortStr = HttpUtils.sendGet(url, param);

        JsonResult jsonResult = JacksonUtil.json2pojo(airPortStr, JsonResult.class);

        if (ObjectUtil.isEmpty(jsonResult) || (!ObjectUtil.isEmpty(jsonResult.getData()) && jsonResult.getCode() != 0)) {
            return JsonResult.error(AriportCodeEnum.BAD_REQUEST.getCode(), AriportCodeEnum.BAD_REQUEST.getMsg());
        }
        return jsonResult;
    }

    @Override
    public JsonResult getAirLineList(Integer droneId) {
        //读取不同租户的机场平台url
        Tenant tenant = tenantMapper.selectById(ShiroUtils.getTenantId());
        if (ObjectUtil.isEmpty(tenant)) {
            return JsonResult.error(AriportCodeEnum.GET_NO_TENANT.getCode(), AriportCodeEnum.GET_NO_TENANT.getMsg());
        }
        if (StringUtils.isEmpty(CommonConfig.airportURL)) {
            return JsonResult.error(AriportCodeEnum.GET_NO_TENANT.getCode(), AriportCodeEnum.GET_NO_TENANT.getMsg());
        }

        String url = CommonConfig.airportURL + API_AIRPORT_LINE_LIST;
        String param = "page=1&limit=100&droneId=" + droneId;
        String airPortStr = HttpUtils.sendGet(url, param);
        JsonResult<AirLineVO> jsonResult = JacksonUtil.json2pojo(airPortStr, JsonResult.class);

        if (ObjectUtil.isEmpty(jsonResult) || (!ObjectUtil.isEmpty(Objects.requireNonNull(jsonResult).getData()) && jsonResult.getCode() != 0)) {
            return JsonResult.error(AriportCodeEnum.BAD_REQUEST.getCode(), AriportCodeEnum.BAD_REQUEST.getMsg());
        }
        return jsonResult;
    }
}
