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

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

public class MyBlogAdapter extends RecyclerView.Adapter<MyBlogAdapter.ViewHolder>{
    List<Blog> blogList = new ArrayList<>();

    public MyBlogAdapter(List<Blog> blogList){
        this.blogList = blogList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView text_my_blog_id;
        private TextView text_my_blog_title;
        private TextView text_my_blog_content;
        private TextView text_my_blog_browse;
        private TextView text_my_blog_time;
        private TextView text_my_blog_flag;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text_my_blog_id = itemView.findViewById(R.id.text_my_blog_id);
            text_my_blog_title = itemView.findViewById(R.id.text_my_blog_title);
            text_my_blog_content = itemView.findViewById(R.id.text_my_blog_content);
            text_my_blog_browse = itemView.findViewById(R.id.text_my_blog_browse);
            text_my_blog_time = itemView.findViewById(R.id.text_my_blog_time);
            text_my_blog_flag = itemView.findViewById(R.id.text_my_blog_flag);
        }
    }

    @NonNull
    @Override
    public MyBlogAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.my_blog_item,parent,false);
        MyBlogAdapter.ViewHolder holder = new MyBlogAdapter.ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 跳转到帖子的详情页
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyBlogAdapter.ViewHolder holder, int position) {
        Blog blog = blogList.get(position);
        holder.text_my_blog_id.setText(String.valueOf(blog.getBid()));
        holder.text_my_blog_title.setText(blog.getBtittle());
        holder.text_my_blog_content.setText(blog.getBcontent());
        holder.text_my_blog_browse.setText(String.valueOf(blog.getBbrowse()));
        holder.text_my_blog_time.setText(TimeUtil.timeToString(blog.getBtime()));
        if(blog.getFlag()==1)
            holder.text_my_blog_flag.setText("已审核");
    }

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