package com.datarecovery.master.utils.xfile;

import android.net.Uri;
import android.os.Build;
import android.os.storage.StorageVolume;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;

public class StorageVolumeUtil {

    /**
     * 获取存储卷的根目录
     */
    public static File getDirectory(StorageVolume storageVolume) {
        File directory;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            directory = storageVolume.getDirectory();
        } else {
            try {
                Method getPathFile = StorageVolume.class.getMethod("getPathFile");
                directory = (File) getPathFile.invoke(storageVolume);
            } catch (NoSuchMethodException | IllegalAccessException |
                     InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
        return directory;
    }

    /**
     * 获取存储卷的根目录Uri
     */
    public static Uri getRootUri(StorageVolume storageVolume) {
        Uri rootUri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            rootUri = storageVolume.createOpenDocumentTreeIntent().getParcelableExtra(
                    DocumentsContract.EXTRA_INITIAL_URI);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                rootUri = DocumentsContract.buildRootUri("com.android.externalstorage.documents",
                        (storageVolume.isPrimary() ? "primary" : storageVolume.getUuid()));
            } else {
                try {
                    Method isPrimary = StorageVolume.class.getMethod("isPrimary");
                    boolean primary = (boolean) isPrimary.invoke(storageVolume);
                    //反射获取getUuid
                    Method getUuid = StorageVolume.class.getMethod("getUuid");
                    String uuid = (String) getUuid.invoke(storageVolume);
                    rootUri = DocumentsContract.buildRootUri("com.android.externalstorage.documents",
                            (primary ? "primary" : uuid));
                } catch (NoSuchMethodException | IllegalAccessException |
                         InvocationTargetException e) {
                }
            }
        }
        return rootUri;
    }

    public static Uri getTreeUri(StorageVolume storageVolume) {
        return Uri.parse(getRootUri(storageVolume).toString().replace("/root/", "/tree/"));
    }

    public static String getMediaStoreVolumeName(StorageVolume storageVolume) {
        String volumeName = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            volumeName = storageVolume.getMediaStoreVolumeName();
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
            volumeName = storageVolume.isPrimary() ? MediaStore.VOLUME_EXTERNAL_PRIMARY : getNormalizedUuid(storageVolume);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            volumeName = storageVolume.isPrimary() ? "external" : getNormalizedUuid(storageVolume);
        } else {
            //反射获取isPrimary
            try {
                Method isPrimary = StorageVolume.class.getMethod("isPrimary");
                boolean primary = (boolean) isPrimary.invoke(storageVolume);
                volumeName = primary ? "external" : getNormalizedUuid(storageVolume);
            } catch (Exception e) {
            }

        }
        return volumeName;
    }

    public static String getNormalizedUuid(StorageVolume storageVolume) {
        String uuid = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            uuid = storageVolume.getUuid();
        } else {
            try {
                Method getUuid = StorageVolume.class.getMethod("getUuid");
                uuid = (String) getUuid.invoke(storageVolume);
            } catch (Exception e) {
            }
        }
        return uuid == null ? null : uuid.toLowerCase(Locale.US);
    }
}