/*
 * 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.internal.entity;

import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.annotation.Nullable;

import com.zhihu.matisse.MimeType;

public class Item implements Parcelable {
    public static final Creator<Item> CREATOR = new Creator<Item>() {
        @Override
        @Nullable
        public Item createFromParcel(Parcel source) {
            return new Item(source);
        }

        @Override
        public Item[] newArray(int size) {
            return new Item[size];
        }
    };
    public static final long ITEM_ID_CAPTURE = -1;
    public static final String ITEM_DISPLAY_NAME_CAPTURE = "Capture";
    public final long id;
    public final String mimeType;
    public final Uri uri;
    public final long size;

    Item(long id, String mimeType, long size) {
        this.id = id;
        this.mimeType = mimeType;
        this.uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
        this.size = size;
    }

    Item(Parcel source) {
        id = source.readLong();
        mimeType = source.readString();
        uri = source.readParcelable(Uri.class.getClassLoader());
        size = source.readLong();
    }

    public static Item valueOf(Cursor cursor) {
        return new Item(cursor.getLong(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)),
                cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.MIME_TYPE)),
                cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns.SIZE)));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(mimeType);
        dest.writeParcelable(uri, 0);
        dest.writeLong(size);
    }

    public Uri getContentUri() {
        return uri;
    }

    public boolean isCapture() {
        return id == ITEM_ID_CAPTURE;
    }

    public boolean isGif() {
        return mimeType.equals(MimeType.GIF.toString());
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Item)) {
            return false;
        }

        Item other = (Item) obj;
        return other.uri.equals(uri);
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Long.valueOf(id).hashCode();
        result = 31 * result + mimeType.hashCode();
        result = 31 * result + uri.hashCode();
        result = 31 * result + Long.valueOf(size).hashCode();
        return result;
    }
}
@@ -1657,46 +1657,177 @@
     
-this.uri = ContentUris.withAppendedId(
+Uri contentUri;%0A        if (isImage()) %7B%0A            contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;%0A        %7D else if (isVideo()) %7B%0A            contentUri = 
 Medi
@@ -1825,38 +1825,37 @@
 ri = MediaStore.
-Images
+Video
 .Media.EXTERNAL_
@@ -1865,16 +1865,187 @@
 TENT_URI
+;%0A        %7D else %7B%0A            // ?%0A            contentUri = MediaStore.Files.getContentUri(%22external%22);%0A        %7D%0A        this.uri = ContentUris.withAppendedId(contentUri
 , id);%0A 
@@ -3057,59 +3057,420 @@
 n is
-Gif() %7B%0A        return mimeType.equals(MimeType.GIF
+Image() %7B%0A        return mimeType.equals(MimeType.JPEG.toString())%0A                %7C%7C mimeType.equals(MimeType.PNG.toString())%0A                %7C%7C mimeType.equals(MimeType.GIF);%0A    %7D%0A%0A    public boolean isGif() %7B%0A        return mimeType.equals(MimeType.GIF.toString());%0A    %7D%0A%0A    public boolean isVideo() %7B%0A        return mimeType.equals(MimeType.MPEG.toString())%0A                %7C%7C mimeType.equals(MimeType.MP4
 .toS

generate different uri for image and video