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 android.widget.TextView;

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

import com.casic.dcms.R;
import com.casic.dcms.utils.Constant;

public class AnalysisRecycleAdapter extends RecyclerView.Adapter<AnalysisRecycleAdapter.ItemViewHolder> {

    private Context context;

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

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

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.imageView.setBackgroundResource(Constant.ANALYSIS_ICONS.get(position));
        holder.textView.setText(Constant.ANALYSIS_ITEMS.get(position));
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position);
                }
            });
        }
    }

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

    private OnGridItemClickListener clickListener;

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

    public void setOnGridItemClickListener(OnGridItemClickListener onGridItemClickListener) {
        this.clickListener = onGridItemClickListener;
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView textView;

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