package com.rong.io.live.helper;


import android.text.TextUtils;

import com.rong.io.live.bean.User;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Author by pq, Date on 2022/6/15.
 * 直播间连麦用户管理
 */
public class LiveRoomMicMemberHelper {
    private ArrayList<User> onApplyMicUsers;
    private HashMap<String, User> onMicUser;

    public LiveRoomMicMemberHelper() {
        onApplyMicUsers = new ArrayList<>();
        onMicUser = new HashMap<>();
    }

    /**
     * 添加连麦用户
     *
     * @param user
     */
    public void addApplyUser(User user) {
        int i = checkApplyExist(user);
        if (i == -1) {
            onApplyMicUsers.add(user);
        }
    }

    /**
     * 删除单个连麦用户
     *
     * @param user
     */
    public void delApplyUser(User user) {
        int i = checkApplyExist(user);
        if (i != -1) {
            onApplyMicUsers.remove(i);
        }
    }

    /**
     * 删除全部申请连麦用户
     */
    public void delAllApplyUser() {
        onApplyMicUsers.clear();
    }

    /**
     * 检查目标连麦用户是否已存在
     *
     * @param targetUser
     * @return
     */
    private int checkApplyExist(User targetUser) {
        if (targetUser == null) {
            return -1;
        }
        for (int i = 0; i < onApplyMicUsers.size(); i++) {
            User user1 = onApplyMicUsers.get(i);
            if (TextUtils.equals(user1.getUserId(), targetUser.getUserId())) {
                return i;
            }
        }
        return -1;
    }


    /**
     * 获取连麦申请中的用户
     *
     * @return
     */
    public ArrayList<User> getOnApplyMicUsers() {
        return onApplyMicUsers;
    }
}
