package thyyxxk.webserver.service.triage;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.triage.RoomScreenDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.mangedoctorinfo.DoctorInfo;
import thyyxxk.webserver.entity.triage.MessageForPush;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.VoiceUtil;
import thyyxxk.webserver.websocket.WebSocketServer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dj
 */
@Slf4j
@Service
public class RoomScreenService {
    private final RoomScreenDao dao;

    @Autowired
    public RoomScreenService(RoomScreenDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<String>> getVideoList() {
        return ResultVoUtil.success(dao.getVideoList());
    }

    public ResultVo<DoctorInfo> getDoctorInfo(String code) {
        String doctorCode = dao.getRoomDoctorCode(code);
        DoctorInfo entity =  dao.getDoctorInfo(doctorCode);
        if (null == entity) {
            entity = dao.getDoctorInfo(code);
        }
        return ResultVoUtil.success(entity);
    }

    public ResultVo<CodeName> getRoomNoAndDept(String roomCode) {
        return ResultVoUtil.success(dao.getRoomNoAndDept(roomCode));
    }

    public ResultVo<String> pushSocketMsg(MessageForPush param) {
        log.info("消息推送>>> {}", param);
        if (param.getAction() == 1) {
            String text = String.format("请%s号%s，到%s%s号诊室就诊。", param.getFzNo(), param.getName(),
                    param.getDeptName(), param.getRoomNo());
            VoiceUtil.textToSpeech(text, String.valueOf(param.getSerialNo()));
        }
        int code = WebSocketServer.sendMessage(param.getRoomCode() + "-triageRoomScreen", JSON.toJSONString(param));
        if (code == ExceptionEnum.SUCCESS.getCode()) {
            return ResultVoUtil.success("推送成功");
        }
        RestTemplate template = new RestTemplate();
        Integer tcode = template.postForObject("http://172.16.32.160:8082/triage/roomScreen/pushMsg", param, Integer.class);
        if (null != tcode && tcode != -1) {
            return ResultVoUtil.success("推送成功");
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "推送失败！");
    }

    public ResultVo<MessageForPush> getNextJz(MessageForPush param) {
        return ResultVoUtil.success(dao.getNextJz(param));
    }

    public ResultVo<List<MessageForPush>> getNextJzList(MessageForPush param) {
        log.info("获取 诊室：{} 的候诊列表。", param.getRoomCode());
        List<MessageForPush> list = dao.getNextJzList(param);
        if (null == list) {
            list = new ArrayList<>();
        }
        return ResultVoUtil.success(list);
    }

    public ResultVo<String> getRoomCodeByIp(String ip) {
        String roomCode = dao.getRoomCodeByIp(ip);
        if (StringUtil.isBlank(roomCode)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未找到 IP【" + ip +
                    "】对应的诊室，请联系信息中心（8321）进行配置。");
        }
        return ResultVoUtil.success(roomCode);
    }
}
