package com.cm.central.control.manager;

import com.cm.central.control.pojo.bos.SocketClientBO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: SocketClientManager
 * @Description: socket客户端管理
 * @Author: WangGeng
 * @Date: 2020/7/28 3:19 下午
 * @Version: 1.0
 **/
public class SocketServerManager {

    private static SocketServerManager socketServerManager = SocketClientManagerBuilder.SOCKET_SERVER_MANAGER;
    /**
     * 在线客户端，Key为通道ID
     */
    private Map<String, SocketClientBO> onLineClientMap = new ConcurrentHashMap<>();
    /**
     * 离线客户端，Key为ClientId
     */
    private Map<String, SocketClientBO> offLineClientMap = new ConcurrentHashMap<>();

    private SocketServerManager() {
    }

    public static SocketServerManager getInstance() {
        return socketServerManager;
    }

    /**
     * 加入Socket客户端
     *
     * @param socketClientBO
     */
    public void addClient(String token, SocketClientBO socketClientBO) {
        onLineClientMap.put(token, socketClientBO);
        offLineClientMap.remove(socketClientBO.getClientId());
    }

    /**
     * 获取Socket在线客户端（通过Token）
     *
     * @param token
     * @return
     */
    public SocketClientBO getOnlineClientByToken(String token) {
        return onLineClientMap.get(token);
    }

    /**
     * 获取Socket在线客户端（通过clientId）
     *
     * @param clientId
     * @return
     */
    public SocketClientBO getOnlineClientByClientId(String clientId) {
        for (Map.Entry<String, SocketClientBO> kv : onLineClientMap.entrySet()) {
            if (StringUtils.equals(clientId, kv.getValue().getClientId())) {
                return kv.getValue();
            }
        }
        return null;
    }

    /**
     * 获取Socket离线客户端（通过clientId）
     *
     * @param clientId
     * @return
     */
    public SocketClientBO getOfflineClientByClientId(String clientId) {
        return offLineClientMap.get(clientId);
    }

    /**
     * 删除Socket客户端
     *
     * @param token
     */
    public void removeOnlineClient(String token) {
        SocketClientBO socketClientBO = getOnlineClientByToken(token);
        if (socketClientBO != null) {
            // 加入离线队列，清空通知队列
            offLineClientMap.put(socketClientBO.getClientId(), socketClientBO);
        }
        onLineClientMap.remove(token);
    }

    /**
     * 判断客户端是否在线（已经登录）
     *
     * @param clientId
     * @return
     */
    public boolean isClientOnline(String clientId) {
        for (Map.Entry<String, SocketClientBO> kv : onLineClientMap.entrySet()) {
            if (StringUtils.equals(clientId, kv.getValue().getClientId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断客户端是否离线
     *
     * @param clientId
     * @return
     */
    public boolean isClientOffLine(String clientId) {
        SocketClientBO socketClientBO = getOfflineClientByClientId(clientId);
        if (socketClientBO != null) {
            return true;
        }
        return false;
    }

    /**
     * 离线客户端列表
     *
     * @return
     */
    public List<SocketClientBO> listOffLineClient() {
        List<SocketClientBO> socketClientBOs = new ArrayList<>();
        for (Map.Entry<String, SocketClientBO> kv : offLineClientMap.entrySet()) {
            socketClientBOs.add(kv.getValue());
        }
        return socketClientBOs;
    }

    private static class SocketClientManagerBuilder {
        public static final SocketServerManager SOCKET_SERVER_MANAGER = new SocketServerManager();
    }

}
