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

import java.util.List;

public class MyQuestionAdapter extends RecyclerView.Adapter<MyQuestionAdapter.ViewHolder>{
    private List<Question> questionList;
    private Context context;
    //重写构造方法
    public MyQuestionAdapter(List<Question> questionList, Context context){
        this.questionList = questionList;
        this.context = context;
    }
    //内部类
    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView text_my_question_id;
        private TextView text_my_question_title;
        private TextView text_my_question_browse;
        private TextView text_my_question_time;
        private TextView text_my_question_flag;

        public ViewHolder(@NonNull View itemView){
            super(itemView);
            text_my_question_id = itemView.findViewById(R.id.text_my_question_id);
            text_my_question_title = itemView.findViewById(R.id.text_my_question_title);
            text_my_question_browse = itemView.findViewById(R.id.text_my_question_browse);
            text_my_question_time = itemView.findViewById(R.id.text_my_question_time);
            text_my_question_flag = itemView.findViewById(R.id.text_my_question_flag);

        }
    }
    //重写 onCreateViewHolder()方法
    @Override
    public MyQuestionAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.my_question_item,parent,false);
        MyQuestionAdapter.ViewHolder holder=new MyQuestionAdapter.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_my_question_id.getText().toString()));
                context.startActivity(intent);
            }
        });
        return holder;
    }
    //重写onBindViewHolder()方法
    @Override
    public void onBindViewHolder(@NonNull MyQuestionAdapter.ViewHolder holder, int position){
        Question question = questionList.get(position);
        holder.text_my_question_id.setText(String.valueOf(question.getQid()));
        holder.text_my_question_title.setText(question.getQtittle());
        holder.text_my_question_browse.setText(String.valueOf(question.getQbrowseNum()));
        holder.text_my_question_time.setText(TimeUtil.timeToString(question.getQtime()));
        if(question.getFlag()==1)
            holder.text_my_question_flag.setText("已审核");
    }

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

