package com.casic.dcms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.casic.dcms.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Pengxh
 * @Time: 2021/4/7 17:17
 * @Email: 290677893@qq.com
 **/
public class ImageRecycleViewAdapter extends RecyclerView.Adapter<ImageRecycleViewAdapter.ItemViewHolder> {

    private Context context;
    private boolean isShow = true;
    private List<String> mediaList = new ArrayList<>();

    public ImageRecycleViewAdapter(Context mContext) {
        this.context = mContext;
    }

    public void setMediaList(@Nullable List<String> imageUrlList) {
        mediaList.clear();
        if (imageUrlList != null) {
            mediaList.addAll(imageUrlList);
        }
        notifyDataSetChanged();
    }

    public void setDeleteView(boolean isShow) {
        this.isShow = isShow;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_select_gridview, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return mediaList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        Glide.with(context)
                .load(mediaList.get(position))
                .apply(new RequestOptions().error(R.drawable.ic_load_error))
                .into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onClick(position);
            }
        });
        if (isShow) {
            holder.deleteView.setVisibility(View.VISIBLE);
            holder.deleteView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteClickListener.onClick(position);
                }
            });
        } else {
            holder.deleteView.setVisibility(View.INVISIBLE);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private ImageView deleteView;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            deleteView = itemView.findViewById(R.id.deleteView);
        }
    }

    private OnDeleteItemClickListener deleteClickListener;

    public interface OnDeleteItemClickListener {
        void onClick(int position);
    }

    public void setOnDeleteClickListener(OnDeleteItemClickListener deleteItemClickListener) {
        this.deleteClickListener = deleteItemClickListener;
    }

    private OnItemClickListener itemClickListener;

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
