package com.example.weighttracker;

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

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

import java.util.List;

// This adapter displays each saved weight entry inside the RecyclerView.
public class WeightAdapter extends RecyclerView.Adapter<WeightAdapter.WeightViewHolder> {

    // Listener for row delete clicks.
    public interface OnDeleteClickListener {
        void onDeleteClick(int id);
    }

    // Listener for tapping a row to edit it.
    public interface OnItemClickListener {
        void onItemClick(WeightEntry entry);
    }

    private List<WeightEntry> items;
    private final OnDeleteClickListener onDeleteClickListener;
    private final OnItemClickListener onItemClickListener;

    public WeightAdapter(
            List<WeightEntry> items,
            OnDeleteClickListener onDeleteClickListener,
            OnItemClickListener onItemClickListener
    ) {
        this.items = items;
        this.onDeleteClickListener = onDeleteClickListener;
        this.onItemClickListener = onItemClickListener;
    }

    // Holds the row views so RecyclerView can reuse them efficiently.
    public static class WeightViewHolder extends RecyclerView.ViewHolder {
        TextView dateText;
        TextView weightText;
        Button deleteButton;

        public WeightViewHolder(@NonNull View itemView) {
            super(itemView);

            dateText = itemView.findViewById(R.id.dateText);
            weightText = itemView.findViewById(R.id.weightText);
            deleteButton = itemView.findViewById(R.id.deleteButton);
        }
    }

    // Creates the row layout for each weight entry.
    @NonNull
    @Override
    public WeightViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.weight_entry_row, parent, false);

        return new WeightViewHolder(view);
    }

    // Binds one WeightEntry object to one visible row.
    @Override
    public void onBindViewHolder(@NonNull WeightViewHolder holder, int position) {
        WeightEntry entry = items.get(position);

        holder.dateText.setText(entry.getDate());
        holder.weightText.setText(String.valueOf(entry.getWeight()));

        // The delete button removes the selected row from the database.
        holder.deleteButton.setOnClickListener(v ->
                onDeleteClickListener.onDeleteClick(entry.getId())
        );

        // Tapping the row loads it into the form for editing.
        holder.itemView.setOnClickListener(v ->
                onItemClickListener.onItemClick(entry)
        );
    }

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

    // Reloads the displayed data after add, update, or delete.
    public void updateData(List<WeightEntry> newItems) {
        items = newItems;

        // This is fine for a small project.
        // For a larger app, a more targeted update would be better.
        notifyDataSetChanged();
    }
}