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 com.base.core.source.SideAngleSurfaceGrid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

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

    @Resource
    BizService bizService;

    @Resource
    AccessWindowService accessWindowService;

    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);
        List<SphericalPoint> pointInArea = accessWindowService.convertAreaToPoints(accessWindowService.regionToListSphericalPoint(region), DEFAULT_POINT_DISTANCE);
        //访问计算
        List<ImageAccessData> imageAccessDataList = accessWindowService.radarCompute(ephTrackCoreDTO.getSatallitCode(),ephList, pointInArea);
        List<ImageAccessData> imageAccessDataListnew=new ArrayList<>();
        Map<Double,List<ImageAccessData>> imageAccessDataMap=imageAccessDataList.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);
                imageAccessDataListnew.add(imageAccessData);
            });
        });
       if (CollectionUtils.isEmpty(imageAccessDataListnew)){
           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 < imageAccessDataListnew.size(); index++) {
            Date accessTime = imageAccessDataListnew.get(0).getAccessTime();
            imageAccessDataListnew.get(index).setAccessTime(accessTime);
        }
        return imageAccessDataListnew;
    }

    @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;
    }
}