package com.njcn.gather.icd.service.support;

import cn.hutool.core.util.StrUtil;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.gather.icd.pojo.enums.IcdResponseEnum;
import com.njcn.gather.icd.pojo.param.PqIcdPathParam;
import com.njcn.gather.icd.pojo.po.PqIcdPath;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Component
@RequiredArgsConstructor
public class IcdPayloadAssembler {

    private final IcdPayloadValidator validator;

    public PqIcdPath fromCreate(PqIcdPathParam.CreateParam param) {
        MultipartFile file = validator.validateIcdFile(param.getIcdFile(), true);
        validator.validateJson(param.getJsonStr());
        validator.validateXml(param.getXmlStr());

        PqIcdPath entity = new PqIcdPath();
        applyCommonFields(entity, param);
        entity.setName(validator.extractName(file.getOriginalFilename()));
        entity.setIcd(getBytes(file));
        entity.setState(DataStateEnum.ENABLE.getCode());
        return entity;
    }

    public PqIcdPath fromExternalCreate(PqIcdPathParam.ExternalCreateParam param, int type, int angle, int usePhaseIndex) {
        byte[] fileBytes = validator.decodeExternalIcdFile(param.getIcd());
        validator.validateJson(param.getJsonStr());
        validator.validateXml(param.getXmlStr());

        PqIcdPath entity = new PqIcdPath();
        entity.setId(normalizeNullableText(param.getId()));
        param.setType(type);
        param.setAngle(angle);
        param.setUsePhaseIndex(usePhaseIndex);
        applyCommonFields(entity, param);
        entity.setName(normalizeNullableText(param.getName()));
        entity.setIcd(fileBytes);
        entity.setState(DataStateEnum.ENABLE.getCode());
        return entity;
    }

    public PqIcdPath merge(PqIcdPath current, PqIcdPathParam.UpdateParam param) {
        MultipartFile file = validator.validateIcdFile(param.getIcdFile(), false);
        validator.validateJson(param.getJsonStr());
        validator.validateXml(param.getXmlStr());

        applyCommonFields(current, param);
        if (file != null && !file.isEmpty()) {
            current.setName(validator.extractName(file.getOriginalFilename()));
            current.setIcd(getBytes(file));
        }
        return current;
    }

    private void applyCommonFields(PqIcdPath entity, PqIcdPathParam param) {
        entity.setAngle(param.getAngle());
        entity.setUsePhaseIndex(param.getUsePhaseIndex());
        entity.setJsonStr(param.getJsonStr());
        entity.setXmlStr(param.getXmlStr());
        entity.setResult(param.getResult());
        entity.setMsg(param.getResult() != null && param.getResult() == 1 ? null : normalizeNullableText(param.getMsg()));
        entity.setType(param.getType());
        entity.setReferenceIcdId(requiresReferenceIcd(param.getType()) ? normalizeNullableText(param.getReferenceIcdId()) : null);
    }

    private String normalizeNullableText(String value) {
        return StrUtil.emptyToNull(StrUtil.trim(value));
    }

    private boolean requiresReferenceIcd(Integer type) {
        return type != null && (type == 2 || type == 4);
    }

    private byte[] getBytes(MultipartFile file) {
        try {
            return file.getBytes();
        } catch (IOException ex) {
            throw new BusinessException(IcdResponseEnum.ICD_FILE_READ_ERROR);
        }
    }
}
