package com.base.core.handler;

import com.base.api.model.PayloadRpcDTO;
import com.base.core.config.StripAccessConfig;
import com.base.core.model.enity.*;
import com.base.core.model.enums.StripAccessTypeEnums;
import com.base.core.operator.StripRollCalc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@MappingCore(stripAccessTypeEnums = StripAccessTypeEnums.POINT_CCD_COMMON)
@Service
@Slf4j
public class CcdPointStripHandler implements StripExecuteHandler<CcdPoint>{

    @Resource
    private StripRollCalc striprollcalc;

    @Resource
    StripAccessConfig stripAccessConfig;

    @Override
    public List<CcdPoint> stripExecute(Region region, EphTrackCoreDTO ephTrackCoreDTO, PayloadRpcDTO payloadRpcDTO) throws Exception {
        log.debug("debug", "begin to PointstripServiceImpl.execute", "");
        CcdPoint ccdPoint=new CcdPoint();
        String payloadType = payloadRpcDTO.getPayloadType();
        SubPoint nearPoint= striprollcalc.getNearPoint(region.getCenNode(),ephTrackCoreDTO);
        // 计算侧摆角
        double rollangle = striprollcalc.calPointRollangle(nearPoint,region.getCenNode(), ephTrackCoreDTO, payloadType);
        //先了解一下小机的卫星是否有这样的处理，再进行修改--zhanghy
        // 侧摆角超过最大限制
        if (Math.abs(rollangle) > ephTrackCoreDTO.getPayloadLMT().getLmt_rollangle()) {


                double angle = Double.parseDouble(payloadRpcDTO.getFox()) / 2;
                double maxSideAngleLmt = ephTrackCoreDTO.getPayloadLMT().getLmt_rollangle();
                double fy = Math.abs(rollangle) - angle;// 侧摆角-视场角
                if (fy < maxSideAngleLmt) {
                    if (rollangle < 0) {
                        rollangle = 0 - maxSideAngleLmt;
                    } else {
                        rollangle = maxSideAngleLmt;
                    }
                } else {
                    System.out.println("Can't calculate sideAngle");
                    return null;
                }

        }
        List<Double> rollanglelist = new ArrayList<>();
        rollanglelist.add(rollangle);
        ccdPoint.setRollAngleList(rollanglelist);
        ccdPoint.setNearPoint(nearPoint);
        List<CcdPoint> ccdPoints=new ArrayList<>();
        ccdPoints.add(ccdPoint);
        return ccdPoints;
    }

    @Override
    public StripDTO formatStripInstance(CcdPoint ccdPoint) {
        return null;
    }
}
