package com.keystar.plane.inspection;

import com.baomidou.mybatisplus.annotation.TableField;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import com.keystar.plane.inspection.bo.Location;
//import com.keystar.plane.inspection.config.MinIoConfig;
import com.keystar.plane.inspection.constant.LocationConstant;
import com.keystar.plane.inspection.dao.DegreesVo;
import com.keystar.plane.inspection.dao.PhotoMessageVo;
import com.keystar.plane.inspection.dao.PlanePhotoVo;
import com.keystar.plane.inspection.entity.PlanePhotoEntity;
import com.keystar.plane.inspection.mqtt.PlaneMqttHandler;
import com.keystar.plane.inspection.service.MqttService;
import com.keystar.plane.inspection.service.NestService;
import com.keystar.plane.inspection.service.PhotoService;
import com.keystar.plane.inspection.service.PlanePhotoService;
import com.keystar.plane.inspection.service.impl.MinioService;
import com.keystar.plane.inspection.utils.*;
import io.minio.messages.Item;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@SpringBootTest
class PlaneInspectionApplicationTests {

    @Autowired
    PhotoService photoService;

    @Autowired
    MqttService mqttService;

    @Autowired
    MinioService minioService;

//    @Autowired
//    MinIoConfig minIoConfig;

	@Autowired
	NestService nestService;

	@Autowired
    PlanePhotoService planePhotoService;
//
//	@Test
//	void contextLoads() {
//	}

//	@Test
//	public void test() throws IOException, JpegProcessingException {
//		PhotoMessageVo photoMessage = PhotoUtils.getPhotoMessage();
//		System.out.println(photoMessage.toString());
//	}
//
//	@Test
//	public void test1() throws IOException, JpegProcessingException {
//		PhotoUtils.getPhotoGps();
//	}
//
//	@Test
//	public void test3() throws Exception {
//
//		String encrypt = Utils.encrypt("Keystar@2022", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDrKWklonbo4ZFgwLgE63wt+sHiB4Uq0Tl6V6g2Aoj5IPRd4TL8mJlMkaJu/c3G212jhjbUjYkWn5HOnrUwrVN1q+mJGGUCED6ruTNMiAc1Oe5gbLwGIeyk4IMuZFUVwq7XdTgVfFo9cVRAM8jIWftJrCm63ZrqqU/AdBoU4FSTQIDAQAB");
//		System.out.println(encrypt);
//	}
//
	@Test
	public void testLogin() throws Exception {
		String s = PlaneMqttHandler.planeLogin();
		System.out.println(s);
	}
//
//	@Test
//	public void testGetPhoto() throws Exception {
//		PlanePhotoVo planePhoto = PlaneMqttHandler.getPlanePhoto();
//		System.out.println(planePhoto);
//	}
//
//	@Test
//	public void test4(){
//		String test = "640 pixels";
//		String[] s = test.split(" ");
//		System.out.println(s[0]);
//	}
//
//	@Test
//	public void test5() throws IOException, JpegProcessingException {
//		// 获取图片的信息
//		PhotoMessageVo photoMessage = PhotoUtils.getPhotoMessage();
//		List<Location> locations = PhotoUtils.getDegrees(photoMessage);
//		System.out.println(locations.toString());
//	}
//
//	@Test
//	public void test6() throws IOException {
//		String pvJson = FileUtils.getPVJson();
//		System.out.println(pvJson);
//	}
//
//	@Test
//	public void test7() throws IOException {
//		String pvJson = FileUtils.getPVJson();
//
//		Map<String, List> stringListMap = FileUtils.jsonTransData(pvJson);
//		System.out.println(stringListMap.toString());
//	}
//
//	@Test
//	public void test8(){
//		FileUtil.ReadFileByIO("F:\\tangyangWrj\\plane-inspection\\target\\classes\\pv.json");
//
//	}
//
//	@Test
//	public void test9(){
//		String nestStatus = nestService.getNestStatus();
//		System.out.println(nestStatus);
//	}

    @Test
    public void test10() throws FileNotFoundException {
        File file = ResourceUtils.getFile("classpath:border.json");
        FileUtil.readFZLatLng(file.getPath());
        System.out.println("");
    }

    @Test
    public void test11(){
        List<Location> locations = LocationConstant.PZLocation.get("01");
        Location location = locations.get(1);
        System.out.println(location.toString());
    }

//    @Test
//    public void test12() throws IOException, JpegProcessingException {
//	    photoService.queryPhoto();
//    }
//
//    @Test
//    public void testMqtt() throws JsonProcessingException {
//	    mqttService.sendMqtt();
//    }

    @Test
    public void getNewFile(){
        List<Item> nest = minioService.listObject("nest");
        System.out.println(nest.toString());
    }

//    @Test
//    public void getNewFile2(){
//        List<String> infraredPhoto = minioService.getInfraredPhoto();
//
//        System.out.println(infraredPhoto.toString());
//    }

    @Test
    public void getFtpFile(){
        FtpUtils.getFtpFile();
    }

    @Test
    public void ssh() throws JSchException, SftpException {
        SSHUtils.connect("root", "keystar", "10.138.6.221", 22);
        SSHUtils.execCmd("cd /data/minio/data/nest/autoUpload/photo/");
        SSHUtils.execCmd("ls -ltr");
        String lsDirectory = "/data/minio/data/nest/autoUpload/photo/";
        SSHUtils.listFiles(lsDirectory);
        SSHUtils.close();
    }

