package com.tuoheng.admin.tzhl.service.fly.log;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.tzhl.constant.TZHLConstant;
import com.tuoheng.admin.tzhl.request.TZHLFlyDataRequest;
import com.tuoheng.admin.tzhl.request.TZHLFlyLogRequest;
import com.tuoheng.admin.tzhl.response.TZHLFlyDataResponse;
import com.tuoheng.admin.tzhl.response.TZHLFlyLogResponse;
import com.tuoheng.admin.tzhl.response.TZHLTokenResponse;
import com.tuoheng.admin.tzhl.service.CallTianYiPlatformService;
import com.tuoheng.common.core.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class FlyLogService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private CallTianYiPlatformService callTianYiPlatformService;

    @Autowired
    private FlyLogBySuccessService flyLogBySuccessService;

    @Autowired
    private FlyLogByErrorService flyLogByErrorService;

    @Autowired
    private FlyLogByNormalService flyLogByNormalService;

    public void getFlyLog(Inspection inspection) {
        String apiPath = TZHLConstant.TIAN_YI_API_FLIGHT_LOG;
        TZHLFlyLogRequest request = new TZHLFlyLogRequest();
        request.setRecordId(inspection.getRecordId());
        TZHLFlyLogResponse flyLogResponseOld = (TZHLFlyLogResponse) redisUtils.get(TZHLConstant.HL_FLY_LOG + inspection.getId());
        if (ObjectUtil.isNotEmpty(flyLogResponseOld)) {
            String createTime = flyLogResponseOld.getCreateTime();
            request.setCreateTime(createTime);
        }
        String dataJson = callTianYiPlatformService.callPost(apiPath, request);
        List<TZHLFlyLogResponse> flyLogResponseList = JSON.parseArray(dataJson, TZHLFlyLogResponse.class);

        if (ObjectUtil.isEmpty(flyLogResponseList)) {
            log.info("获取飞行日志，flyLogResponseList为空, inspectionId={}", inspection.getId());
            return;
        }

        //根据日期进行降序排序
        List<TZHLFlyLogResponse> flyLogResponseListSortDesc = flyLogResponseList.stream().sorted(Comparator.comparing(TZHLFlyLogResponse::getCreateTime).reversed()).collect(Collectors.toList());
        TZHLFlyLogResponse flyLogResponseLast = flyLogResponseListSortDesc.get(0);
        redisUtils.set(TZHLConstant.HL_FLY_LOG + inspection.getId(), flyLogResponseLast);

        for (TZHLFlyLogResponse flyLogResponse : flyLogResponseList) {
            log.info("获取飞行日志，flyLogResponse={}", flyLogResponse);
            if ("success".equals(flyLogResponse.getStatus())) {
                flyLogBySuccessService.getFlyLog(inspection, flyLogResponse);
            } else if ("normal".equals(flyLogResponse.getStatus())) {
                flyLogByNormalService.getFlyLog(inspection, flyLogResponse);
            } else if ("error".equals(flyLogResponse.getStatus())) {
//            flyLogByErrorService.getFlyLog(inspection, flyLogResponse);
            } else {
                log.info("status类型不匹配");
            }
        }
    }

}
