package com.bs.ct.utils;

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.exif.GpsDirectory;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class ExifUtils {
    private static final Map<String, String> CHINESE_TAG_NAMES = new HashMap<>();

    static {
        // 初始化常用EXIF标签的中文翻译
        CHINESE_TAG_NAMES.put("Compression Type", "压缩类型");
        CHINESE_TAG_NAMES.put("Image Width", "图像宽度");
        CHINESE_TAG_NAMES.put("Image Height", "图像高度");
        CHINESE_TAG_NAMES.put("Data Precision", "数据精度");
        CHINESE_TAG_NAMES.put("Make", "制造商");
        CHINESE_TAG_NAMES.put("Model", "型号");
        CHINESE_TAG_NAMES.put("Orientation", "方向");
        CHINESE_TAG_NAMES.put("DateTime Original", "拍摄时间");
        CHINESE_TAG_NAMES.put("Create Date", "创建日期");
        CHINESE_TAG_NAMES.put("Exposure Time", "曝光时间");
        CHINESE_TAG_NAMES.put("F-Number", "光圈值");
        CHINESE_TAG_NAMES.put("ISO Speed Ratings", "ISO速度");
        CHINESE_TAG_NAMES.put("Focal Length", "焦距");
        CHINESE_TAG_NAMES.put("Flash", "闪光灯");
        CHINESE_TAG_NAMES.put("Metering Mode", "测光模式");
        CHINESE_TAG_NAMES.put("White Balance", "白平衡");
        CHINESE_TAG_NAMES.put("Exposure Program", "曝光程序");
        CHINESE_TAG_NAMES.put("Exposure Bias Value", "曝光补偿");
        CHINESE_TAG_NAMES.put("GPS Latitude", "GPS纬度");
        CHINESE_TAG_NAMES.put("GPS Longitude", "GPS经度");
        CHINESE_TAG_NAMES.put("GPS Altitude", "GPS海拔");
        CHINESE_TAG_NAMES.put("GPS Date Stamp", "GPS日期");
        CHINESE_TAG_NAMES.put("GPS Time Stamp", "GPS时间");
        CHINESE_TAG_NAMES.put("File Name", "文件名");
        CHINESE_TAG_NAMES.put("File Size", "文件大小");
        CHINESE_TAG_NAMES.put("File Modified Date", "文件修改时间");
    }

    public static Map<String, String> getExifInfoMap(String imagePath) throws Exception {
        Map<String, String> result = new LinkedHashMap<>();
        Metadata metadata = ImageMetadataReader.readMetadata(new File(imagePath));

        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                String chineseName = CHINESE_TAG_NAMES.getOrDefault(tag.getTagName(), tag.getTagName());
                result.put(chineseName, tag.getDescription());
            }
        }
        return result;
    }

    public static StringBuffer getExifInfo(String imagePath) throws Exception {
        StringBuffer result = new StringBuffer();
        Metadata metadata = ImageMetadataReader.readMetadata(new File(imagePath));

        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                String chineseName = CHINESE_TAG_NAMES.getOrDefault(tag.getTagName(), tag.getTagName());
                result.append(chineseName + ":" + tag.getDescription() + "\n");
            }
        }
        return result;
    }
}
