package com.base.biz.service;

import com.base.api.client.AntennaRpcService;
import com.base.api.model.AntennaRpcDTO;
import com.base.biz.model.dto.request.LinkCountDTO;
import com.base.biz.model.entity.SatelliteInfo;
import com.base.biz.model.entity.StationInfo;
import com.base.orbit.utils.LinkCount;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class LinkCountService {
    @Autowired
    private SatelliteService satelliteService;
    @Autowired
    private StationService stationService;
    @Resource
    private AntennaService antennaService;
    @Resource
    private LinkCount linkCount;


    public Double linkCount(LinkCountDTO req) {
        SatelliteInfo satelliteInfo = satelliteService.getSatelliteInfoByCode(req.getSatelliteCode());
        StationInfo stationInfo = stationService.getStationInfoById(req.getStationId());
        AntennaRpcDTO antennaRpcDTO = new AntennaRpcDTO();
        antennaRpcDTO.setDmzdh(stationInfo.getStationCode());
        antennaRpcDTO.setTxkj(req.getAntenna());
        List<AntennaRpcDTO> antennaRpcDTOs = antennaService.getInfoList(antennaRpcDTO);
        Optional<AntennaRpcDTO> antennaRpcDTO1 = antennaRpcDTOs.stream().findFirst();
        if (antennaRpcDTO1.isPresent()) {
            /*卫星发射EIRP */
            double eirp = Double.parseDouble(satelliteInfo.getEirp());
            //  载波频率作为卫星属性由平台传入
//            double f = req.getFrequencyEnums().getValue();
            double f = Double.parseDouble(satelliteInfo.getCarrier());
            double h = Double.parseDouble(satelliteInfo.getAltitude());
            double l1 = req.getL1();
            double l2 = req.getL2();
            double l3 = req.getL3();
            double l4 = req.getL4();
            double elevation = req.getElevation();

            /*地面站 G/T*/
            double GT = Double.parseDouble(antennaRpcDTO1.get().getDmzGT());
            /*信息速率*/
            double fb = Double.parseDouble(satelliteInfo.getXxsl());
            return linkCount.linkMargin(eirp, f, h, l1, l2, l3, l4, GT, fb, elevation);
        }
        return null;
    }
}
