package com.njcn.gather.icd.mapping.component;

import com.njcn.gather.icd.mapping.pojo.bo.IcdToXmlGenerateResult;
import com.njcn.gather.icd.mapping.pojo.bo.analysis.IndexCandidate;
import com.njcn.gather.icd.mapping.pojo.bo.analysis.IndexCandidateReportItem;
import com.njcn.gather.icd.mapping.pojo.vo.IcdToXmlResponse;
import com.njcn.gather.icd.mapping.pojo.vo.IndexCandidateReportItemResponse;
import com.njcn.gather.icd.mapping.pojo.vo.IndexCandidateResponse;
import com.njcn.gather.icd.mapping.pojo.vo.MappingDocumentResponse;
import org.springframework.stereotype.Component;

@Component
public class IcdToXmlResponseConverter {

    public IcdToXmlResponse fromResult(IcdToXmlGenerateResult result) {
            IcdToXmlResponse response = new IcdToXmlResponse();
            response.setStatus(result.getStatus());
            response.setMessage(result.getMessage());
            response.setIedName(result.getIedName());
            response.setLdInst(result.getLdInst());
            response.setSavedPath(result.getSavedPath());
            response.getProblems().addAll(result.getProblems());

            if (result.getIndexAnalysis() != null && result.getIndexAnalysis().getCandidates() != null) {
                for (IndexCandidate candidate : result.getIndexAnalysis().getCandidates()) {
                    IndexCandidateResponse candidateResponse = new IndexCandidateResponse();
                    candidateResponse.setGroupKey(candidate.getGroupKey());
                    candidateResponse.setGroupDesc(candidate.getGroupDesc());
                    candidateResponse.setReportCount(candidate.getReportCount());
                    candidateResponse.getTemplateLabels().addAll(candidate.getTemplateLabels());

                    if (candidate.getReports() != null) {
                        for (IndexCandidateReportItem item : candidate.getReports()) {
                            IndexCandidateReportItemResponse itemResponse = new IndexCandidateReportItemResponse();
                            itemResponse.setReportName(item.getReportName());
                            itemResponse.setDataSetName(item.getDataSetName());
                            itemResponse.setReportDesc(item.getReportDesc());
                            itemResponse.getAvailableLnInstValues().addAll(item.getAvailableLnInstValues());
                            candidateResponse.getReports().add(itemResponse);
                        }
                    }

                    response.getIndexCandidates().add(candidateResponse);
                }
            }

            if (result.getMappingDocument() != null) {
                MappingDocumentResponse doc = new MappingDocumentResponse();
                doc.setVersion(result.getMappingDocument().getVersion());
                doc.setAuthor(result.getMappingDocument().getAuthor());
                doc.setIed(result.getMappingDocument().getIed());
                doc.setLd(result.getMappingDocument().getLd());
                doc.setReportCount(result.getMappingDocument().getReportMap() == null
                        ? 0 : result.getMappingDocument().getReportMap().size());
                doc.setDataSetCount(result.getMappingDocument().getDataSetList() == null
                        ? 0 : result.getMappingDocument().getDataSetList().size());
                response.setMappingDocument(doc);
            }

            return response;
        }
}