package com.dji.sample.common.util;


import com.dji.sample.component.oss.model.OssConfiguration;
import com.dji.sample.component.oss.service.impl.OssServiceContext;
import com.dji.sample.media.model.MediaExifDTO;
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.*;
import com.drew.metadata.file.FileTypeDirectory;
import com.drew.metadata.mp4.Mp4Directory;
import com.drew.metadata.mp4.media.Mp4VideoDirectory;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

/**
 * @author hqjiang
 * @version 1.0
 * @date 2024/7/3
 */

@Slf4j
@Component
public class PicExifUtil {

    public static final String MP4 = ".MP4";
    public static final String JPG = ".JPG";

    public static MediaExifDTO getPicExif(String objectKey) {
        long sTime = System.currentTimeMillis();
        if (objectKey.endsWith(MP4)) {
            log.info("======获取MP4文件" + objectKey + "开始时间：" + sTime);
        }
        OssServiceContext ossService = SpringBeanUtilsTest.getBean(OssServiceContext.class);
        InputStream inputStream = ossService.getObject(OssConfiguration.bucket, objectKey);

        long eTime = System.currentTimeMillis();
        if (objectKey.endsWith(MP4)) {
            log.info("======获取MP4文件" + objectKey + "结束时间：" + sTime + ",耗时：" + (eTime - sTime));
        }

        return getPicExif(inputStream,objectKey.endsWith(MP4));
    }

    public static MediaExifDTO getPicExif(InputStream inputStream,boolean isVideo) {
        try {
            MediaExifDTO.MediaExifDTOBuilder builder = MediaExifDTO.builder();
            Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
            //视频
            if(isVideo) {
                //视频宽高
                Mp4VideoDirectory mp4VideoDirectory = metadata.getFirstDirectoryOfType(Mp4VideoDirectory.class);
                builder.imageWidth(mp4VideoDirectory.getInteger(Mp4VideoDirectory.TAG_WIDTH));
                builder.imageHeight(mp4VideoDirectory.getInteger(Mp4VideoDirectory.TAG_HEIGHT));
                builder.xResolution(mp4VideoDirectory.getInteger(Mp4VideoDirectory.TAG_HORIZONTAL_RESOLUTION));
                builder.yResolution(mp4VideoDirectory.getInteger(Mp4VideoDirectory.TAG_VERTICAL_RESOLUTION));

                //视频时长
                Mp4Directory mp4Directory = metadata.getFirstDirectoryOfType(Mp4Directory.class);
                builder.durationSeconds(mp4Directory.getInteger(Mp4Directory.TAG_DURATION_SECONDS));
                builder.createTime(mp4Directory.getDate(Mp4Directory.TAG_CREATION_TIME).getTime());

            } else {
                //经纬度
                GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
                builder.absoluteAltitude(gpsDirectory.getDouble(GpsDirectory.TAG_ALTITUDE));
                builder.longitude(gpsDirectory.getGeoLocation().getLongitude());
                builder.longitudeRef(gpsDirectory.getString(GpsDirectory.TAG_LONGITUDE_REF));
                builder.latitude(gpsDirectory.getGeoLocation().getLatitude());
                builder.latitudeRef(gpsDirectory.getString(GpsDirectory.TAG_LATITUDE_REF));
                //分辨率
                ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
                builder.xResolution(exifIFD0Directory.getInteger(ExifIFD0Directory.TAG_X_RESOLUTION));
                builder.yResolution(exifIFD0Directory.getInteger(ExifIFD0Directory.TAG_Y_RESOLUTION));
                //图片宽高
                ExifSubIFDDirectory exifSubIFDDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
                builder.imageWidth(exifSubIFDDirectory.getInteger(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH));
                builder.imageHeight(exifSubIFDDirectory.getInteger(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT));
            }
            //媒体类型
            FileTypeDirectory fileTypeDirectory = metadata.getFirstDirectoryOfType(FileTypeDirectory.class);
            builder.mediaType(fileTypeDirectory.getString(FileTypeDirectory.TAG_DETECTED_FILE_MIME_TYPE));
            builder.pictureType(fileTypeDirectory.getString(FileTypeDirectory.TAG_DETECTED_FILE_TYPE_NAME));
            return builder.build();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
