/*
 * Copyright 2016 The twicalico authors
 *
 * 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 "AS IS" 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.github.moko256.twicalico;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.StringRes;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by moko256 on 2017/03/11.
 *
 * @author moko256
 */

public class AddedImagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_IMAGE = 1;
    private static final int VIEW_TYPE_ADD = 2;

    Context context;

    private ArrayList<Uri> images = new ArrayList<>();
    private int limit = -1;

    private View.OnClickListener onAddButtonClickListener;

    public AddedImagesAdapter(Context context){
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        return ((limit == -1) || ((position < limit))) && (position < images.size())? VIEW_TYPE_IMAGE: VIEW_TYPE_ADD;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_IMAGE){
            return new ImageChildViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_images_adapter_image_child, parent, false));
        } else {
            return new AddImageViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_images_adapter_add_image, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_IMAGE){
            ImageChildViewHolder viewHolder = (ImageChildViewHolder) holder;
            Uri image = images.get(position);
            viewHolder.title.setText(image.getLastPathSegment());
            GlideApp.with(context).load(image).into(viewHolder.image);
        } else {
            holder.itemView.setOnClickListener(onAddButtonClickListener);
        }
    }

    @Override
    public int getItemCount() {
        return (limit == -1 || images.size()<limit)? images.size() + 1: limit;
    }


    public View.OnClickListener getOnAddButtonClickListener() {
        return onAddButtonClickListener;
    }

    public void setOnAddButtonClickListener(View.OnClickListener onAddButtonClickListener) {
        this.onAddButtonClickListener = onAddButtonClickListener;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public ArrayList<Uri> getImagesList() {
        return images;
    }

    public void clearImages() {
        images.clear();
        images = null;
    }

    class ImageChildViewHolder extends RecyclerView.ViewHolder{

        ImageView image;
        TextView title;

        ImageChildViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.layout_images_adapter_image_child_image);
            title = itemView.findViewById(R.id.layout_images_adapter_image_child_title);
        }
    }

    class AddImageViewHolder extends RecyclerView.ViewHolder{

        AddImageViewHolder(View itemView) {
            super(itemView);
        }
    }
}

@@ -689,15 +689,23 @@
 oid.
-net.Uri
+database.Cursor
 ;%0Aim
@@ -721,73 +721,51 @@
 oid.
-support.annotation.StringRes;%0Aimport android.support.v4.util.Pair
+net.Uri;%0Aimport android.provider.MediaStore
 ;%0Aim
@@ -2427,16 +2427,509 @@
 ition);%0A
+%0A            String fileName = null;%0A            Cursor cursor = context.getContentResolver().query(%0A                    image,%0A                    new String%5B%5D%7BMediaStore.MediaColumns.DISPLAY_NAME%7D,%0A                    null, null, null);%0A            if (cursor != null && cursor.moveToNext())%7B%0A                fileName = cursor.getString(%0A                        cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME)%0A                );%0A            %7D%0A            cursor.close();%0A%0A
         
@@ -2957,16 +2957,45 @@
 setText(
+fileName != null? fileName : 
 image.ge

fix bug that file name of images is not shown