package com.bokecc.livemodule.live.function.lottery.view;

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.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.utils.ImageLoader;
import com.bokecc.livemodule.view.HeadView;
import com.bokecc.sdk.mobile.live.pojo.LotteryUserInfo;

import java.util.List;

public class LotteryUserAdapter extends RecyclerView.Adapter<LotteryUserAdapter.UserHolder>  {
    private final LayoutInflater mInflater;
    private List<LotteryUserInfo> users;
    private final Context mContext;

    public LotteryUserAdapter(Context context, List<LotteryUserInfo> users) {
        this.users = users;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setUsers(List<LotteryUserInfo> users) {
        this.users = users;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public UserHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new UserHolder(mInflater.inflate(R.layout.item_lottery_user, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull UserHolder userHolder, int i) {
        ImageLoader.loadIntoImageView(mContext,users.get(i).getUserAvatar(),userHolder.mUserHeadView,R.drawable.head_view_publisher,R.drawable.head_view_publisher);
        if (TextUtils.isEmpty(users.get(i).getUserName())){
            userHolder.userName.setText("未知");
        }else{
            userHolder.userName.setText(users.get(i).getUserName());
        }
    }

    @Override
    public int getItemCount() {
        return users == null?0:users.size();
    }

    public class UserHolder extends RecyclerView.ViewHolder {
        public TextView userName ;
        public HeadView mUserHeadView;
        public UserHolder(@NonNull View itemView) {
            super(itemView);
            mUserHeadView = itemView.findViewById(R.id.hv_user_portrait);
            userName = itemView.findViewById(R.id.tv_username);
        }
    }
}
