package com.base.plan;


import com.base.api.client.EphRpcService;
import com.base.api.client.FzTaskRpcService;
import com.base.api.client.PayloadRpcService;
import com.base.api.client.PointRpcService;
import com.base.api.model.CalEphRpcDTO;
import com.base.api.model.FzTaskRpcDTO;
import com.base.api.model.PointRpcDTO;
import com.base.api.model.request.FzTaskInfoRpcReqDTO;
import com.base.api.model.request.PayloadRpcReqDTO;
import com.base.api.model.request.PointListRpcReqDTO;
import com.base.orbit.utils.DateUtils;
import com.base.plan.constant.Constants;
import com.base.plan.deal.impl.PointNuaaFileHandler;
import com.base.plan.deal.impl.StripNuaaFileHandler;
import com.base.plan.mapper.StripMapper;
import com.base.plan.model.dto.PointDTO;
import com.base.plan.service.ArcService;
import com.base.plan.task.handler.StripTaskHandler;
import com.base.task.model.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.ParseException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class PlanApplicationTest {

    @Resource
    StripMapper stripMapper;
    @Resource
    PointRpcService pointRpcService;
    @Resource
    ArcService arcService;
    @Resource
    StripNuaaFileHandler stripNuaaFileTool;
    @Resource
    StripTaskHandler stripTaskHandler;
    @Resource
    FzTaskRpcService fzTaskRpcService;
    @Resource
    PayloadRpcService payloadRpcService;
    @Resource
    EphRpcService ephRpcService;
    @Resource
    PointNuaaFileHandler pointNuaaFileHandler;

    @Test
    public void point(){
        List<String> parentIds=new ArrayList<>();
        parentIds.add("2360584077808448");
        PointListRpcReqDTO req=new PointListRpcReqDTO();
        req.setParentIds(parentIds);
        List<PointRpcDTO> pointRpcDTOS=pointRpcService.getPointDTOList(req);
        pointRpcDTOS.sort(Comparator.comparing(PointRpcDTO::getSerial));
        List<String> girds = pointNuaaFileHandler.totalGird(pointRpcDTOS);


        PointListRpcReqDTO pointReq=new PointListRpcReqDTO();
        List<String> pointIds = new ArrayList<>();
        pointIds.add("1995");

        pointReq.setParentIds(pointIds);
        List<PointRpcDTO> pointDTOList = pointRpcService.getPointDTOList(pointReq);

        Double aDouble = pointNuaaFileHandler.coverageRate(girds, pointDTOList);
        System.out.println("~~~~~~~~~~~~~"+aDouble);

    }

    @Test
    public void sort(){
        List<PointRpcDTO> pointRpcDTOS=new ArrayList<>();
        PointRpcDTO pointRpcDTO1=new PointRpcDTO();
        PointRpcDTO pointRpcDTO2=new PointRpcDTO();
        PointRpcDTO pointRpcDTO3=new PointRpcDTO();
        PointRpcDTO pointRpcDTO4=new PointRpcDTO();

        pointRpcDTO1.setLongitude("91.3494729225677");
        pointRpcDTO1.setLatitude("57.8096520762106");

        pointRpcDTO2.setLongitude("66.8881639080354");
        pointRpcDTO2.setLatitude("77.5005329184002");

        pointRpcDTO3.setLongitude("105.46760666446");
        pointRpcDTO3.setLatitude("69.2045054651617");

        pointRpcDTO4.setLongitude("25.8484602410675");
        pointRpcDTO4.setLatitude("88.886502064902");

        pointRpcDTOS.add(pointRpcDTO1);
        pointRpcDTOS.add(pointRpcDTO2);
        pointRpcDTOS.add(pointRpcDTO3);
        pointRpcDTOS.add(pointRpcDTO4);
        List<PointRpcDTO>  pointRpcDTOS1= pointNuaaFileHandler.sort(pointRpcDTOS);
        System.out.println(pointRpcDTOS1);
    }

    @Test
    public void sel(){
        List<CalEphRpcDTO> dtos = ephRpcService.selEphDataByFile("QWERT17_1", "2023-02-26 13:40:00", "2023-02-26 13:42:00");
        System.out.println(dtos);

    }

    @Test
    public void time(){
        long time = new Date().getTime();

        Long aLong = DateUtils.DateToTimeStamp("2023-03-07 11:59:59");

        Long aa = (time+aLong)/2;

        System.out.println(new Date(aa));

    }

    @Test
    public void save() {
        Task task = new Task();
        task.setResultDataSavePath("D:\\text\\111");
        try {
            stripTaskHandler.taskExecute(task);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void up() {
        File file = new File("D:\\soft\\MyAppInstaller_mcr\\application\\input.txt");
        try {
            long count = Files.lines(file.toPath()).count();
            InputStreamReader read = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(read);

            List<String> lonlist = new ArrayList<>();
            List<String> latlist = new ArrayList<>();

            String line = null;

            for (int i = 0; i < count + 1; i++) {
                line = br.readLine();
                if (i == 1) {
                    lonlist.addAll(Arrays.asList(line.split(Constants.BLANK_SPACE)));
                }
                if (i == 2) {
                    latlist.addAll(Arrays.asList(line.split(Constants.BLANK_SPACE)));
                }
            }

            System.out.println(latlist);
            System.out.println(lonlist);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void selpayLoads() {
        String line = "[114 114.002918004124 114.316811354585 114][14.0034126673886 13.9999999999996 16.0000000000005 14]";
        Pattern r = Pattern.compile(Constants.PATTERN);

        Matcher matcher = r.matcher(line);
        List str = new ArrayList();

        while (matcher.find()) {
            str.add(matcher.group());
        }

        String lons = String.valueOf(str.get(0));
        String lats = String.valueOf(str.get(1));

        //  按照空格分隔取数值存入集合
        List<String> lonlist = Arrays.asList(lons.substring(1, lons.length() - 1).split(Constants.BLANK_SPACE));
        List<String> latlist = Arrays.asList(lats.substring(1, lats.length() - 1).split(Constants.BLANK_SPACE));
        List<PointDTO> pointInfos = new ArrayList<>();

        if (latlist.size() == Constants.POINT_NUM && lonlist.size() == Constants.POINT_NUM) {
            for (int i = 0; i < Constants.POINT_NUM; i++) {
                PointDTO pointDTO = new PointDTO();
                pointDTO.setLatitude(latlist.get(i));
                pointDTO.setLongitude(lonlist.get(i));
                pointInfos.add(pointDTO);
            }
        }
        List<PointDTO> pointDTOS = dealList(pointInfos);
//        List<Arc> pointListArcList = arcService.getPointListArcList(pointDTOS);

        List<String> gridIds = new ArrayList<>();
//        pointListArcList.stream().forEach(pointListArc -> {
//                    List<String> ids = pointListArc.getGridlist().stream().map(GridInfo::getId).collect(Collectors.toList());
//                    gridIds.addAll(ids);
//                }
//        );
        String gridIdStr = org.apache.commons.lang3.StringUtils.join(gridIds, ",");
        System.out.println(gridIdStr);

    }


    /**
     * 将原集合进行重新排序
     *
     * @param pointInfos 原数组
     */
    public List<PointDTO> dealList(List<PointDTO> pointInfos) {
        //  根据经度最小值排序，默认经度最小值为最左侧点
        List<PointDTO> sortPointInfos = pointInfos.stream().sorted
                (Comparator.comparing(PointDTO::getLongitude)).limit(Constants.POINT_NUM).collect(Collectors.toList());

        for (int i = 0; i < Constants.POINT_NUM; i++) {
            PointDTO pointInfo = sortPointInfos.get(i);
            pointInfo.setSerial(String.valueOf(i));
        }

        return sortPointInfos;
    }

}
