package com.rong.io.live.helper;


import android.text.TextUtils;
import android.util.Log;

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 {
    public static final String NICK_NAME_DEFAULT = "连麦用户";
    private OnEventListener mEventListener;
    private ArrayList<User> onApplyMicUsers;
    private ArrayList<User> onMicUser;
    private ArrayList<User> userCacheList;

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

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

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

    /**
     * 删除单个连麦用户
     *
     * @param
     */
    public void delOnMicUser(String userId) {
        User user = new User();
        user.setUserId(userId);
        int i = checkOnMicExist(user);
        delUserCache(user);
        if (i != -1) {
            onMicUser.remove(i);
        }
    }

    public void delApplyUser(String userId) {
        User user = new User();
        user.setUserId(userId);
        delApplyUser(user, false);
    }


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

    /**
     * 删除全部申请连麦用户缓存信息
     */
    public void delAllUserCache() {
        userCacheList.clear();
    }


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

    /**
     * 删除单个连麦用户
     *
     * @param user
     */
    public void delApplyUser(User user, boolean isCache) {
        int i = checkApplyExist(user);
        if (i != -1) {
            if (isCache) {
                User user1 = onApplyMicUsers.get(i);
                Log.i("pq", "添加userCache" + user1.getUserId());
                userCacheList.add(user1);
                Log.i("pq", "添加userCache" + userCacheList.size());
            }
            Log.i("pq", "del apply user:" + i);
            onApplyMicUsers.remove(i);
        }else{
            Log.i("pq", "no find target user");
        }
    }

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

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

    /**
     * 检查目标连麦用户是否已存在
     *
     * @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;
    }

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


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

    public ArrayList<User> getOnMicUsers() {
        return onMicUser;
    }

    public void setOnEventListener(OnEventListener listener) {
        this.mEventListener = listener;
    }


    private void delUserCache(User targetUser) {
        if (targetUser == null) {
            return;
        }
        if (userCacheList != null && userCacheList.size() > 0) {
            for (int i = 0; i < userCacheList.size(); i++) {
                User user = userCacheList.get(i);
                if (TextUtils.equals(user.getUserId(), targetUser.getUserId())) {
                    Log.i("pq", "删除userCache" + userCacheList.size());
                    userCacheList.remove(user);
                }
            }
        }
    }

    public User createUser(String userId) {
        Log.i("pq", "缓存userCacheList" + userCacheList.size());
        if (userCacheList != null && userCacheList.size() > 0) {
            for (int i = 0; i < userCacheList.size(); i++) {
                User user = userCacheList.get(i);
                if (TextUtils.equals(user.getUserId(), userId)) {
                    Log.i("pq", "命中缓存userCache" + user);
                    return user;
                }
            }
        }
        User user = new User();
        user.setUserId(userId);
        user.setUserName(NICK_NAME_DEFAULT);
        if (mEventListener != null) {
            Log.i("pq", "未命中缓存,获取用户信息" + user.getUserId());
            mEventListener.getMicUserInfo(userId);
        }
        return user;
    }

    public void refreshUserInfo(User user) {
        Log.i("pq", "refreshUserInfo:" + user.getUserId());
        Log.i("pq", "refreshUserInfo:" + user.getUserName());
        Log.i("pq", "refreshUserInfo:" + user.getPortraitUrl());
        int i = checkOnMicExist(user);
        Log.i("pq", "刷新麦上用户缓存:" + i);
        if (i != -1) {
            onMicUser.set(i, user);
        }
        int k = checkUserCacheExist(user);
        Log.i("pq", "刷新用户缓存:" + i);
        if (k != -1) {
            userCacheList.set(k, user);
        }
    }

    public interface OnEventListener {
        void getMicUserInfo(String userId);
    }
}
