package com.example.leudaemialikeme.Adapter;

import android.content.Context;
import android.content.Intent;
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.Activity.QuestionDetailActivity;
import com.example.leudaemialikeme.Model.MyAnswerItemView;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.TimeUtil;

import java.util.List;

public class MyAnswerAdapter extends RecyclerView.Adapter<MyAnswerAdapter.ViewHolder>{
    private List<MyAnswerItemView> answerList;
    private Context context;

    public MyAnswerAdapter(List<MyAnswerItemView> answerList, Context context) {
        this.answerList = answerList;
        this.context = context;
    }
    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView text_answer_qid;
        private TextView text_my_answer_title;
        private TextView text_my_answer;
        private TextView text_answer_time;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text_answer_qid = itemView.findViewById(R.id.text_answer_qid);
            text_my_answer_title = itemView.findViewById(R.id.text_my_answer_title);
            text_my_answer = itemView.findViewById(R.id.text_my_answer);
            text_answer_time = itemView.findViewById(R.id.text_answer_time);
        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.my_answer_item,parent,false);
        MyAnswerAdapter.ViewHolder holder=new MyAnswerAdapter.ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, QuestionDetailActivity.class);
                intent.putExtra("qid", Integer.parseInt(holder.text_answer_qid.getText().toString()));
                context.startActivity(intent);
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MyAnswerItemView answer = answerList.get(position);
        holder.text_answer_qid.setText(String.valueOf(answer.getQid()));
        holder.text_my_answer_title.setText(answer.getQtitle());
        holder.text_my_answer.setText(answer.getAcontent());
        holder.text_answer_time.setText(TimeUtil.timeToString(answer.getAnswerTime()));
    }


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