package com.sjyt.icom.asst.common.model.room;

import cn.hutool.core.util.ObjectUtil;
import com.sjyt.icom.asst.common.model.message.News;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.*;

/**
 * @author: Tab
 * @date: 2022-06-12 23:14
 * @since 1.0.0
 */
@Data
@Slf4j
public abstract class AbsRoom implements Serializable {
    private static final long serialVersionUID = 1220044544122343234L;
    /**
     * 房间号
     */
    private String rid;

    /**
     * 房 间 类型
     */
    private String type;

    /**
     * 房间限定人数
     */
    private int quota;

    /**
     * 客户端集合
     */
    private final List<AbsClient> clientList =new ArrayList<>();

    /**
     * 池化对象标识
     */
    private boolean active;

    public void destroy(){
        active=false;
        log.info("房间销毁,房间类型为：{}",type);
    }

    public void register(AbsClient client) {
        if (ObjectUtil.isNotNull(client) && !clientList.contains(client)){
            clientList.add(client);
        }
    }

    public void nullify(AbsClient client){
        if (ObjectUtil.isNotNull(client)){
            clientList.remove(client);
        }
    }

    public void notice(AbsClient client, News news) {
        for (AbsClient chatClient : clientList) {
            if (!client.equals(chatClient)){
                client.receive(news);
            }
        }
    }
}
