package com.kfb.kfbv1;

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

import androidx.recyclerview.widget.RecyclerView;

import com.kfb.kfbv1.dummy.DummyContent;

import java.util.List;

/**
 * {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the
 * specified {@link OnListFragmentInteractionListener}.
 * TODO: Replace the implementation with code for your data type.
 */
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {

    private final List<DummyContent.DummyItem> mValues;
    private final ItemFragment.OnListFragmentInteractionListener mListener;

    public MyItemRecyclerViewAdapter(List<DummyContent.DummyItem> items, ItemFragment.OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.Nazwa.setText(mValues.get(position).Nazwa);
        holder.Status.setText(mValues.get(position).Status);
        holder.Error.setText(mValues.get(position).Error);
        holder.State.setText(mValues.get(position).State);
        holder.Temperatura.setText(mValues.get(position).Temperatura);
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListFragmentInteraction(holder.mItem);
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView Nazwa;
        public final TextView Status;
        public final TextView Error;
        public final TextView State;
        public final TextView Temperatura;

        public DummyContent.DummyItem mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            Nazwa = (TextView) view.findViewById(R.id.idNazwa);
            Status = (TextView) view.findViewById(R.id.idStatus);
            Error = (TextView) view.findViewById(R.id.idError);
            State = (TextView) view.findViewById(R.id.idState);
            Temperatura = (TextView) view.findViewById(R.id.idTemperatura);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + Nazwa.getText() + "'";
        }
    }
}