package com.base.plan.service;


import com.base.api.client.SatelliteRpcService;
import com.base.api.model.FzTaskRpcDTO;
import com.base.api.model.PayloadRpcDTO;
import com.base.api.model.SatelliteRpcDTO;
import com.base.api.model.SubPointDataRpcDTO;
import com.base.core.StripExecuteInitialize;
import com.base.core.config.StripAccessConfig;
import com.base.plan.model.entity.EphTrack;
import com.base.plan.model.entity.PayloadLMT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class EphTrackService {

    @Resource
    SatelliteRpcService satelliteRpcService;

    @Resource
    StripExecuteInitialize stripExecuteInitialize;

    @Resource
    StripAccessConfig stripAccessConfig;

    /**
     * 将可见星下点数据(jdbc获取)分星转换
     *
     * @param subPointDataRpcDTOS 可见星下点数据
     * @param fzTaskInfo          对应观测任务
     * @return List<EphTrack>星下点 轨迹实体类集合
     */
    public EphTrack convertEphTrack(List<SubPointDataRpcDTO> subPointDataRpcDTOS, PayloadRpcDTO payloadRpcDTO, FzTaskRpcDTO fzTaskInfo) throws Exception {
        log.debug("debug", "begin to EphTrackService.convertEphTrack");
        double alt = subPointDataRpcDTOS.get(0).getAlt();
        // 最大海拔限制
//        String maxalt = Parameters.getInstance().getCoufigur().getPPAS_PARA().get("MaxAlt").toString();
        String maxalt =stripAccessConfig.getMaxAlt();
        double max_alt = Double.parseDouble(maxalt);
        if (alt > max_alt) {
            log.error("error", "eph altitude out of limitation! altitude is :" + alt, fzTaskInfo.getReqId());
            // System.out.println("task type error!");
            throw new Exception("eph altitude out of limitation! altitude is :" + alt);
        }
        EphTrack eph_i = new EphTrack();
        eph_i.setSubPointlist(subPointDataRpcDTOS);
        eph_i.setSatalliteid(String.valueOf(subPointDataRpcDTOS.get(0).getSatelliteId()));
        eph_i.setSatallitCode(payloadRpcDTO.getSatelliteCode());
        eph_i.setUpdown(String.valueOf(subPointDataRpcDTOS.get(0).getUpdown()));
        eph_i.setPayloadLMT(parsePayloadLMT(eph_i, payloadRpcDTO));
        return eph_i;
    }

    public List<EphTrack> convertEphTracknew(List<SubPointDataRpcDTO> subPointDataRpcDTOS, PayloadRpcDTO payloadRpcDTO) throws Exception {
        List<EphTrack> ephdatas = new ArrayList<>();
        double alt = subPointDataRpcDTOS.get(0).getAlt();
        // 最大海拔限制
//        String maxalt = Parameters.getInstance().getCoufigur().getPPAS_PARA().get("MaxAlt").toString();
        String maxalt =stripAccessConfig.getMaxAlt();
        double max_alt = Double.parseDouble(maxalt);
        if (alt > max_alt) {
            log.error("error", "eph altitude out of limitation! altitude is :" + alt);
            // System.out.println("task type error!");
            throw new Exception("eph altitude out of limitation! altitude is :" + alt);
        }
        int k = 0;
        EphTrack eph_0 = new EphTrack();
        eph_0.addSubPoint(subPointDataRpcDTOS.get(0));
        eph_0.setSatalliteid(String.valueOf(subPointDataRpcDTOS.get(0).getSatelliteId()));
        eph_0.setSatallitCode(payloadRpcDTO.getSatelliteCode());
        eph_0.setUpdown(String.valueOf(subPointDataRpcDTOS.get(0).getUpdown()));
        eph_0.setPayloadLMT(parsePayloadLMT(eph_0, payloadRpcDTO));
        ephdatas.add(eph_0);

        PayloadLMT lastPayloadLMT = eph_0.getPayloadLMT();
        String lastSatID = eph_0.getSatalliteid();

        for (int i = 1; i < subPointDataRpcDTOS.size(); i++) {

            Date temp1 = subPointDataRpcDTOS.get(i - 1).getDate();
            Date temp2 = subPointDataRpcDTOS.get(i).getDate();
            String sat1 = subPointDataRpcDTOS.get(i - 1).getSatelliteId();
            String sat2 = subPointDataRpcDTOS.get(i).getSatelliteId();
            long diff = temp2.getTime() - temp1.getTime();

            if (diff == 1000 && sat1.equalsIgnoreCase(sat2)) {
                ephdatas.get(k).addSubPoint(subPointDataRpcDTOS.get(i));
            } else {
                EphTrack eph_i = new EphTrack();
                eph_i.addSubPoint(subPointDataRpcDTOS.get(i));
                eph_i.setSatalliteid(subPointDataRpcDTOS.get(i).getSatelliteId());
                eph_i.setUpdown(subPointDataRpcDTOS.get(i).getUpdown());
                eph_i.setSatallitCode(payloadRpcDTO.getSatelliteCode());
                if (!eph_i.getSatalliteid().equalsIgnoreCase(lastSatID)) {
                    lastPayloadLMT = parsePayloadLMT(eph_i, payloadRpcDTO);
                    lastSatID = eph_i.getSatalliteid();
                }
                eph_i.setPayloadLMT(lastPayloadLMT);

                ephdatas.add(eph_i);
                k++;

            }
        }

        return ephdatas;
    }


    private PayloadLMT parsePayloadLMT(EphTrack eph_0, PayloadRpcDTO payloadRpcDTO) throws Exception {
        PayloadLMT payloadLMT = new PayloadLMT();
        /*
         * 设置卫星标识
         */
        payloadLMT.setSatellite(eph_0.getSatalliteid());

        List<String> strings = new ArrayList<>();
        strings.add(payloadRpcDTO.getSatelliteCode());
        SatelliteRpcDTO satelliteRpcDTO = satelliteRpcService.listByIds(strings).get(0);

        float minSunEle=-90;
        float maxSunEle=90;
        if (payloadRpcDTO.getMinSunElevation()!=null){
            float tempMinSunEle=Float.parseFloat(payloadRpcDTO.getMinSunElevation());
            if (tempMinSunEle>minSunEle) {
                minSunEle=tempMinSunEle;
            }
        }
        if (payloadRpcDTO.getMaxSunElevation()!=null){
            float tempMaxSunEle=Float.parseFloat(payloadRpcDTO.getMaxSunElevation());
            if (tempMaxSunEle<maxSunEle) {
                maxSunEle=tempMaxSunEle;
            }
        }
        /**
         * PPAS中从通过任务号和卫星编号获取卫星需求匹配表中适配载荷,当前只存在一个载荷
         * 设置最大幅宽maxswith, maxswithpayload对应的载荷id
         */
        payloadLMT.setMaxInfoBasisPayload(payloadRpcDTO);

        List<PayloadRpcDTO> payloadRpcDTOS=new ArrayList<>();
        payloadRpcDTOS.add(payloadRpcDTO);
        payloadLMT.setPayloads(payloadRpcDTOS);

        payloadLMT.setMax_sunangle(maxSunEle);
        payloadLMT.setMin_sunangle(minSunEle);
        payloadLMT.setMax_rollangle(Float.parseFloat(satelliteRpcDTO.getMaxRollAngle()));
        payloadLMT.setLmt_rollangle(Float.parseFloat(satelliteRpcDTO.getMaxRollAngle()));

        /**
         ** 设置观测时长,将任务中的观测时长限制设置为最小观测时长 如果任务为点目标，观测时长限制为最大观测时长
         */
//        float max_duration = Float.parseFloat(Parameters.getInstance().getCoufigur().getPPAS_PARA().get("max_duration").toString());
        float max_duration =Float.parseFloat(stripAccessConfig.getMaxDuration());
        if (payloadRpcDTO.getPayloadType().equalsIgnoreCase("ELECTRON")) {
//            max_duration = Float.parseFloat(Parameters.getInstance().getCoufigur().getPPAS_PARA().get("max_ele_duration").toString());
            max_duration = 1800f;
        }
        /**
         * 最小观测时长
         */
//        float min_duration = Float.parseFloat(Parameters.getInstance().getCoufigur().getPPAS_PARA().get("min_duration").toString());
        float min_duration =Float.parseFloat(stripAccessConfig.getMinDuration());
        if (payloadRpcDTO.getPayloadType().equalsIgnoreCase("ELECTRON")) {
//            min_duration = Float.parseFloat(Parameters.getInstance().getCoufigur().getPPAS_PARA().get("min_ele_duration").toString());
            min_duration = 600f;
        }

        // 目前的最小最大时长只从配置文件中取
        payloadLMT.setMax_duration(max_duration);
        payloadLMT.setMin_duration(min_duration);

        return payloadLMT;
    }
}
