package com.base.plan.service;

import com.base.api.client.EphRpcService;
import com.base.api.client.PayloadRpcService;
import com.base.api.client.SatelliteRpcService;
import com.base.api.client.SubPointDataRpcService;
import com.base.api.model.CalEphRpcDTO;
import com.base.api.model.SatelliteRpcDTO;
import com.base.api.model.SubPointDataRpcDTO;
import com.base.core.BizService;
import com.base.core.model.enity.Eph;
import com.base.core.config.ImageLimitation;
import com.base.core.model.enity.SubPoint;
import com.base.orbit.utils.BizUtils;
import com.base.orbit.utils.DateUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
public class BizServiceImpl implements BizService {
    @Resource
    SubPointDataRpcService subPointDataRpcService;

    @Resource
    SatelliteRpcService satelliteRpcService;

    @Resource
    PayloadRpcService payloadRpcService;

    @Resource
    EphRpcService ephRpcService;
    @Override
    public List<Eph> getBizEphList(String code, Date startTime, Date endTime) {
        String start= DateUtils.DateToString(startTime);
        String end= DateUtils.DateToString(endTime);
        List<CalEphRpcDTO> calEphRpcDTOS=ephRpcService.selEphDataByFile(code,start,end);
        List<SubPointDataRpcDTO> satelliteSubPointDataList = subPointDataRpcService.getSubPointDataListForTaskUpload(code, start, end);
        if (calEphRpcDTOS.size()!=satelliteSubPointDataList.size()){
            return null;
        }
        List<Eph> ephList= BizUtils.copyEntityToDto(Eph.class, calEphRpcDTOS);
        for (int i = 0;i<ephList.size();i++){
            Eph eph = ephList.get(i);
            SubPointDataRpcDTO subPointDataRpcDTO = satelliteSubPointDataList.get(i);
            eph.setLat(subPointDataRpcDTO.getLat());
            eph.setLon(subPointDataRpcDTO.getLon());
            eph.setAlt(subPointDataRpcDTO.getAlt());
        }
        return ephList;
    }

    @Override
    public List<SubPoint> getBizSubPointList(String code, Date startTime, Date endTime,String updown) {
        String start= DateUtils.DateToString(startTime);
        String end= DateUtils.DateToString(endTime);
        List<SubPointDataRpcDTO> satelliteSubPointDataList = subPointDataRpcService.getSubPointDataList(code, start, end,updown);
        return BizUtils.copyEntityToDto(SubPoint.class, satelliteSubPointDataList);
    }

    @Override
    public List<SubPoint> getBizSubPointList(String code, Date startTime, Date endTime) {
        String start= DateUtils.DateToString(startTime);
        String end= DateUtils.DateToString(endTime);
        List<SubPointDataRpcDTO> satelliteSubPointDataList = subPointDataRpcService.getSubPointDataListForTaskUpload(code, start, end);
        return BizUtils.copyEntityToDto(SubPoint.class, satelliteSubPointDataList);
    }

    @Override
    public void getImageLimitation(String code, ImageLimitation imageLimitation) {
        List<SatelliteRpcDTO> satelliteRpcDTOList = satelliteRpcService.listByIds(Lists.newArrayList(code));
        if (CollectionUtils.isEmpty(satelliteRpcDTOList)){
            return;
        }
        if (!CollectionUtils.isEmpty(satelliteRpcDTOList)){
            SatelliteRpcDTO satelliteRpcDTO = satelliteRpcDTOList.get(0);
            String satelliteCode = satelliteRpcDTO.getSatelliteCode();
            String loadType = satelliteRpcDTO.getLoadType();
            String altitude = satelliteRpcDTO.getAltitude();
            String maxRollAngle = satelliteRpcDTO.getMaxRollAngle();
            String minRollAngle = satelliteRpcDTO.getMinRollAngle();

            imageLimitation.setMaxRoll(Double.parseDouble(maxRollAngle));
            imageLimitation.setMinRoll(Double.parseDouble(minRollAngle));
            imageLimitation.setSatName(satelliteCode);
            imageLimitation.setAltitude(Double.parseDouble(altitude));
            imageLimitation.setPayloadType(loadType);
            imageLimitation.setMaxDistance((long) (Math.tan((Double.parseDouble(maxRollAngle) + Double.parseDouble(maxRollAngle) / 10.0) * Math.PI / 180.0) * Double.parseDouble(altitude)));
            // 单位：公里
            imageLimitation.setMaxDistance(
                    Math.sqrt(Math.pow(imageLimitation.getMaxDistance(), 2) + Math.pow(imageLimitation.getStepLength() * 8.0, 2)));
            // // 单位：弧度
            imageLimitation.setMaxDistance((imageLimitation.getMaxDistance() / 111.0) * Math.PI / 180.0);
        }

    }
}
