package com.base.track.utils;

import com.base.orbit.utils.DateUtils;
import com.base.orbit.utils.DistanceCount;
import com.base.orbit.utils.LinkCount;
import com.base.track.model.dto.request.VisualRequestDTO;
import com.base.track.model.dto.response.ElevationAndDateDTO;
import com.base.track.model.entity.VisualData;
import org.springframework.stereotype.Component;

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


@Component
public class VisualAlgorithmFacde {

    @Resource
    private LinkCount linkCount;    //  余量链路
    @Resource
    private DistanceCount distanceCount;    //  距离
    @Resource
    private ElevationCount elevationCount;  //  仰角


    //  调用子系统对象的业务方法
    public List<VisualData> visualAlg(VisualRequestDTO visualRequestDTO) {
        List<VisualData> visualDataList = new ArrayList<>();
        ElevationAndDateDTO elevationAndDateDTO = elevationCount.elevationMargin(visualRequestDTO);
        List<Double> elevations = elevationAndDateDTO.getElevations();
        List<Date> dates = elevationAndDateDTO.getDates();

        /*地面站 G/T*/
        double gtVal = 0.0;
        if (visualRequestDTO.getAntenna() == 12) {
            gtVal = 35.0;
        } else if (visualRequestDTO.getAntenna() == 5.2) {
            gtVal = 28.5;
        }
        //  余量
        List<Double> links = new LinkedList<>();
        for (Double elevation : elevationAndDateDTO.getElevations()) {
            Double link = linkCount.linkMargin(visualRequestDTO.getEirp(), visualRequestDTO.getCarrier(), visualRequestDTO.getH(),
                    visualRequestDTO.getL1(), visualRequestDTO.getL2(), visualRequestDTO.getL3(), visualRequestDTO.getL4(), gtVal, visualRequestDTO.getFb(),
                    elevation
            );
            links.add(link);
        }

        //  距离
        List<Double> dinstances = distanceCount.distanceVal(elevations, visualRequestDTO.getH());

        for (int i = 0; i < elevations.size(); i++) {
            VisualData visualData = new VisualData();
            visualData.setGradientAngle(elevations.get(i).toString());
            visualData.setAllowance(links.get(i).toString());
            visualData.setHappenTime(DateUtils.DateToString(dates.get(i)));
            visualData.setDistance(dinstances.get(i).toString());
            visualData.setTrbutDataId(visualRequestDTO.getTrbutDataId());
            visualDataList.add(visualData);
        }
        return visualDataList;
    }
}
