package com.base.track.controller.rpc;

import com.base.orbit.utils.DateUtils;
import com.base.orbit.web.Result;
import com.base.task.cdlatch.aop.model.InstanceAop;
import com.base.task.cdlatch.aop.model.SubPointAop;
import com.base.track.model.dto.request.AstronomyRequestDTO;
import com.base.track.model.dto.request.TrbutRequestDTO;
import com.base.track.service.CalService;
import com.base.track.service.TrbutDataService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/calc")
public class CalRpcController {

    @Resource
    private CalService calcService;

    @Resource
    TrbutDataService trbutDataService;

    @RequestMapping(value = "/calcEph")
    public Result<String> calcEph(@RequestParam("satelliteCodes") List<String> satelliteCodes, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) {
        SubPointAop subPointAop = new SubPointAop();
        subPointAop.setSatelliteCodes(satelliteCodes);
        subPointAop.setStartTime(startTime);
        subPointAop.setEndTime(endTime);
        calcService.calcEph(subPointAop);
        return Result.success("星历计算成功加入列队");
    }

    /**
     * 计算星历星下点
     * @param satelliteCodes
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/calcSubPoint")
    public Result<String> calcSubPoint(@RequestParam("satelliteCodes") List<String> satelliteCodes, @RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) {
        SubPointAop subPointAop = new SubPointAop();
        subPointAop.setSatelliteCodes(satelliteCodes);
        subPointAop.setStartTime(startTime);
        subPointAop.setEndTime(endTime);
        calcService.calcSubPoint(subPointAop);
        return Result.success("星历星下点计算成功加入列队");
    }

    @RequestMapping(value = "/calcStation")
    public Result<String> calcStation(@RequestParam("satelliteIdList") List<String> satelliteIdList,@RequestParam("stationIdList") List<String> stationIdList,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) {
        SubPointAop subPointAop = new SubPointAop();
        subPointAop.setStationIds(stationIdList);
        subPointAop.setSatelliteCodes(satelliteIdList);
        subPointAop.setStartTime(startTime);
        subPointAop.setEndTime(endTime);

        for (String stationId:stationIdList) {
            List<String> station = new ArrayList<>();
            station.add(stationId);
            BeanUtils.copyProperties(subPointAop,SubPointAop.class);
            subPointAop.setStationIds(station);
            calcService.calcTrbut(subPointAop);
        }
        return Result.success("地面站计算成功加入列队");
    }

    @PostMapping(value = "/calcAstronomy")
    public Result<String> calcAstronomy(@RequestBody AstronomyRequestDTO astronomyRequest) {
        calcService.calcAstronomy(astronomyRequest.getSatelliteIds(), astronomyRequest.getStationIds(), astronomyRequest.getStartTime(), astronomyRequest.getEndTime());
        return Result.success("计算天文事件成功");
    }

    /**
     * 中继星进出站计算
     *
     * @param satelliteIdList 卫星代号集合
     * @param stationIdList   地面站代号集合
     * @param startTime       开始时间
     * @param endTime         结束时间
     * @return
     */
    @RequestMapping("/relayCount")
    public Result<String> calcRelaySatellite(@RequestParam("satelliteIdList") List<String> satelliteIdList,@RequestParam("stationIdList") List<String> stationIdList,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        TrbutRequestDTO trbutRequestDTO = new TrbutRequestDTO();
        trbutRequestDTO.setEndTime(DateUtils.parseDate(endTime));
        trbutRequestDTO.setStartTime(DateUtils.parseDate(startTime));
        trbutRequestDTO.setSatelliteCodes(satelliteIdList);
        trbutRequestDTO.setStationIds(stationIdList);
        trbutDataService.relayTrbutCountAndBatch(trbutRequestDTO);
        return Result.success("中继星进出站计算成功");
    }

    /**
     * 脚本调用
     * @param instanceAop
     * @return
     */
    @PostMapping(value = "/calcInstance")
    public Result<String> calcInstance(@RequestBody InstanceAop instanceAop) {
        calcService.calcInstance(instanceAop);
        return Result.success("瞬根推算成功加入列队");
    }


    /**
     * 脚本调用
     */
    @PostMapping("/fileroute")
    public void fileroute(){
        calcService.fileroute();
    }
}
