package com.example.leudaemialikeme.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.leudaemialikeme.Model.AnswerItemView;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

public class QuestionDetailAdapter extends RecyclerView.Adapter<QuestionDetailAdapter.ViewHolder>{
    List<AnswerItemView> answerList = new ArrayList<>();
    public QuestionDetailAdapter(List<AnswerItemView> answerList){
        this.answerList = answerList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView text_answer_username;
        private TextView text_answer_content;
        private TextView text_answer_time;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text_answer_username = itemView.findViewById(R.id.text_answer_username);
            text_answer_content = itemView.findViewById(R.id.text_answer_content);
            text_answer_time = itemView.findViewById(R.id.text_answer_time);
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull QuestionDetailAdapter.ViewHolder holder, int position) {
        AnswerItemView answer = answerList.get(position);
        holder.text_answer_username.setText(answer.getAnswerAuthor());
        holder.text_answer_content.setText(answer.getAnswerContent());
        holder.text_answer_time.setText(TimeUtil.timeToString(answer.getAnswerTime()));
    }

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