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 androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.rdiot.yx485.R;
import com.rdiot.yx485.bean.HomeResultBean.HomeInfo.MemberBean;
import com.rdiot.yx485.databinding.ItemPersonListBinding;
import com.rdiot.yx485.http.Url;

import java.util.List;

public class HomePersonAdapter extends RecyclerView.Adapter<HomePersonAdapter.DeviceViewHolder> {

    private final List<MemberBean> mMemberBeanList;
    private final OnMemberClickListener mOnMemberClickListener;
    private Context mContext;
    private String mAdminId;

    public HomePersonAdapter(List<MemberBean> mMemberBeanList, OnMemberClickListener listener, String adminId) {
        this.mMemberBeanList = mMemberBeanList;
        mOnMemberClickListener = listener;
        mAdminId = adminId;
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        ItemPersonListBinding binding = ItemPersonListBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new DeviceViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.bindView(position);
    }

    @Override
    public int getItemCount() {
        return mMemberBeanList == null ? 1 : mMemberBeanList.size() + 1;
    }

    public interface OnMemberClickListener {

        void onMemberClick(MemberBean member);

        void onAddMemberClick();

    }

    class DeviceViewHolder extends RecyclerView.ViewHolder {
        private final ItemPersonListBinding mBinding;

        public DeviceViewHolder(@NonNull ItemPersonListBinding itemView) {
            super(itemView.getRoot());
            mBinding = itemView;
        }

        public void bindView(int position) {
            if (position >= mMemberBeanList.size()) {
                mBinding.pername.setText("邀请家人");
                mBinding.image.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.round_add_40));
                mBinding.getRoot().setOnClickListener(v -> {
                    if (mOnMemberClickListener != null) {
                        mOnMemberClickListener.onAddMemberClick();
                    }
                });
                return;
            }

            MemberBean memberBean = mMemberBeanList.get(position);
            if (!TextUtils.isEmpty(memberBean.user_name)) {
                mBinding.pername.setText(memberBean.user_name);
            }

            if (!TextUtils.isEmpty(memberBean.photo)) {
                Glide.with(mContext)
                        .load(Url.BASE_URL + memberBean.photo)
                        .error(Glide.with(mContext).load(R.drawable.tx))
                        .into(mBinding.image);
            }

            mBinding.tag.setVisibility(memberBean.user_id.equals(mAdminId) ? View.VISIBLE : View.GONE);
            mBinding.getRoot().setOnClickListener(v -> {
                if (mOnMemberClickListener != null) {
                    mOnMemberClickListener.onMemberClick(memberBean);
                }
            });
        }
    }
}