package com.docus.server.fsy.converter;

import com.docus.core.util.Func;
import com.docus.server.archive.rpc.dto.ReportDownDto;
import com.docus.server.archive.rpc.dto.ReportDownPatientDto;
import com.docus.server.archive.rpc.dto.ReportDownScanFileDto;
import com.docus.server.archive.rpc.dto.ReportDownTwoDto;
import com.docus.server.archive.utils.XmlUtil;
import com.docus.server.fsy.api.dto.LisPatientGroupReportRequest;
import com.docus.server.fsy.api.dto.LisPatientGroupReportResponse;
import com.docus.server.fsy.api.dto.LisPatientListRequest;
import com.docus.server.fsy.api.dto.LisPatientListResponse;
import com.docus.server.fsy.api.dto.LisPatientReportRequest;
import com.docus.server.fsy.api.dto.LisPatientReportResponse;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

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

/**
 * @author YongBin Wen
 * @date 2024/3/29 10:24
 */
public class LisReportConverter {
    /**
     * test-pass
     */
    public static String convertWsParam(LisPatientListRequest request) {
        String pidInNo = Func.isBlank(request.getPidInNo()) ? "" : request.getPidInNo();
        String pidAddmissTimes = Objects.isNull(request.getPidAddmissTimes()) ? "" : request.getPidAddmissTimes().toString();
        String startDate = Func.isBlank(request.getStartDate()) ? "" : request.getStartDate();
        String endDate = Func.isBlank(request.getEndDate()) ? "" : request.getEndDate();

        return "<Request>\n" +
                "  <PidInNo>" + pidInNo + "</PidInNo>\n" +
                "  <PidAddmissTimes>" + pidAddmissTimes + "</PidAddmissTimes>\n" +
                "  <StartDate>" + startDate + "</StartDate>\n" +
                "  <EndDate>" + endDate + "</EndDate>\n" +
                "</Request>";
    }

    public static void main(String[] args) {
        System.out.println("<Request>\n" +
                "  <PidInNo></PidInNo>\n" +
                "  <PidAddmissTimes></PidAddmissTimes>\n" +
                "  <StartDate></StartDate>\n" +
                "  <EndDate></EndDate>\n" +
                "</Request>");
    }

    /**
     * test-pass
     */
    public static String convertWsParam(LisPatientReportRequest request) {
        String repId = Func.isBlank(request.getRepId()) ? "" : request.getRepId();
        String customReportSuffix = Func.isBlank(request.getCustomReportSuffix()) ? "" : request.getCustomReportSuffix();

        return "<Request>\n" +
                "  <RepID>" + repId + "</RepID>\n" +
                "  <CustomReportSuffix>" + customReportSuffix + "</CustomReportSuffix>\n" +
                "</Request>";
    }

    public static String convertWsParam(LisPatientGroupReportRequest request) {
        return "<Request>\n" +
                "  <RepIds>" + request.getRepIds() + "</RepIds>\n" +
                "  <ReportCode>" + request.getReportCode() + "</ReportCode>\n" +
                "</Request>";
    }

    /**
     * test-pass
     */
    public static LisPatientListResponse convertWsPatientListResponse(String result) {
        if (Func.isBlank(result)) {
            return null;
        }
        XmlUtil xmlUtil = XmlUtil.of(result);
        if (wsIsFailed(xmlUtil)) {
            return null;
        }
        ArrayList<LisPatientListResponse.Patient> patients = new ArrayList<>();
        NodeList pidReportMainNodeList = xmlUtil.getNodeList("/Response/Result/PidReportMain");
        int mainNodeListLength = pidReportMainNodeList.getLength();
        for (int i = 0; i < mainNodeListLength; i++) {
            Node pidReportMainNode = pidReportMainNodeList.item(i);
            NodeList childNodes = pidReportMainNode.getChildNodes();
            int childNodesLength = childNodes.getLength();
            LisPatientListResponse.Patient patient = new LisPatientListResponse.Patient();
            for (int j = 0; j < childNodesLength; j++) {
                Node node = childNodes.item(j);
                if ("PidInNo".equals(node.getNodeName())) {
                    patient.setPidInNo(node.getTextContent());
                    continue;
                }
                if ("PidAddmissTimes".equals(node.getNodeName())) {
                    patient.setPidAddmissTimes(node.getTextContent());
                    continue;
                }
                if ("RepId".equals(node.getNodeName())) {
                    patient.setRepId(node.getTextContent());
                    continue;
                }
                if ("PidComName".equals(node.getNodeName())) {
                    patient.setPidComName(node.getTextContent());
                    continue;
                }
                if ("RepInDate".equals(node.getNodeName())) {
                    patient.setRepInDate(node.getTextContent());
                }
                if ("RepReportType".equals(node.getNodeName())) {
                    patient.setRepReportType(node.getTextContent());
                }
            }
            patients.add(patient);
        }

        LisPatientListResponse response = new LisPatientListResponse();
        response.setPatientList(patients);
        return response;
    }

