package com.keystar.plane.inspection.service.impl;

import com.drew.imaging.jpeg.JpegProcessingException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.keystar.plane.inspection.bo.Location;
import com.keystar.plane.inspection.bo.PhotoMqttMessageBo;
import com.keystar.plane.inspection.constant.LocationConstant;
import com.keystar.plane.inspection.dao.PhotoMessageVo;
import com.keystar.plane.inspection.dao.PlanePhotoVo;
import com.keystar.plane.inspection.service.PhotoService;
import com.keystar.plane.inspection.utils.FileUtil;
import com.keystar.plane.inspection.utils.GisCheckUtils;
import com.keystar.plane.inspection.utils.PhotoUtils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.junit.jupiter.params.shadow.com.univocity.parsers.conversions.LowerCaseConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class PhotoServiceImpl implements PhotoService {

//    @Autowired
//    MqttAsyncClient mqttAsyncClient;

//    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();


    @Override
    public PlanePhotoVo queryPhoto(String filePath) throws IOException, JpegProcessingException {
        PlanePhotoVo planePhotoVo = new PlanePhotoVo();
//            file = ResourceUtils.getFile("classpath:1114_THRM.JPG");
        File file = new File(filePath);

        // 获取图片中的信息
        PhotoMessageVo photoMessage = PhotoUtils.getPhotoMessage(file.getPath());
        // 根据图片信息，获取图片四个角的经纬度，判断图片在哪个光伏区
        List<Location> degrees = PhotoUtils.getDegrees(photoMessage);
        Location location = new Location();
        location.setLat(photoMessage.getLatitude());
        location.setLon(photoMessage.getLongitude());
        for ( Map.Entry<String, List<Location>> pzMap : LocationConstant.PZLocation.entrySet()){
            List<Location> value = pzMap.getValue();
            // 判断第一个多边形是否全部在第二个多边形内
//            if (GisCheckUtils.isPolygonAllInPolygon(degrees, value)){
//                planePhotoVo.setPZName(pzMap.getKey());
//            }

            // 判断第一个多边形是否存在一个点在第二个多边形内
//            if (GisCheckUtils.isPolygonInPolygon(degrees, value)){
//                planePhotoVo.setPZName(pzMap.getKey());
//            }

            // 当图片与光伏区的重合面积大于图片本身面积的百分之80才判断图片属于这个光伏区
            double photoArea = GisCheckUtils.planarPolygonAreaMeters(degrees);
            if (GisCheckUtils.CoincidentArea(degrees, value) >= photoArea * 0.6){
                planePhotoVo.setPZName(pzMap.getKey());
            }
        }

        // 查出图片中包含哪些光伏板
        if (planePhotoVo.getPZName() != null){
            String pzName = planePhotoVo.getPZName();
            Map<String, List<Location>> stringListMap = LocationConstant.PVLocation.get(pzName.split("FZ")[1]);
            List<String> pvNameList = new ArrayList<>();
            for (Map.Entry<String, List<Location>> pvMap : stringListMap.entrySet()){
                // 判断第一个多边形是否存在一个点在第二个多边形内
//                if (GisCheckUtils.isPolygonInPolygon(pvMap.getValue(), degrees)){
//                    pvNameList.add(pvMap.getKey());
//                }
                // 判断第一个多边形是否全部在第二个多边形内
//                if (GisCheckUtils.isPolygonAllInPolygon(pvMap.getValue(), degrees)){
//                    pvNameList.add(pvMap.getKey());
//                }

                // 当图片与的支架重合面积大于支架本身面积的百分之80才判断这个支架属于这个图片
                double pvArea = GisCheckUtils.planarPolygonAreaMeters(pvMap.getValue());
                if (GisCheckUtils.CoincidentArea(degrees, pvMap.getValue()) >= pvArea * 0.6){
                    pvNameList.add(pvMap.getKey());
                }
            }
            planePhotoVo.setPVNames(pvNameList);
        }

        return planePhotoVo;
    }



}
