/*
 * Copyright (C) 2014 nohana, Inc.
 * Copyright 2017 Zhihu Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhihu.matisse;

import android.content.ContentResolver;
import android.net.Uri;
import android.webkit.MimeTypeMap;

import com.zhihu.matisse.internal.utils.PhotoMetadataUtils;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
 * MIME Type enumeration to restrict selectable media on the selection activity.
 */
@SuppressWarnings("unused")
public enum MimeType {
    JPEG("image/jpeg", new HashSet<String>() {{
        add("jpg");
        add("jpeg");
    }}),
    PNG("image/png", new HashSet<String>() {{
        add("png");
    }}),
    GIF("image/gif", new HashSet<String>() {{
        add("gif");
    }}),
    MPEG("video/mpeg", new HashSet<String>() {{
        add("mpeg");
        add("mpg");
    }}),
    MP4("video/mp4", new HashSet<String>() {{
        add("mp4");
        add("m4v");
    }});

    private final String mMimeTypeName;
    private final Set<String> mExtensions;

    MimeType(String mimeTypeName, Set<String> extensions) {
        mMimeTypeName = mimeTypeName;
        mExtensions = extensions;
    }

    public static Set<MimeType> allOf() {
        return EnumSet.allOf(MimeType.class);
    }

    public static Set<MimeType> of(MimeType type) {
        return EnumSet.of(type);
    }

    public static Set<MimeType> of(MimeType type, MimeType... rest) {
        return EnumSet.of(type, rest);
    }

    public static Set<MimeType> ofImage() {
        return EnumSet.of(JPEG, PNG, GIF);
    }

    public static Set<MimeType> ofVideo() {
        return EnumSet.of(MPEG, MP4);
    }

    @Override
    public String toString() {
        return mMimeTypeName;
    }

    public boolean checkType(ContentResolver resolver, Uri uri) {
        MimeTypeMap map = MimeTypeMap.getSingleton();
        if (uri == null) {
            return false;
        }
        String type = map.getExtensionFromMimeType(resolver.getType(uri));
        for (String extension : mExtensions) {
            if (extension.equals(type)) {
                return true;
            }
            String path = PhotoMetadataUtils.getPath(resolver, uri);
            if (path != null && path.toLowerCase(Locale.US).endsWith(extension)) {
                return true;
            }
        }
        return false;
    }
}

@@ -1006,17 +1006,230 @@
 ctivity.
+ Matisse only supports images and videos.%0A *%0A * Good example of mime types Android supports:%0A * https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/MediaFile.java
 %0A
-
  */%0A@Sup
@@ -1275,16 +1275,60 @@
 eType %7B%0A
+    // ============== images ==============%0A
     JPEG
@@ -1554,24 +1554,24 @@
 add(%22gif%22);%0A
-
     %7D%7D),%0A   
@@ -1563,24 +1563,227 @@
 );%0A    %7D%7D),%0A
+    BMP(%22image/x-ms-bmp%22, new HashSet%3CString%3E() %7B%7B%0A        add(%22bmp%22);%0A    %7D%7D),%0A    WEBP(%22image/webp%22, new HashSet%3CString%3E() %7B%7B%0A        add(%22webp%22);%0A    %7D%7D),%0A%0A    // ============== videos ==============%0A
     MPEG(%22vi

add bmp, webp image mime types