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

import com.njcn.gather.icd.mapping.pojo.dto.IcdToXmlGenerateCommand;
import com.njcn.gather.icd.mapping.pojo.dto.IndexBindingCommand;
import com.njcn.gather.icd.mapping.pojo.dto.IndexSelectionGroupCommand;
import com.njcn.gather.icd.mapping.pojo.param.IcdToXmlGenerateRequest;
import com.njcn.gather.icd.mapping.pojo.param.IndexBindingRequest;
import com.njcn.gather.icd.mapping.pojo.param.IndexSelectionGroupRequest;
import com.njcn.gather.icd.mapping.pojo.param.JsonToXmlRequest;import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class IcdToXmlRequestConverter {

    public IcdToXmlGenerateCommand toCommand(MultipartFile icdFile, IcdToXmlGenerateRequest request) {
        IcdToXmlGenerateCommand command = new IcdToXmlGenerateCommand();
        try {
            command.setFileBytes(icdFile.getBytes());
        } catch (IOException e) {
            throw new RuntimeException("读取 ICD 文件失败", e);
        }
        command.setFileName(icdFile.getOriginalFilename());
        command.setVersion(request.getVersion());
        command.setAuthor(request.getAuthor());
        command.setSaveToDisk(request.isSaveToDisk());
        command.setOutputDir(request.getOutputDir());
        command.setIndexSelection(toIndexSelectionGroupCommands(request.getIndexSelection()));
        return command;
    }

    /**
     * 将 JsonToXmlRequest 转换为 IcdToXmlGenerateCommand。
     * 注意：此方法不处理文件，仅转换索引选择结果。
     */
    public IcdToXmlGenerateCommand toCommand(JsonToXmlRequest request) {
        IcdToXmlGenerateCommand command = new IcdToXmlGenerateCommand();
        command.setVersion("1.0");
        command.setAuthor("");
        command.setSaveToDisk(false);
        command.setOutputDir(null);

        
        return command;
    }

    private List<IndexSelectionGroupCommand> toIndexSelectionGroupCommands(List<IndexSelectionGroupRequest> indexSelection) {
        List<IndexSelectionGroupCommand> indexSelectionGroupCommands = new ArrayList<>();
        if (indexSelection != null) {
            for (IndexSelectionGroupRequest groupRequest : indexSelection) {
                IndexSelectionGroupCommand groupCommand = new IndexSelectionGroupCommand();
                groupCommand.setGroupKey(groupRequest.getGroupKey());
                groupCommand.setGroupDesc(groupRequest.getGroupDesc());
                groupCommand.setBindings(toIndexBindingCommands(groupRequest.getBindings()));
                indexSelectionGroupCommands.add(groupCommand);
            }
        }
        return indexSelectionGroupCommands;
    }

    private List<IndexBindingCommand> toIndexBindingCommands(List<IndexBindingRequest> bindings) {
        List<IndexBindingCommand> indexBindingCommands = new ArrayList<>();
        if (bindings != null) {
            for (IndexBindingRequest bindingRequest : bindings) {
                IndexBindingCommand bindingCommand = new IndexBindingCommand();
                bindingCommand.setReportName(bindingRequest.getReportName());
                bindingCommand.setDataSetName(bindingRequest.getDataSetName());
                bindingCommand.setLabel(bindingRequest.getLabel());
                bindingCommand.setLnInst(bindingRequest.getLnInst());
                indexBindingCommands.add(bindingCommand);
            }
        }
        return indexBindingCommands;
    }
}