package com.rehome.sgbaxj.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 android.widget.LinearLayout;

import com.rehome.sgbaxj.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

public class Q4UploadGridAdapter extends BaseAdapter {
    private ArrayList<String> listUrls;
    private LayoutInflater inflater;
    Context context;
    private OnItemDelClickListener onItemDelClickListener;

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

    public Q4UploadGridAdapter(ArrayList<String> listUrls, Context context, OnItemDelClickListener onItemDelClickListener) {
        this.listUrls = listUrls;
        this.context=context;
        this.onItemDelClickListener=onItemDelClickListener;
        if (listUrls.size() == 7) {
            listUrls.remove(listUrls.size() - 1);
        }
        //这个意思就是 如果最后返回的列表有7哥 就把最后一个移除了
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return listUrls.size();
    }

    @Override
    public String getItem(int position) {
        return listUrls.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.sblcsave_item, parent, false);
            holder.image = (ImageView) convertView.findViewById(R.id.imageView);
            holder.ll_del = (LinearLayout) convertView.findViewById(R.id.ll_del);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final String path = listUrls.get(position);
        //这里是如果取到path的值是paizhao  就显示加好  如果不是  就是返回的那个图片
        if (path.equals("paizhao")) {
            holder.image.setImageResource(R.mipmap.find_add_img);
            holder.ll_del.setVisibility(View.GONE);
        } else {
            holder.ll_del.setVisibility(View.VISIBLE);
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.color_f6)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(context)
                    .load(path)
                    .apply(options)
                    .into(holder.image);

//
//                Glide.with(TjqxdActivity.this)
//                        .load(path)
//                        .placeholder(R.mipmap.default_error)
//                        .error(R.mipmap.default_error)
//                        .centerCrop()
//                        .into(holder.image);
        }

        holder.ll_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemDelClickListener!=null){
                    onItemDelClickListener.onClick(position);
                }
            }
        });

        return convertView;
    }

    class ViewHolder {
        ImageView image;
        LinearLayout ll_del;
    }
}