package xn.xxp.home.window;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.List;

import http.client.HttpTool;
import xn.xxp.R;
import xn.xxp.databinding.ItemSafeClassifyImageBinding;

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

    private Context context;
    private List<String> classifyList;

    public WindowHorizontalAdapter(Context context, List<String> classifyList) {
        this.context = context;
        this.classifyList = classifyList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(ItemSafeClassifyImageBinding.inflate(LayoutInflater.from(context), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemSafeClassifyImageBinding binding = holder.binding;
        if (null != classifyList && null != classifyList.get(position)) {
            String url = classifyList.get(position);
            if (!TextUtils.isEmpty(url)) {
                Glide.with(binding.image).load(HttpTool.checkUrl(url)).placeholder(R.mipmap.img_default).error(R.mipmap.img_default).addListener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        binding.image.setVisibility(View.GONE);
                        LogUtils.e("图片加载失败", Log.getStackTraceString(e));
                        return true;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                }).into(binding.image);
            } else {
                Glide.with(binding.image).load(R.mipmap.img_default).placeholder(R.mipmap.img_default).error(R.mipmap.img_default).into(binding.image);
            }
        }
    }

    @Override
    public int getItemCount() {
        return null == classifyList ? 0 : classifyList.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<String> classifyList) {
        this.classifyList = classifyList;
        LogUtils.d(classifyList);
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ItemSafeClassifyImageBinding binding;

        public ViewHolder(ItemSafeClassifyImageBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
