package com.ruoyi.web.work.hl7.response;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.message.ACK;
import ca.uhn.hl7v2.model.v24.message.ADT_A01;
import ca.uhn.hl7v2.model.v24.message.ADT_A03;
import ca.uhn.hl7v2.model.v24.message.ORU_R01;
import ca.uhn.hl7v2.model.v24.segment.ERR;
import ca.uhn.hl7v2.model.v24.segment.MSA;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import com.ruoyi.web.work.hl7.util.GetAckTimestampUtil;
import com.ruoyi.web.work.hl7.constant.ACKConstant;
import lombok.extern.log4j.Log4j2;

/**
 * 响应ACK
 */
@Log4j2
public class ResponseSuccessful {

    // 通用方法用于设置ACK消息的MSH段
    private static void setupMSH(MSH msh,String sendingApp, String sendingFacility, String receivingApp, String receivingFacility,String msgType) throws HL7Exception {
        msh.getFieldSeparator().setValue("|");
        msh.getEncodingCharacters().setValue("^~\\&");
        msh.getSendingApplication().getNamespaceID().setValue(sendingApp); //发送应用程序
        msh.getSendingFacility().getNamespaceID().setValue(sendingFacility); //发送设备
        msh.getReceivingApplication().getNamespaceID().setValue(receivingApp); // 接收应用程序
        msh.getReceivingFacility().getNamespaceID().setValue(receivingFacility); //接收设备
        msh.getDateTimeOfMessage().getTimeOfAnEvent().setValue(GetAckTimestampUtil.getMessageDateTime());//消息的日期/时间
        msh.getMessageType().getMessageType().setValue("ACK");
        msh.getMessageType().getTriggerEvent().setValue(msgType);
        msh.getMessageControlID().setValue(GetAckTimestampUtil.getMessageId());  //消息控制ID
        msh.getProcessingID().getProcessingID().setValue(ACKConstant.PROCESS_ID); //P
        msh.getVersionID().getVersionID().setValue(ACKConstant.VERSION_ID); //2.4
        msh.getCountryCode().setValue(ACKConstant.CHINA_CODE); //CHN
    }

    // 通用方法用于设置ACK消息的MSA段
    private static void setupMSA(MSA msa, String msgId) throws DataTypeException {
        msa.getAcknowledgementCode().setValue(ACKConstant.CODE);
        msa.getMessageControlID().setValue(msgId);
        msa.getTextMessage().setValue(ACKConstant.SUCCESS);
    }

    // ADT^A03响应消息
    public static Message ResponseADTA03Msg(Message message) throws HL7Exception {
        ACK ack = new ACK();
        MSH msh = (MSH) ack.get("MSH");
        ADT_A03 a03 = (ADT_A03) message;
        String receivingFacility = a03.getMSH().getReceivingFacility().getNamespaceID().getValue();
        String msgType = a03.getMSH().getMessageType().getTriggerEvent().getValue();
        setupMSH(msh,"ANT","HIS", "DOCARE", receivingFacility,msgType);
        MSA msa = (MSA) ack.get("MSA");
        String msgId = a03.getMSH().getMessageControlID().getValue();
        setupMSA(msa, msgId);
        log.info("响应ack:{}", ack);
        return ack;
    }

    // ADT^A01响应消息
    public static Message ResponseADTA01Msg(Message message) throws HL7Exception {
        ACK ack = new ACK();
        MSH msh = (MSH) ack.get("MSH");
        ADT_A01 a01 = (ADT_A01) message;
        String receivingFacility = a01.getMSH().getReceivingFacility().getNamespaceID().getValue();
        String sendingApp = a01.getMSH().getMessageType().getTriggerEvent().getValue().equals("A01") ? "DoCare" : "ANT";
        String sendingFacility = a01.getMSH().getMessageType().getTriggerEvent().getValue().equals("A01") ? "MedicalSystem" : "HIS";
        String receivingApp = a01.getMSH().getMessageType().getTriggerEvent().getValue().equals("A01") ? "HIS" : "DOCARE";
        String msgType = a01.getMSH().getMessageType().getTriggerEvent().getValue();
        setupMSH(msh,sendingApp, sendingFacility,receivingApp,receivingFacility,msgType);
        MSA msa = (MSA) ack.get("MSA");
        String msgId = a01.getMSH().getMessageControlID().getValue();
        setupMSA(msa,msgId);
        log.info("响应ack:{}", ack);
        return ack;
    }

    //ORU^R01响应消息
    public static Message ResponseORUR01Msg(Message message) throws HL7Exception {
        ACK ack = new ACK();
        MSH msh = (MSH) ack.get("MSH");
        ORU_R01 oruR01 = (ORU_R01) message;
        String msgType = oruR01.getMSH().getMessageType().getTriggerEvent().getValue();
        setupMSH(msh,"HIS","WinninSoft","ECG","MedEx",msgType);
        MSA msa = (MSA) ack.get("MSA");
        String msgId = oruR01.getMSH().getMessageControlID().getValue();
        setupMSA(msa, msgId);
        log.info("响应ack:{}", ack);
        return ack;
    }

    //响应错误消息
    public static Message ResponseError(Message message) throws HL7Exception {
        ACK ack = new ACK();
        MSH msh = (MSH) ack.get("MSH");
        setupMSH(msh,"System","AccessSoft","CIS","WinningSoft",msh.getMessageType().getTriggerEvent().getValue());
        MSA msa = (MSA) ack.get("MSA");
        setupMSA(msa, msh.getMessageControlID().getValue());
        ERR err = ack.getERR();
        err.getErrorCodeAndLocation(0).getCodeIdentifyingError().getIdentifier().setValue(err.encode());
        log.info("响应ack:{}", ack);

        // 提取错误信息
        String value = err.getErrorCodeAndLocation(0).getCodeIdentifyingError().getIdentifier().getValue();
        String value1 = err.getErrorCodeAndLocation(0).getCodeIdentifyingError().getText().getValue();
        String value2 = err.getErrorCodeAndLocation(0).getCodeIdentifyingError().getAlternateText().getValue();
        // 处理或记录错误信息
        System.out.println("Error Code: " + value);
        System.out.println("Error Location: " + value1);
        System.out.println("Error Description: " + value2);
        return ack;
    }
}