    /**
     * test-pass
     */
    public static LisPatientReportResponse convertWsPatientReportResponse(String result) {
        if (Func.isBlank(result)) {
            return null;
        }
        XmlUtil xmlUtil = XmlUtil.of(result);
        if (wsIsFailed(xmlUtil)) {
            return null;
        }
        Node repIdNode = xmlUtil.getNode("/Response/Result/Patient/RepId");
        Node patientReportNode = xmlUtil.getNode("/Response/Result/Patient/PatientReport");
        LisPatientReportResponse response = new LisPatientReportResponse();
        response.setRepId(repIdNode.getTextContent());
        response.setPatientReport(patientReportNode.getTextContent());
        return response;
    }

    /**
     * test-pass
     */
    public static boolean wsIsFailed(XmlUtil xmlUtil) {
        Node resultCodeNode = xmlUtil.getNode("/Response/ResultCode");
        String failedCode = "1";
        return resultCodeNode == null || failedCode.equals(resultCodeNode.getTextContent());
    }

    /**
     * 根据任务信息，获得的患者报告信息转换上报下载服务参数
     */
    public static ReportDownDto convertDownloadPlatform(ReportDownTwoDto reportDownTwoDto, LisPatientListResponse.Patient patient, LisPatientReportResponse reportResponse) {
        ReportDownScanFileDto scanFileDto = new ReportDownScanFileDto();
        scanFileDto.setTaskid(reportDownTwoDto.getTasks().get(0).getTaskId());
        scanFileDto.setFiletitle(patient.getPidComName());
        scanFileDto.setFilesource(1);
        scanFileDto.setFilestoragetype(1);
        scanFileDto.setFiletype(2);
        scanFileDto.setDownurl(reportResponse.getPatientReport());
        scanFileDto.setSerialnum(reportResponse.getRepId());


        List<ReportDownScanFileDto> scanfiles = new ArrayList<>();
        scanfiles.add(scanFileDto);

        ReportDownPatientDto reportDownPatientDto = new ReportDownPatientDto();
        reportDownPatientDto.setPatientid(reportDownTwoDto.getPatientId());

        ReportDownDto reportDownDto = new ReportDownDto();
        reportDownDto.setIp("ws-java");
        reportDownDto.setScanusercode("ws-java");
        reportDownDto.setScanusername("ws-java-");
        reportDownDto.setPatient(reportDownPatientDto);
        reportDownDto.setScanfiles(scanfiles);
        return reportDownDto;
    }

    public static ReportDownDto convertDownloadPlatform(ReportDownTwoDto reportDownTwoDto, LisPatientGroupReportResponse groupReport) {
        ReportDownScanFileDto scanFileDto = new ReportDownScanFileDto();
        scanFileDto.setTaskid(reportDownTwoDto.getTasks().get(0).getTaskId());
        scanFileDto.setFiletitle("检验报告单");
        scanFileDto.setFilesource(1);
        scanFileDto.setFilestoragetype(1);
        scanFileDto.setFiletype(2);
        scanFileDto.setDownurl(groupReport.getPatientReport());
        scanFileDto.setSerialnum("jianyan-"+reportDownTwoDto.getPatientId());


        List<ReportDownScanFileDto> scanfiles = new ArrayList<>();
        scanfiles.add(scanFileDto);

        ReportDownPatientDto reportDownPatientDto = new ReportDownPatientDto();
        reportDownPatientDto.setPatientid(reportDownTwoDto.getPatientId());

        ReportDownDto reportDownDto = new ReportDownDto();
        reportDownDto.setIp("ws-java");
        reportDownDto.setScanusercode("ws-java");
        reportDownDto.setScanusername("ws-java-");
        reportDownDto.setPatient(reportDownPatientDto);
        reportDownDto.setScanfiles(scanfiles);
        return reportDownDto;
    }


    public static LisPatientGroupReportResponse convertWsPatientGroupReportResponse(String result) {
        if (Func.isBlank(result)) {
            return null;
        }
        XmlUtil xmlUtil = XmlUtil.of(result);
        if (wsIsFailed(xmlUtil)) {
            return null;
        }
        Node repIdsNode = xmlUtil.getNode("/Response/Result/Patient/RepId");
        Node patientReportNode = xmlUtil.getNode("/Response/Result/Patient/PatientReport");
        LisPatientGroupReportResponse response = new LisPatientGroupReportResponse();
        response.setRepIds(repIdsNode.getTextContent());
        response.setPatientReport(patientReportNode.getTextContent());
        return response;
    }


}
