package com.example.quiz.data.adapter;


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

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

import com.example.quiz.R;
import com.example.quiz.data.model.Course;
import com.example.quiz.viewModel.SharedViewModel;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class BetsAdapter extends RecyclerView.Adapter<BetsAdapter.BetViewHolder> {

    private List<Course> bets = new ArrayList<>();
    private onItemClickListener listener;

    public BetsAdapter() {
    }

    public interface onItemClickListener {
        void onItemClick(Course bet);
    }

    public void setOnItemClickListener(onItemClickListener listener){
        this.listener = listener;
    }

    public void setBets(List<Course> bets){
        this.bets = bets;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull BetViewHolder holder, int position) {
        Course bet = bets.get(position);
        OffsetDateTime dateTime = OffsetDateTime.parse(bet.getHeureDepartPrevue());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
        String formattedDate = dateTime.format(formatter);
        holder.tvDate.setText(formattedDate);
        holder.tvName.setText(bet.getNom());

        String reunion = "Reunion "+bet.getReunionNumero();
        holder.tvReunion.setText(reunion);

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) listener.onItemClick(bet);
        });
    }

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

    static class BetViewHolder extends RecyclerView.ViewHolder{
        TextView tvName, tvDate, tvReunion;

        public BetViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvDate = itemView.findViewById(R.id.tvDate);
            tvReunion = itemView.findViewById(R.id.tvReunion);
        }
    }
}
