package de.avatic.lcc.service.transformer.material;

import de.avatic.lcc.dto.configuration.material.update.MaterialUpdateDTO;
import de.avatic.lcc.model.db.materials.Material;
import org.springframework.stereotype.Service;


@Service
public class MaterialUpdateDTOTransformer {


    public Material fromMaterialUpdateDTO(MaterialUpdateDTO dto) {
        Material entity = new Material();

        entity.setNormalizedPartNumber(normalizePartNumber(dto.getPartNumber()));
        entity.setPartNumber(dto.getPartNumber());
        entity.setName(dto.getName());
        entity.setDeprecated(!dto.isDelete());
        entity.setHsCode(dto.getHsCode());

        return entity;
    }

    private String normalizePartNumber(String partNumber) {
        if (partNumber.length() > 12) throw new IllegalArgumentException("Part number must be less than 12 characters");
        return "000000000000".concat(partNumber).substring(partNumber.length());
    }

}
