package com.navinfo.outdoor.util;

import android.content.Intent;
import android.util.Log;
import android.widget.ImageView;

import androidx.exifinterface.media.ExifInterface;

import com.bumptech.glide.Glide;
import com.navinfo.outdoor.api.Constant;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;

import java.io.File;
import java.io.IOException;
import java.util.BitSet;

import static org.greenrobot.eventbus.EventBus.TAG;

public class PhotoUtils {
    private static int numbits = 6 * 5;
    // 为了加密，所以改了base32编码代码表
    final static char[] digits = { 'd', 'f', 'g', 'y', 'n', 't', 'u', 'v', 'q',
            'b', '9', 's', 'z', '7', 'i', '2', 'h', '5', '0', 'p', '4', 'm',
            '8', 'x', 'c', 'j', '6', 'e', '1', 'r', '3', 'k' };

    public static String showPhotoAndGetPath(File file, ImageView imageView) {
        Glide.with(imageView.getContext()).load(file).into(imageView);
        return file.getAbsolutePath();
    }

    public static File showPhotoFile(String d, LatLng latLng) {
        String lat="";
        if (latLng!=null){
           lat= initEncode(latLng);
        }
        long time = System.currentTimeMillis();
        File file = new File(Constant.PICTURE_FOLDER, d + time + "_p" + lat + "_a" + 0 + ".jpg");
        return file;
    }

    public static String initEncode(LatLng latLng) {
        BitSet latbits = getBits(latLng.latitude, -90, 90);
        BitSet lonbits = getBits(latLng.longitude, -180, 180);
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < numbits; i++) {
            buffer.append((lonbits.get(i)) ? '1' : '0');
            buffer.append((latbits.get(i)) ? '1' : '0');
        }
        return base32(Long.parseLong(buffer.toString(), 2));
    }

    private static BitSet getBits(double lat, double floor, double ceiling) {
        BitSet buffer = new BitSet(numbits);
        for (int i = 0; i < numbits; i++) {
            double mid = (floor + ceiling) / 2;
            if (lat >= mid) {
                buffer.set(i);
                floor = mid;
            } else {
                ceiling = mid;
            }
        }
        return buffer;
    }

    public static String base32(long i) {
        char[] buf = new char[65];
        int charPos = 64;
        boolean negative = (i < 0);
        if (!negative)
            i = -i;
        while (i <= -32) {
            buf[charPos--] = digits[(int) (-(i % 32))];
            i /= 32;
        }
        buf[charPos] = digits[(int) (-i)];

        if (negative)
            buf[--charPos] = '-';
        return new String(buf, charPos, (65 - charPos));
    }

    /**
     * 获取图片旋转角度
     *
     * @param filepath
     * @return
     */
    public static int getExifOrientation(String filepath) {
        int degree = 0;
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException ex) {
            Log.d(TAG, "cannot read exif" + ex);
        }
        if (exif != null) {
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
            }
        }
        return degree;
    }

}
