package it.integry.integrywmsnative.view.bottom_sheet__lu_content;

import androidx.databinding.ObservableField;

import java.math.BigDecimal;
import java.math.RoundingMode;

import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.model.MtbTCol;

public class BottomSheetFragmentLUContentViewModel {

    public ObservableField<MtbColt> mtbColt = new ObservableField<>();
    public ObservableField<String> pesoKg = new ObservableField<>();
    public ObservableField<String> pesoNettoKg = new ObservableField<>();
    private Listener mListener;
    private MtbColrListener mMtbColrListener;

    public void closeCurrentLU() {
        if (this.mListener != null) this.mListener.onBottomSheetLUClose();
    }

    public BottomSheetFragmentLUContentViewModel setMtbColt(MtbColt mtbColt) {
        this.mtbColt.set(mtbColt);
        return this;
    }

    public ObservableField<MtbColt> getObservableMtbColt() {
        return this.mtbColt;
    }

    public BottomSheetFragmentLUContentViewModel setListener(Listener listener) {
        this.mListener = listener;
        return this;
    }

    public BottomSheetFragmentLUContentViewModel setMtbColrListener(MtbColrListener mtbColrListener) {
        this.mMtbColrListener = mtbColrListener;
        return this;
    }

    public void refreshPesi() {
        MtbColt collo = this.mtbColt.get();
        if (collo == null) return;

        MtbTCol tipoPedana = collo.getMtbTCol();
        BigDecimal taraPedana = tipoPedana != null ? tipoPedana.getTaraKg() : BigDecimal.ZERO;
        BigDecimal pesoNetto = BigDecimal.ZERO;

        for (MtbColr mtbColr : collo.getMtbColr()) {
            MtbAart articolo = mtbColr.getMtbAart();

            if (articolo == null || articolo.getPesoKg() == null || articolo.getTaraKg() == null)
                continue;

            BigDecimal pesoRiga = articolo.isFlagQtaCnfFissaBoolean() ? mtbColr.getNumCnf().multiply(articolo.getPesoKg()) : mtbColr.getQtaCol();
            BigDecimal pesoTara = articolo.getTaraKg().multiply(mtbColr.getNumCnf());
            pesoRiga = pesoRiga.add(pesoTara);
            pesoNetto = pesoNetto.add(pesoRiga);

        }

        this.pesoNettoKg.set(pesoNetto.setScale(2, RoundingMode.CEILING).toString());
        this.pesoKg.set(pesoNetto.add(taraPedana).setScale(2, RoundingMode.CEILING).toString());
    }

    public interface Listener {
        void onBottomSheetLUClose();
    }

    public interface MtbColrListener {

    }
}
