package com.daya.live_teaching.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooleshow.base.utils.GlideImageLoaderUtils;
import com.daya.live_teaching.R;
import com.daya.live_teaching.model.ClassMember;
import com.daya.live_teaching.ui.base.BaseRecyclerAdapter;
import com.daya.live_teaching.ui.base.BaseViewHolder;

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

import androidx.annotation.NonNull;

/**
 * Description:
 * Copyright  : Copyright (c) 2019
 * Company    : 大雅乐盟
 * Author     : r
 * Date       : 2020/6/9 10:43
 */
public class OnlineMembersListAdapter extends BaseRecyclerAdapter<OnlineMembersListAdapter.ViewHolder> {

    private Context mContext;
    List<ClassMember> menberList = new ArrayList<>();

    public OnlineMembersListAdapter(Context context) {
        this.mContext = context;
    }

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

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

    public void setData(List<ClassMember> classMembers) {
        this.menberList.clear();
        this.menberList.addAll(classMembers);
        notifyDataSetChanged();
    }

    public class ViewHolder extends BaseViewHolder {
        ImageView ivPortrait;
        TextView  tvName;
        ImageView ivMic;
        ImageView  iv_raise_hands;

        public ViewHolder(View itemView) {
            super(itemView);
            ivPortrait = itemView.findViewById(R.id.iv_portrait);
            tvName = itemView.findViewById(R.id.tv_name);
            ivMic = itemView.findViewById(R.id.iv_mic);
            iv_raise_hands = itemView.findViewById(R.id.iv_raise_hands);
        }
        @Override
        protected void onBind(int position) {
            ClassMember member = menberList.get(position);
            GlideImageLoaderUtils.getInstance().loadCircleImage(mContext, member.getHeadUrl(), ivPortrait);
            tvName.setText(member.getUserName());
            ivMic.setImageResource(member.isMicrophone() ? R.drawable.ic_memeber_mic_on : R.drawable.ic_memeber_mic_off);
            iv_raise_hands.setVisibility(member.isHandUpOn() ? View.VISIBLE : View.GONE);
            ivMic.setOnClickListener(view -> {
                ivMic.setImageResource(member.isMicrophone() ? R.drawable.ic_memeber_mic_on : R.drawable.ic_memeber_mic_off);
                onSubViewClickListener.onSubViewClick(!member.isMicrophone() ? 0 : 1, member.getUserId());
            });
        }
    }

    public OnSubViewClickListener onSubViewClickListener;

    public interface OnSubViewClickListener {
        void onSubViewClick(int type, String userId);
    }


    public void setOnSubViewClickListener(OnSubViewClickListener listener) {
        this.onSubViewClickListener = listener;
    }
}
