package thyyxxk.webserver.service.zhuyuanyisheng.emr;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
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.zhuyuanyisheng.emr.EmrSocketParam;
import thyyxxk.webserver.service.TokenService;
import thyyxxk.webserver.service.externalhttp.WebSocketService;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import javax.websocket.Session;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
public class EmrWebSocketServiceV2 {
    @Data
    public static class Edit {
        private Session session;
        private String userCode;
    }

    private final WebSocketService socketService;
    private final RedisLikeService redisLikeService;
    private static final ConcurrentHashMap<String, Edit> EDITOR_USER = new ConcurrentHashMap<>();

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

    public ResultVo<UserInfo> isBeingEditing(String documentId) {
        ResultVo<String> userCode = socketService.isBeingEditing(documentId);
        if (userCode.getCode() != ExceptionEnum.SUCCESS.getCode()) {
            return ResultVoUtil.success();
        }
        UserInfo us = redisLikeService.getUserInfoByCode(userCode.getData());
        return ResultVoUtil.success(us);
    }

    public static void open(String sid, Session session) {
        EmrSocketParam param = parameterParsing(sid);
        Edit edit = new Edit();
        edit.setSession(session);
        edit.setUserCode(param.getUserCode());
        EDITOR_USER.put(param.getId(), edit);
    }

    public static void close(String sid) {
        EmrSocketParam param = parameterParsing(sid);
        EDITOR_USER.remove(param.getId());
    }

    public static EmrSocketParam parameterParsing(String sid) {
        EmrSocketParam param = new EmrSocketParam();
        String[] sids = sid.split("_");
        param.setSid(sid)
                .setId(sids[1])
                .setUserCode(sids[2]);
        return param;
    }

    public ResultVo<String> forcedKickingOutOfPersonnelByDocumentId(String documentId) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("documentId", documentId);
            jsonObject.put("userInfo", redisLikeService.getUserInfoByToken());
            ResultVo<String> res = socketService
                    .forcedKickingOutOfPersonnelByDocumentId(jsonObject);
            if (ExceptionEnum.SUCCESS.getCode() == res.getCode()) {
                return ResultVoUtil.success();
            }
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "强制踢出失败，请重试。");
        }
        return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "强制踢出失败，请重试。");
    }

}
