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.BaseActivity;
import com.example.leudaemialikeme.Activity.LoginActivity;
import com.example.leudaemialikeme.Activity.QuestionDetailActivity;
import com.example.leudaemialikeme.Model.Question;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.OkHttpUtil;
import com.example.leudaemialikeme.Utils.TimeUtil;

import java.util.List;

public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.ViewHolder>{
    private List<Question> questionList;
    private Context context;
    //重写构造方法
    public QuestionAdapter(List<Question> questionList, Context context){
        this.questionList = questionList;
        this.context = context;
    }
    public QuestionAdapter(){

    }

    public int getItemCount(){
        return questionList.size();
    }
    //内部类
    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView questionId, questiontitle,questionInfo,questionRead,questionTime;

        public ViewHolder(@NonNull View itemView){
            super(itemView);
            this.questionId = itemView.findViewById(R.id.question_id);
            this.questiontitle = itemView.findViewById(R.id.questionTitle);
            this.questionInfo = (TextView)itemView.findViewById(R.id.questionInfo);
            this.questionRead = (TextView)itemView.findViewById(R.id.questionRead);
            this.questionTime = (TextView)itemView.findViewById(R.id.questionTime);
        }
    }

    public void setQuestionList(List<Question> questionList){
        this.questionList=questionList;
    }
    //重写 onCreateViewHolder()方法
    @Override
    public QuestionAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.question_item,parent,false);
        QuestionAdapter.ViewHolder holder=new QuestionAdapter.ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int qid = Integer.parseInt(holder.questionId.getText().toString());
                addQuestionBrowse(qid);
                Intent intent = new Intent(context, QuestionDetailActivity.class);
                intent.putExtra("qid", qid);
                context.startActivity(intent);
            }
        });
        return holder;
    }

    private void addQuestionBrowse(int qid) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url = LoginActivity.SERVER_URL+"/main-servlet?action=addQuestionBrowse&qid="+qid+"&iduser="+ BaseActivity.owner.getNetId();
                    OkHttpUtil.synGet(url);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //重写onBindViewHolder()方法
    @Override
    public void onBindViewHolder(@NonNull QuestionAdapter.ViewHolder holder, int position){
        Question question = questionList.get(position);
        holder.questionId.setText(String.valueOf(question.getQid()));
        holder.questiontitle.setText(question.getQtittle());
        holder.questionInfo.setText(question.getQcontent());
        holder.questionRead.setText(String.valueOf(question.getQbrowseNum()));
        holder.questionTime.setText(TimeUtil.timeToString(question.getQtime()));
    }
}
