package com.navinfo.outdoor.adapter;

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

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.bean.TaskExplainInfo;
import com.navinfo.outdoor.bean.TaskPrefectureBean;

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

public class TaskExplainAdapter extends RecyclerView.Adapter<TaskExplainAdapter.ViewHolder> {
    private List<TaskPrefectureBean.BodyBean.ListBean> explainList = new ArrayList<>();
    private Context context;

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

    public void setExplainList(List<TaskPrefectureBean.BodyBean.ListBean> explainList) {
        this.explainList.addAll(explainList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View inflate = LayoutInflater.from(context).inflate(R.layout.tas_explain_item, parent, false);

        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(explainList.get(position).getIconurl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.ivExplain);
        holder.tvExplain.setText(explainList.get(position).getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClick != null) {
                    mOnItemClick.onClick(position);
                }
            }
        });

    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivExplain;
        TextView tvExplain;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivExplain = itemView.findViewById(R.id.iv_tas_explain);
            tvExplain = itemView.findViewById(R.id.tas_explain_title);
        }
    }

    public interface OnItemClick {
        void onClick(int pos);
    }

    private OnItemClick mOnItemClick;

    public void setOnItemClick(OnItemClick pOnItemClick) {
        mOnItemClick = pOnItemClick;
    }

}
