package thyyxxk.webserver.controller.zhuyuanyizheng.emr;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrSocketMessage;
import thyyxxk.webserver.service.externalhttp.WebSocketService;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.service.zhuyuanyisheng.emr.EmrWebSocketServer;
import thyyxxk.webserver.service.zhuyuanyisheng.emr.EmrWebSocketServiceV2;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/emrSocket")
public class EmrWebSocketController {

    private final EmrWebSocketServer server;
    private final EmrWebSocketServiceV2 emrWebSocketServerV2;
    private final WebSocketService webSocketService;
    private final RedisLikeService redisLikeService;

    @Autowired
    public EmrWebSocketController(EmrWebSocketServer server, EmrWebSocketServiceV2 emrWebSocketServerV2, WebSocketService webSocketService, RedisLikeService redisLikeService) {
        this.server = server;
        this.emrWebSocketServerV2 = emrWebSocketServerV2;
        this.webSocketService = webSocketService;
        this.redisLikeService = redisLikeService;
    }

    @GetMapping("/getRoomPeople")
    public ResultVo<Map<String, UserInfo>> getRoomPeople(@RequestParam("sid") String sid) {
        return server.getRoomPeople(sid);
    }

    @GetMapping("/repeatedlyOpenTheSamePatient")
    public ResultVo<Boolean> repeatedlyOpenTheSamePatient(@RequestParam("sid") String sid) {
        return server.repeatedlyOpenTheSamePatient(sid);
    }

    @PassToken
    @GetMapping("/sendAMessage")
    public ResultVo<String> sendAMessage(@RequestParam("sid") String sid,
                                         @RequestParam("key") String key,
                                         @RequestParam("message") String message) {
        JSONObject obj = new JSONObject();
        obj.put("sid", sid);
        obj.put("msg", String.format("{\"%s\": \"%s\"}", key, message));
        webSocketService.emrSendAMessage(obj);
        return ResultVoUtil.success();
    }

    @GetMapping("/getChatHistoryBySid")
    public ResultVo<List<EmrSocketMessage>> getChatHistoryBySid(@RequestParam("sid") String sid) {
        return server.getChatHistoryBySid(sid);
    }

    @GetMapping("/isThereADoctorEditing")
    public ResultVo<UserInfo> isBeingEditing(@RequestParam("sid") String sid) {
        return emrWebSocketServerV2.isBeingEditing(sid);
    }

    @GetMapping("/forcedKickingOutOfPersonnelByDocumentId")
    public ResultVo<String> forcedKickingOutOfPersonnelByDocumentId(@RequestParam("documentId") String documentId) {
        return emrWebSocketServerV2.forcedKickingOutOfPersonnelByDocumentId(documentId);
    }

    @PostMapping("/sendEmrSocketMessage")
    @PassToken
    public ResultVo<String> sendEmrSocketMessage(@RequestBody JSONObject js) {
        webSocketService.sendEmrSocketMessage(js);
        return ResultVoUtil.success();
    }

    @GetMapping("/getCurrentDocumentUserCode")
    @PassToken
    public ResultVo<UserInfo> getCurrentDocumentUserCode(@RequestParam("documentId") String documentId) {
        ResultVo<String> res = webSocketService.getCurrentDocumentUserCode(documentId);
        String userCode = res.getData();
        if (StringUtil.isBlank(userCode)) {
            return ResultVoUtil.success(ExceptionEnum.SUCCESS, null, null);
        }
        return ResultVoUtil.success(redisLikeService.getUserInfoByCode(userCode));
    }

}
