package com.rdiot.yx485.adapter;

import android.content.Context;
import android.text.TextUtils;
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.rdiot.yx485.R;
import com.rdiot.yx485.bean.MessageListBean;
import com.rdiot.yx485.webview.WebViewActivity;

import java.util.List;

public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.ViewHolder> {

    public List<MessageListBean.DataBean.MessageInfo> mData;
    private Context mContext;
    private OnMessageReadListener mReadListener;

    public MessageListAdapter(Context context, List<MessageListBean.DataBean.MessageInfo> data) {
        mContext = context;
        this.mData = data;
    }

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


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (!TextUtils.isEmpty(mData.get(position).title)) {
            holder.name.setText(mData.get(position).title);
        }
        if (!TextUtils.isEmpty(mData.get(position).content)) {
            holder.message_content.setText(mData.get(position).content);
        }
        if (!TextUtils.isEmpty(mData.get(position).created_at)) {
            holder.message_date.setText(mData.get(position).created_at);
        }
        if ("1".equals(mData.get(position).status)) {
            holder.message_unread.setVisibility(View.VISIBLE);
        } else {
            holder.message_unread.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(mData.get(position).detail_url)) {
            holder.itemView.setOnClickListener(v -> {
                        if (mReadListener != null) {
                            mReadListener.onMessageRead(mData.get(position).record_id);
                        }
                        WebViewActivity.newIntent(mContext, mData.get(position).detail_url, null);
                    }

            );
        }
    }

    @Override
    public int getItemCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    public void setReadListener(OnMessageReadListener readListener) {
        mReadListener = readListener;
    }

    public interface OnMessageReadListener {
        void onMessageRead(String messageId);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView name;
        TextView message_unread;
        TextView message_date;
        TextView message_content;

        ViewHolder(View itemView) {
            super(itemView);
            message_date = itemView.findViewById(R.id.message_date);
            name = itemView.findViewById(R.id.message_title);
            message_unread = itemView.findViewById(R.id.message_unread);
            message_content = itemView.findViewById(R.id.message_content);
        }
    }
}