    @Test
    public void testSSH(){
        // 连接到指定的服务器
        try {
            // 1、首先远程连接ssh
            SSHRemoteCall.getInstance().sshRemoteCallLogin("10.138.6.221", "root", "keystar");
            String command = "ls -lt /data/minio/data/nest/autoUpload/photo/";
            SSHRemoteCall.getInstance().execCommand(command);

            // 7、关闭连接
            SSHRemoteCall.getInstance().closeSession();
        } catch (Exception e) {
            // 打印错误信息
            System.err.println("远程连接失败......");
            e.printStackTrace();
        }
    }


//    @Test
//    public void getNew(){
//        List<String> newPhoto = FileUtil.getNewPhoto();
//        System.out.println(newPhoto.toString());
//
//    }

    @Test
    public void sendMqtt() throws JsonProcessingException {
	    mqttService.sendPhotoFile("/nest/autoUpload/photo/2422/1111/", "0");


    }

    @Test
    public void insert(){
        PlanePhotoEntity planePhotoEntity = new PlanePhotoEntity();
        planePhotoEntity.setId(1L);
        planePhotoEntity.setMissionId(1);
        planePhotoEntity.setRecordId(1);
        planePhotoEntity.setPath("123/123");
        planePhotoEntity.setPvName("pvName");
        planePhotoEntity.setPzName("pzName");
        planePhotoEntity.setTime(new Date());
        boolean save = planePhotoService.save(planePhotoEntity);
        System.out.println(save);
    }


    @Test
    public void test() throws IOException, JpegProcessingException {
	    photoService.queryPhoto("C:\\Users\\KR0282\\Desktop\\photo\\3\\3.2\\20230217174008_DJI_20230217173108_0001_THRM.jpg");
	}

	/**
	 *  测试重合面积
	 **/
	@Test
    public void CongHe() throws IOException, JpegProcessingException {
        List<Location> location1 = LocationConstant.PZLocation.get("FZ01");
        // 获取图片中的信息
        PhotoMessageVo photoMessage = PhotoUtils.getPhotoMessage("C:\\Users\\KR0282\\Desktop\\photo\\3\\32\\1113_THRM.jpg");
        // 根据图片信息，获取图片四个角的经纬度，判断图片在哪个光伏区
        List<Location> degrees = PhotoUtils.getDegrees(photoMessage);
        List<Location> locations = LocationConstant.PVLocation.get("01").get("01HL09-3    01HL09-4");
        double v = GisCheckUtils.CoincidentArea(location1, degrees);
        double v1 = GisCheckUtils.CoincidentArea(locations, degrees);
        System.out.println(v);
        System.out.println(v1);
    }

    /**
     *  测试计算多边形面积
     **/
    @Test
    public void MianJi(){
        // 球面积计算公式
//        List<Location> pointList = LocationConstant.PZLocation.get("FZ01");
//        double v = GisCheckUtils.calculatePolygonArea(pointList);
//        // 970.2378908613696
//        // 722337.9547839954
//        System.out.println(v);
    }

    @Test
    public void MJ2(){
        List<Location> locations = LocationConstant.PVLocation.get("01").get("01HL09-3    01HL09-4");
        List<Location> sortLocation = GisCheckUtils.sortLocation(locations);
        System.out.println(sortLocation);
        // h
        double lat0 = sortLocation.get(0).getLat();
        double lat1 = sortLocation.get(1).getLat();
        double latH = lat0-lat1;
        BigDecimal meterH = new BigDecimal(latH).divide(new BigDecimal(0.00000899), 10, BigDecimal.ROUND_HALF_UP);
        // 4.6251901441m

        // w
        double lon1 = sortLocation.get(1).getLon();
        double lon2 = sortLocation.get(2).getLon();
        double lonW = lon1-lon2;
        BigDecimal meterW = new BigDecimal(lonW).divide(new BigDecimal(0.00001141), 10, BigDecimal.ROUND_HALF_UP);
        // 29.3227901828

        //s
        BigDecimal multiply = meterH.multiply(meterW);
        System.out.println(multiply);
        // 135.62348015099879734148
    }

    /**
     * 地球半径（米）
     * 6371000.0 ~ 6371009.0
     */
    private static double earthRadiusMeters = 6371006.0;

    /**
     * 米每度
     */
    private static double metersPerDegree = 2.0 * Math.PI * earthRadiusMeters / 360.0;

    /**
     * 弧度每度
     */
    private static double radiansPerDegree = Math.PI / 180.0;
    @Test
    public void MJ3() throws IOException, JpegProcessingException {
//        List<Location> pointList = LocationConstant.PVLocation.get("01").get("01HL09-3    01HL09-4");

        File file = ResourceUtils.getFile("classpath:1114_THRM.JPG");
        // 获取图片中的信息
        PhotoMessageVo photoMessage = PhotoUtils.getPhotoMessage(file.getPath());
        // 根据图片信息，获取图片四个角的经纬度，判断图片在哪个光伏区
//        List<Location> pointLs = PhotoUtils.getDegrees(photoMessage);
        List<Location> pointLs = LocationConstant.PZLocation.get("FZ01");
        List<Location> pointList = GisCheckUtils.sortLocation(pointLs);

        // 145.57421875
        // 34100.140625
        // 1295.87890625
        double v = GisCheckUtils.planarPolygonAreaMeters(pointList);
        System.out.println(v);
    }

    @Test
    public void MJ4(){

    }
}
