package com.example.calendaryapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class WeekAdapter extends RecyclerView.Adapter<WeekAdapter.WeekViewHolder> {
    private ArrayList<WeekItem> mExampleList;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }


    public static class WeekViewHolder extends RecyclerView.ViewHolder {
        //en definitiva lo que contiene la informacion de cada item del listado que se asocia la layout
        //encargado de mostrar cada item
        public TextView mTextView1;
        public TextView mTextView2;

        public WeekViewHolder(View itemView, final OnItemClickListener listener) {
            super(itemView);
            //mImageView = itemView.findViewById(R.id.imageView);
            mTextView1 = itemView.findViewById(R.id.textView);
            mTextView2 = itemView.findViewById(R.id.textView2);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onItemClick(position);
                        }

                    }
                }
            });
        }
    }

    public WeekAdapter(ArrayList<WeekItem> exampleList) {
        mExampleList = exampleList;
    }


    @Override
    public WeekViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //se invoca por el layout manager
        //crea una nueva vista
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.week_item, parent, false);
        WeekViewHolder evh = new WeekViewHolder(v,mListener);
        return evh;
    }

    @Override
    public void onBindViewHolder(WeekViewHolder holder, int position) {
        //lo llama el layoutmanager
        //metodo que asocia el viewholder a los datos
        //mediante la posicion que se setea previamente
        WeekItem currentItem = mExampleList.get(position);
        holder.mTextView1.setText(currentItem.getText1());
        holder.mTextView2.setText(currentItem.getText2());
    }

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