package com.casic.dcms.adapter;

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

import androidx.annotation.Nullable;

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

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

public class ImageGridViewAdapter extends BaseAdapter {

    private final Context context;
    private final List<String> images = new ArrayList<>();

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

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

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

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object getItem(int position) {
        return images.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_select_gridview, null);
            holder = new ItemViewHolder();
            holder.imageView = convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        } else {
            holder = (ItemViewHolder) convertView.getTag();
        }
        Glide.with(context)
                .load(images.get(position))
                .apply(new RequestOptions().error(R.drawable.ic_load_error))
                .into(holder.imageView);
        return convertView;
    }

    private static class ItemViewHolder {
        private ImageView imageView;
    }
}
