package com.keystar.plane.inspection.utils;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.lang.GeoLocation;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.GpsDirectory;
import com.keystar.plane.inspection.bo.Location;
import com.keystar.plane.inspection.dao.DegreesVo;
import com.keystar.plane.inspection.dao.PhotoMessageVo;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


public class PhotoUtils {


    /**
     *  获取JPG图片的metadata信息
     **/
    public static PhotoMessageVo getPhotoMessage(String mFilePath) throws IOException, JpegProcessingException {

//        File file = ResourceUtils.getFile("classpath:20230208144300_DJI_20230208141838_0001_THRM.jpg");
        Metadata metadata = JpegMetadataReader.readMetadata(new File(mFilePath));
        PhotoMessageVo photoMessageVo = new PhotoMessageVo();
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                String tagName = tag.getTagName();  //标签名
                String desc = tag.getDescription(); //标签信息
//                System.out.println(tagName + "===" + desc);//照片信息

                switch(tagName)
                {
                    case "Image Height" :
                        photoMessageVo.setImageHigh(Utils.getNumber(desc));
                        break;
                    case "Image Width" :
                        photoMessageVo.setImageWidth(Utils.getNumber(desc));
                        break;
                    case "Focal Length" :
                        photoMessageVo.setFocalLength(Utils.getNumber(desc));
                        break;
                    case "Focal Length 35" :
                        photoMessageVo.setFocalLength35(Utils.getNumber(desc));
                        break;
                    case "GPS Altitude" :
                        photoMessageVo.setAltitude(Utils.getNumber(desc));
                        break;
                    default:
                        break;
                }
            }
        }

        Collection<GpsDirectory> gpsDirectories = metadata.getDirectoriesOfType(GpsDirectory.class);
        for(GpsDirectory gps : gpsDirectories) {
            //获取图片的经纬度信息
            GeoLocation geoLocation = gps.getGeoLocation();
            photoMessageVo.setLatitude(geoLocation.getLatitude());
            photoMessageVo.setLongitude(geoLocation.getLongitude());
        }
        return photoMessageVo;
    }


    /**
     *  获取图片的经纬度信息
     **/
    public static void getPhotoGps() throws IOException, JpegProcessingException {

        File file = ResourceUtils.getFile("classpath:20230208144300_DJI_20230208141838_0001_THRM.jpg");
        Metadata metadata = JpegMetadataReader.readMetadata(file);

        Collection<GpsDirectory> gpsDirectories = metadata.getDirectoriesOfType(GpsDirectory.class);
        for(GpsDirectory gps : gpsDirectories) {
            //获取图片的经纬度信息
            GeoLocation geoLocation = gps.getGeoLocation();
            System.out.println(geoLocation.getLongitude());
            System.out.println(geoLocation.getLatitude());
        }
    }

    /**
     *  求解图片四个角的经纬度坐标
     **/
    public static List<Location> getDegrees(PhotoMessageVo photoMessageVo){
        //飞行高度 l  altitude
        //图片宽度 h  imageWidth
        //图片高度 w  imageHigh
        //焦距 p    focalLength
        //显示视场角 dFov = 40.6

//        double altitude = photoMessageVo.getAltitude() - 52;
        double altitude = photoMessageVo.getAltitude() - 50;

        double dFov = 40.6;
        //对角线d  diagonal 根据dFov计算
        double a = Math.toRadians(dFov / 2);
        double tan = Math.tan(a);
        BigDecimal diagonal  = new BigDecimal(photoMessageVo.getFocalLength()).multiply(BigDecimal.valueOf(tan));
        //开平方：高度的平方+宽度的平方
        double num1 = Math.sqrt(Math.pow(photoMessageVo.getImageWidth(), 2) + Math.pow(photoMessageVo.getImageHigh(),2));
        DegreesVo degreesVo = new DegreesVo();
        //经度换算单位(w)
        BigDecimal num2 = new BigDecimal(0.00001141);
        //纬度换算单位(h)
        BigDecimal num3 = new BigDecimal(0.00000899);
        // w
        BigDecimal w = BigDecimal.valueOf(photoMessageVo.getImageWidth()/num1).multiply(diagonal);
        // h
        BigDecimal h = BigDecimal.valueOf(photoMessageVo.getImageHigh()/num1).multiply(diagonal);
        // HFov
        BigDecimal divide = w.divide(BigDecimal.valueOf(photoMessageVo.getFocalLength()), 10, BigDecimal.ROUND_HALF_UP);
        double atan = Math.atan(divide.doubleValue());
        BigDecimal aa = BigDecimal.valueOf(Math.atan(atan)).multiply(BigDecimal.valueOf(180)).divide(BigDecimal.valueOf(Math.PI), 10, BigDecimal.ROUND_HALF_UP);
        BigDecimal HFov = new BigDecimal(2).multiply(aa);

        double wTan = Math.tan(Math.toRadians(HFov.divide(BigDecimal.valueOf(2),10,BigDecimal.ROUND_HALF_UP).doubleValue()));
        // 实际精度的长度（米）
        BigDecimal airLong = new BigDecimal(altitude).multiply(BigDecimal.valueOf(wTan));
        // 实际经度 W（角度）
        BigDecimal airLongitude = airLong.multiply(num2);

        // VFov
        BigDecimal HDivide = h.divide(BigDecimal.valueOf(photoMessageVo.getFocalLength()), 10, BigDecimal.ROUND_HALF_UP);
        double bTan = Math.atan(HDivide.doubleValue());
        BigDecimal H = BigDecimal.valueOf(Math.atan(bTan)).multiply(BigDecimal.valueOf(180)).divide(BigDecimal.valueOf(Math.PI), 10, BigDecimal.ROUND_HALF_UP);
        BigDecimal VFov = new BigDecimal(2).multiply(H);

        double HTan = Math.tan(Math.toRadians(VFov.divide(BigDecimal.valueOf(2),10,BigDecimal.ROUND_HALF_UP).doubleValue()));
        // 实际纬度的高（米）
        BigDecimal airHight = new BigDecimal(altitude).multiply(BigDecimal.valueOf(HTan));
        //实际纬度H(角度)
        BigDecimal airLatitude = airHight.multiply(num3);


        List<Location> locations = new ArrayList<>();
        Location location = new Location();
        location.setLon(photoMessageVo.getLongitude() - airLongitude.doubleValue());
        location.setLat(photoMessageVo.getLatitude() + airLatitude.doubleValue());
        locations.add(location);

        Location location1 = new Location();
        location1.setLon(photoMessageVo.getLongitude() + airLongitude.doubleValue());
        location1.setLat(photoMessageVo.getLatitude() + airLatitude.doubleValue());
        locations.add(location1);

        Location location2 = new Location();
        location2.setLon(photoMessageVo.getLongitude() - airLongitude.doubleValue());
        location2.setLat(photoMessageVo.getLatitude() - airLatitude.doubleValue());
        locations.add(location2);

        Location location3 = new Location();
        location3.setLon(photoMessageVo.getLongitude() + airLongitude.doubleValue());
        location3.setLat(photoMessageVo.getLatitude() - airLatitude.doubleValue());
        locations.add(location3);

        return locations;
    }


}
