package com.bokecc.livemodule.live.function.redpacket.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bokecc.dwlivedemo.R;
import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.pojo.RedPacketRankInfo;

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

/**
 * 随堂测统计柱状图列表适配器
 */
public class RedPacketRankAdapter extends RecyclerView.Adapter<RedPacketRankAdapter.RankViewHolder> {

    private Context mContext;
    private List<RedPacketRankInfo.RedPacketRankBean> redPacketRankBeans;
    private LayoutInflater mInflater;

    public RedPacketRankAdapter(Context context) {
        redPacketRankBeans = new ArrayList<>();
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    /**
     * 添加数据
     */
    public void add(List<RedPacketRankInfo.RedPacketRankBean> redPacketRankBeans) {
        this.redPacketRankBeans = redPacketRankBeans;
        notifyDataSetChanged();
    }

    @Override
    public RankViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.red_packet_rank_item, parent, false);
        return new RankViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RankViewHolder holder, int position) {

        RedPacketRankInfo.RedPacketRankBean redPacketRankBean = redPacketRankBeans.get(position);
        if (position == 0){
            holder.mIvRank.setVisibility(View.VISIBLE);
            holder.mIvRank.setImageResource(R.mipmap.red_packet_gold);
        }else  if (position == 1){
            holder.mIvRank.setVisibility(View.VISIBLE);
            holder.mIvRank.setImageResource(R.mipmap.red_packet_silver);
        }else  if (position == 2){
            holder.mIvRank.setVisibility(View.VISIBLE);
            holder.mIvRank.setImageResource(R.mipmap.red_packet_copper);
        }else{
            holder.mIvRank.setVisibility(View.INVISIBLE);
        }
        String selfId = "";
        if (DWLive.getInstance().getViewer()!=null){
            selfId = DWLive.getInstance().getViewer().getId();
        }
        if (redPacketRankBean.getUserId().equals(selfId)){
            holder.mUserName.setTextColor(Color.parseColor("#F25642"));
            holder.mUserName.setText(redPacketRankBean.getUserName()+"(我)");
        }else{
            holder.mUserName.setTextColor(Color.parseColor("#333333"));
            if (!TextUtils.isEmpty(redPacketRankBean.getUserName()))
                holder.mUserName.setText(redPacketRankBean.getUserName());
        }
        holder.mCredit.setText(String.valueOf(redPacketRankBean.getAmount()));

    }

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

    final class RankViewHolder extends RecyclerView.ViewHolder {

        TextView mUserName;

        ImageView mIvRank;

        TextView mCredit;

        RankViewHolder(View itemView) {
            super(itemView);
            mUserName = itemView.findViewById(R.id.tv_rank_name);
            mIvRank = itemView.findViewById(R.id.iv_rank);
            mCredit = itemView.findViewById(R.id.tv_rank_credit);
        }
    }
}