package it.integry.integrywmsnative.core.REST.model;

public class Ean13PesoModel {

    private String precode;
    private Float peso;

    public String getPrecode() {
        return precode;
    }

    public Ean13PesoModel setPrecode(String precode) {
        this.precode = precode;
        return this;
    }

    public Float getPeso() {
        return peso;
    }

    public Ean13PesoModel setPeso(Float peso) {
        this.peso = peso;
        return this;
    }

    public Ean128Model toEan128() {
        Ean128Model ean128Model = new Ean128Model();
        ean128Model.NetWeightKg = getPeso();

        return ean128Model;
    }

    public static Ean13PesoModel fromBarcode(String barcode) throws Exception {
        if(barcode == null || barcode.length() != 13) {
            throw new Exception("Errore durante il parse del barcode (" + barcode + ")");
        }

        String precode = barcode.substring(1, 7);
        String pesoString = barcode.substring(7, barcode.length() - 1);

        Float pesoFloat = Integer.parseInt(pesoString) / (float) 1000;

        return new Ean13PesoModel()
                .setPrecode(precode)
                .setPeso(pesoFloat);

    }
}
