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

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

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

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

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

    /**
     * 用户信息
     */
    private final Set<ExtraInfo> extraInfos = new HashSet<>(16);

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

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

    public Room addExtraInfo(ExtraInfo extraInfo){
        extraInfos.add(extraInfo);
        return this;
    }

    /**
     *
     * @param uid uid
     * @return ExtraInfo
     */
    public ExtraInfo getExtraInfo(String uid){
        return Optional.ofNullable(uid)
                .flatMap(id -> extraInfos.stream()
                        .filter(it -> it.getUid().equals(uid))
                        .findFirst())
                .orElse(null);
    }

    /**
     * 返回StreamUrlSrs
     * @param uid uid
     * @return SrsUrlObj
     */
    public Optional<SrsUrlObj> getStreamUrlForUid(String uid){
        return extraInfos.stream().filter(it -> ObjectUtil.equal(it.getUid(), uid))
                .map(ExtraInfo::getUrlObj).findFirst();
    }
}
