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 HistoryQuestionAdapter extends RecyclerView.Adapter<HistoryQuestionAdapter.ViewHolder>{
    private List<Question> historyQuestionList;
    private Context context;

    //重写构造方法
    public HistoryQuestionAdapter(List<Question> historyQuestionList, Context context){
        this.historyQuestionList = historyQuestionList;
        this.context = context;
    }
    public int getItemCount(){
        return historyQuestionList.size();
    }
    //内部类
    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView questionTitle,questionInfo,questionRead,questionTime,question_id;
        public ViewHolder(@NonNull View itemView){
            super(itemView);
            this.question_id = itemView.findViewById(R.id.question_id);
            this.questionTitle = itemView.findViewById(R.id.questionTitle);
            this.questionInfo = itemView.findViewById(R.id.questionInfo);
            this.questionRead = itemView.findViewById(R.id.questionRead);
            this.questionTime = itemView.findViewById(R.id.questionTime);
        }
    }
    //重写 onCreateViewHolder()方法
    @Override
    public HistoryQuestionAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.question_item,parent,false);
        HistoryQuestionAdapter.ViewHolder holder=new HistoryQuestionAdapter.ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int qid = Integer.parseInt(holder.question_id.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 ViewHolder holder,int position){
        Question question = historyQuestionList.get(position);
        holder.question_id.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()));
    }
}


