package com.navinfo.outdoor.adapter;

import android.content.Context;
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.navinfo.outdoor.R;
import com.navinfo.outdoor.bean.TaskPrefectureBean;

import org.jetbrains.annotations.NotNull;

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

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
    private Context context;
    private List<TaskPrefectureBean.BodyBean.ListBean> messageList = new ArrayList<>();

    public void setMessageList(List<TaskPrefectureBean.BodyBean.ListBean> messageList) {
        this.messageList.addAll(messageList);
        notifyDataSetChanged();
    }

    public MessageAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.message_item, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        holder.tvMessageTitle.setText(messageList.get(position).getTitle());
        holder.tvMessageDest.setText(messageList.get(position).getSubtitle());
        holder.tvMessageTime.setText(messageList.get(position).getCreatetime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (messageClickItem!=null){
                    messageClickItem.item(messageList.get(position).getId());
                }

            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvMessageTitle;
        private TextView tvMessageDest;
        private TextView tvMessageTime;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            tvMessageTitle = itemView.findViewById(R.id.tv_message_title);
            tvMessageDest = itemView.findViewById(R.id.tv_message_dest);
            tvMessageTime = itemView.findViewById(R.id.tv_message_time);
        }
    }
    public messageClickItem messageClickItem;

    public void setMessageClickItem(MessageAdapter.messageClickItem messageClickItem) {
        this.messageClickItem = messageClickItem;
    }

    public interface messageClickItem{
        void item(int aInt);
    }
}
