package com.sjyt.icom.asst.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sjyt.icom.asst.common.enums.MsgTypeEnum;
import com.sjyt.icom.asst.common.enums.RoomEnum;
import com.sjyt.icom.asst.common.enums.SrsCallBackEnum;
import com.sjyt.icom.asst.common.event.SrsCallBackEvent;
import com.sjyt.icom.asst.common.model.message.News;
import com.sjyt.icom.asst.common.model.message.VerifyClaims;
import com.sjyt.icom.asst.common.model.room.AbsClient;
import com.sjyt.icom.asst.common.model.room.AbsRoom;
import com.sjyt.icom.asst.common.model.room.SrsUrlObj;
import com.sjyt.icom.asst.common.model.sdp.RespApply;
import com.sjyt.icom.asst.context.handle.room.RoomFactory;
import com.sjyt.icom.asst.endpoint.BeanResolvable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.yeauty.pojo.Session;

import java.util.List;
import java.util.Optional;

/**
 * @author: Tab
 * @date: 2022-06-12 23:27
 * @since 1.0.0
 */
@Slf4j
@Service
public class RoomManageService extends BaseRoomManagerService<SrsCallBackEvent> {
    @Autowired
    public RoomManageService(RoomFactory roomFactory, BeanResolvable beanResolvable) {
        super(roomFactory, beanResolvable);
    }

    /**
     * 离开房间
     *
     * @param srsCallBackEvent t
     */
    @EventListener
    public void leave(SrsCallBackEvent srsCallBackEvent) {
        int type = srsCallBackEvent.getEventType();
        if (type == SrsCallBackEnum.CLOSE.getCode() ||
                type == SrsCallBackEnum.STOP.getCode() ||
                type == SrsCallBackEnum.UN_PUBLISH.getCode()
        ) {
            final VerifyClaims claims = srsCallBackEvent.getClaims();
            final String rid = claims.getRid();
            final String uid = claims.getFrom();
            final AbsRoom room = getRoom(rid);
            Optional.ofNullable(room).ifPresent(it->{

            });
            //离开通知
            Optional.ofNullable(rNum).map(it -> it.toArray(new String[0])).ifPresent(sendMsgService::sendForLeave);
            getRoomMap().remove(rid);
        }

    }

    /**
     * 加入房间
     *
     * @param srsCallBackEvent t
     */
    @EventListener
    public void join(SrsCallBackEvent srsCallBackEvent) {
        final VerifyClaims claims = srsCallBackEvent.getClaims();
        final String roomCode = claims.getRoomCode();
        final String from = claims.getFrom();
        final String to = claims.getTo();
        final String rid = claims.getRid();
        final String cmdCode = claims.getCmdCode();
        final int type = srsCallBackEvent.getEventType();
        if (type == SrsCallBackEnum.PUBLISH.getCode()) {
            log.info("加入房间");
            //一对一
            if (roomCode.equals(RoomEnum.ONE2ONE.CODE)) {
                List<ExtraInfo> extraInfos = getExtraInfo(rid, from);
                if (extraInfos.size() == 0) {
                    final AbsRoom absRoom = getRoom(rid);
                    final ExtraInfo extraInfo = absRoom.getExtraInfo(from);
                    final News news = sendMsgService.newsLetter(from, to, cmdCode);
                    final SrsUrlObj urlObj = extraInfo.getUrlObj();
                    final RespApply apply = RespApply.builder().msgType(MsgTypeEnum.RespApply.getCODE()).rid(rid).urlObj(urlObj).build();
                    news.getMessage().setBody(BeanUtil.beanToMap(apply));
                    sendMsgService.send2To(news);
                } else {
                    sendMsgService.sendForJoin(extraInfos.toArray(new ExtraInfo[0]));
                }
            }
            //其他
            if (roomCode.equals(RoomEnum.MEET.CODE)) {
                List<ExtraInfo> extraInfos = getExtraInfo(rid, from);
                sendMsgService.sendForJoin(extraInfos.toArray(new ExtraInfo[0]));
                System.out.println("加入房间");
            }
        }
    }

    public AbsRoom create(RoomEnum roomEnum) {
        return null;
    }


    public void addClient(String rid, AbsClient client) {
        final AbsRoom absRoom = roomMap.get(rid);
        if (ObjectUtil.isNotNull(absRoom)) {
            absRoom.register(client);
        }
    }

    public void removeClient(String rid, AbsClient client) {
        final AbsRoom absRoom = roomMap.get(rid);
        if (ObjectUtil.isNotNull(absRoom)) {
            absRoom.nullify(client);
        }
    }

    public Session getSession(String uid){
        return getBeanResolvable().getSession(uid);
    }
}
