package thyyxxk.webserver.service.zhuyuanyisheng.emr;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.socketmessage.ApiMessageBody;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrSocketMessage;
import thyyxxk.webserver.service.externalhttp.WebSocketService;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import javax.websocket.Session;
import java.util.*;

@Service
@Slf4j
public class EmrWebSocketServer {
    private final RedisLikeService redisLikeService;
    private final WebSocketService socketService;
    private static final Map<String, List<EmrSocketMessage>> CHAT_ROOM = new HashMap<>();

    @Autowired
    public EmrWebSocketServer(RedisLikeService redisLikeService, WebSocketService socketService) {
        this.redisLikeService = redisLikeService;
        this.socketService = socketService;
    }

    public ResultVo<Map<String, UserInfo>> getRoomPeople(String sid) {
        ApiMessageBody messageBody = new ApiMessageBody();
        messageBody.setSid(sid);
        ResultVo<List<String>> response = socketService.fetchEmrSessions(messageBody);
        if (null == response || response.getCode() != ExceptionEnum.SUCCESS.getCode()) {
            return ResultVoUtil.success();
        }
        List<String> list = response.getData();
        if (list == null) {
            return ResultVoUtil.success();
        }
        Map<String, UserInfo> map = new HashMap<>(list.size());
        for (String item : list) {
            String code = item.split("-")[0];
            map.put(code, redisLikeService.getUserInfoByCode(code));
        }
        return ResultVoUtil.success(map);
    }

    public ResultVo<Boolean> repeatedlyOpenTheSamePatient(String sid) {
        ApiMessageBody messageBody = new ApiMessageBody();
        messageBody.setSid(sid);
        ResultVo<List<String>> response = socketService.fetchEmrSessions(messageBody);
        if (null == response || response.getCode() != ExceptionEnum.SUCCESS.getCode()) {
            return ResultVoUtil.success();
        }
        List<String> list = response.getData();
        if (list == null) {
            return ResultVoUtil.success(false);
        }
        String userCode = TokenUtil.getInstance().getTokenUserId();
        for (String item : list) {
            String code = item.split("-")[0];
            if (code.equals(userCode)) {
                return ResultVoUtil.success(true);
            }
        }
        return ResultVoUtil.success(false);
    }

    public static String sessionParseUserCode(Session session) {
        String query = session.getQueryString();
        return query.substring(query.indexOf("=") + 1);
    }

    public ResultVo<String> sendAMessage(String sid, String key, String message) {

        return ResultVoUtil.success();
    }

    public ResultVo<List<EmrSocketMessage>> getChatHistoryBySid(String sid) {
        return ResultVoUtil.success(CHAT_ROOM.get(sid));
    }

    public static void clearChatHistoryBySid(String sid) {
        CHAT_ROOM.remove(sid);
    }


}
