package com.base.plan.deal.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.base.api.client.EphRpcService;
import com.base.api.client.FzTaskRpcService;
import com.base.api.model.CalEphRpcDTO;
import com.base.api.model.FzTaskRpcDTO;
import com.base.api.model.request.FzTaskInfoRpcReqDTO;
import com.base.orbit.utils.DateUtils;
import com.base.plan.constant.Constants;
import com.base.plan.deal.NuaaFileHandler;
import com.base.plan.mapper.StripMapper;
import com.base.plan.model.dto.PlanBizInfo;
import com.base.plan.model.entity.StripInfo;
import com.base.task.model.BaseBizInfo;
import com.base.task.model.Task;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Service
@Slf4j
public class StripNuaaFileHandler implements NuaaFileHandler {

    @Resource
    StripMapper stripMapper;
    @Resource
    FzTaskRpcService fzTaskRpcService;
    @Resource
    EphRpcService ephRpcService;


    @Override
    public void saveData(String stripStr, String stripId, String rollAngle, Task task) {
        if (!StringUtils.isBlank(stripStr)) {
            //  添加条带信息
            StripInfo stripInfo = new StripInfo();
            String[] timeSplit = stripStr.split(Constants.BLANK_SPACE);
            Date observationStartTime = DateUtils.string2LocalDate(timeSplit[1] + " " + timeSplit[2], DateUtils.YYYY_MM_DD_HH_MM_SS);
            Date observationEndTime = DateUtils.string2LocalDate(timeSplit[3] + " " + timeSplit[4], DateUtils.YYYY_MM_DD_HH_MM_SS);
            stripInfo.setId(stripId);
            stripInfo.setObservationStartTime(observationStartTime);
            stripInfo.setObservationEndTime(observationEndTime);
            stripInfo.setRollAngle(rollAngle.substring(0, rollAngle.length() - 1));

            //  通过任务中获取
            PlanBizInfo planBizInfo = (PlanBizInfo) task.getBaseBizInfo();
            stripInfo.setWorkMode(planBizInfo.getWorkMode());
            stripInfo.setReqId(planBizInfo.getParentId());
            stripInfo.setPayloadId(planBizInfo.getPayloadId());
            stripInfo.setSatelliteId(planBizInfo.getSatelliteId());
            stripInfo.setSatelliteCode(task.getSatelliteCode());

            FzTaskInfoRpcReqDTO fzTaskInfoRpcReqDTO = new FzTaskInfoRpcReqDTO();
            fzTaskInfoRpcReqDTO.setReqIds(Lists.newArrayList(planBizInfo.getParentId()));
            FzTaskRpcDTO fzTaskInfo = fzTaskRpcService.getFzTaskDTOList(fzTaskInfoRpcReqDTO).get(0);
            stripInfo.setTargetName(fzTaskInfo.getTargetName());
            stripInfo.setPriority(fzTaskInfo.getPriority());

            //  存在条带的时间区间内，一定存在星下点，此处改为根据星下点获取圈号
            String start= DateUtils.DateToString(stripInfo.getObservationStartTime());
            String end= DateUtils.DateToString(stripInfo.getObservationEndTime());
            List<CalEphRpcDTO> dtos = ephRpcService.selEphDataByFile(stripInfo.getSatelliteCode(), start,end);
           if (!CollectionUtils.isEmpty(dtos)){
               String cirCle = dtos.get(0).getCirCle();
               stripInfo.setCircle(cirCle);
           }
            try {
                stripMapper.insert(stripInfo);
            } catch (Exception e) {
                log.error("~~~~~~~~~~~~~~~insert strip info fail msg is {}", e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
