package com.base.core.handler;

import com.base.api.model.PayloadRpcDTO;
import com.base.core.BizService;
import com.base.core.model.enity.*;
import com.base.core.model.enums.StripAccessTypeEnums;
import com.base.core.operator.AccessWindowService;
import com.base.core.operator.StripRollCalc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@MappingCore(stripAccessTypeEnums =  StripAccessTypeEnums.POINT_SAR)
public class SarPointStripAccessHandler implements StripExecuteHandler<ImageAccessData>{

    @Resource
    BizService bizService;

    @Resource
    AccessWindowService accessWindowService;

    private StripRollCalc striprollcalc = new StripRollCalc();

    final static double DEFAULT_POINT_DISTANCE = 1;


    @Override
    public List<ImageAccessData> stripExecute(Region region, EphTrackCoreDTO ephTrackCoreDTO, PayloadRpcDTO payloadRpcDTO) throws Exception {
        //查询星历
        Date startTime = ephTrackCoreDTO.getSubPointlist().get(0).getDate();
        Date endTime = ephTrackCoreDTO.getSubPointlist().get(ephTrackCoreDTO.getSubPointlist().size() - 1).getDate();
        List<Eph> ephList = bizService.getBizEphList(ephTrackCoreDTO.getSatallitCode(), startTime, endTime);
        /*
         * 2.将region转换为SphericalPoint
         */
        List<SphericalPoint> pointInArea = regionToListSphericalPoint(region);

        /*
         * 3.计算雷达卫星访问信息
         */
        List<ImageAccessData> imageAccessDatas  = accessWindowService.radarCompute(ephTrackCoreDTO.getSatallitCode(),ephList,pointInArea);

        List<ImageAccessData> ImageAccessDatasInMsec = new ArrayList<>();
        Map<Double,List<ImageAccessData>> imageAccessDataMap = imageAccessDatas.stream().collect(Collectors.groupingBy(ImageAccessData::getRollAngle));
        imageAccessDataMap.forEach((key,value)->{
            Map<Date,List<ImageAccessData>> dateListMap=value.stream().collect(Collectors.groupingBy(ImageAccessData::getAccessTime));
            dateListMap.forEach((key1,value1)->{
                ImageAccessData imageAccessData=value1.get(0);
                imageAccessData.setEphList(ephList);
                imageAccessData.setPointInArea(pointInArea);
                ImageAccessDatasInMsec.add(imageAccessData);
            });
        });
        if (CollectionUtils.isEmpty(ImageAccessDatasInMsec)){
            log.info("sar access compute res is empty!");
            return new ArrayList<>();
            /*throw new RuntimeException("sar access compute res is empty!");*/
        }
        for (int index = 1; index < ImageAccessDatasInMsec.size(); index++) {
            Date accessTime = ImageAccessDatasInMsec.get(0).getAccessTime();
            ImageAccessDatasInMsec.get(index).setAccessTime(accessTime);
        }
        return ImageAccessDatasInMsec;
    }

    @Override
    public StripDTO formatStripInstance(ImageAccessData imageAccessData) {
        StripDTO stripDTO = new StripDTO();
        stripDTO.setRollAngle(imageAccessData.getRollAngle());
        stripDTO.setAccessTime(imageAccessData.getAccessTime());
        stripDTO.setAccessTimeend(imageAccessData.getAccessTimeend());
        stripDTO.setEphList(imageAccessData.getEphList());
        stripDTO.setPointInArea(imageAccessData.getPointInArea());
        return stripDTO;
    }

    private List<SphericalPoint> regionToListSphericalPoint(Region region) {
        List<SphericalPoint> pointInArea = new ArrayList<>();
        SphericalPoint sphericalPoint = new SphericalPoint();
        sphericalPoint.setLat(region.getCenNode().getLat());
        sphericalPoint.setLon(region.getCenNode().getLon());
        pointInArea.add(sphericalPoint);

        return pointInArea;
    }
}