package it.integry.integrywmsnative.gest.pv_ordine_acquisto_edit.dialog;

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

import javax.inject.Inject;

import it.integry.integrywmsnative.core.data_store.db.entity.ArticoloOrdine;
import it.integry.integrywmsnative.core.data_store.db.repository.ArticoliOrdineRepository;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityBigDecimal;

public class DialogEditArticoloViewModel {

    ArticoliOrdineRepository mArticoliOrdineRepository;

    private Listener mListener;

    private ArticoloOrdine mCurrentArticolo;

    private BigDecimal internalNumCnf;
    private BigDecimal internalQtaCnf;
    private BigDecimal internalQtaTot;

    @Inject
    public DialogEditArticoloViewModel(ArticoliOrdineRepository articoliOrdineRepository) {
        this.mArticoliOrdineRepository = articoliOrdineRepository;
    }

    public DialogEditArticoloViewModel setCurrentArticolo(ArticoloOrdine currentArticolo) {
        this.mCurrentArticolo = currentArticolo;
        return this;
    }

    public DialogEditArticoloViewModel setInitialNumCnf(BigDecimal numCnf) {
        this.internalNumCnf = numCnf;
        return this;
    }

    public DialogEditArticoloViewModel setInitialQtaCnf(BigDecimal qtaCnf) {
        this.internalQtaCnf = qtaCnf;
        return this;
    }

    public DialogEditArticoloViewModel setInitialQtaTot(BigDecimal qtaTot) {
        this.internalQtaTot = qtaTot;
        return this;
    }

    public void setNumCnf(BigDecimal newValue) {
        this.internalNumCnf = newValue;
        if (newValue == null) return;

        this.internalQtaTot = UtilityBigDecimal.multiply(newValue, internalQtaCnf)
                .setScale(2, RoundingMode.CEILING);

        this.mListener.onDataChanged();
    }

    public void setQtaCnf(BigDecimal newValue) {
        this.internalQtaCnf = newValue;
        if (newValue == null) return;

        this.mListener.onDataChanged();
    }

    public void setQtaTot(BigDecimal newValue) {
        this.internalQtaTot = newValue;
        if (newValue == null) return;
        this.internalNumCnf = UtilityBigDecimal.divide(newValue, internalQtaCnf)
                .setScale(0, RoundingMode.CEILING);

        this.mListener.onDataChanged();
    }

    public BigDecimal getNumCnf() {
        return this.internalNumCnf;
    }

    public BigDecimal getQtaCnf() {
        return this.internalQtaCnf;
    }

    public BigDecimal getQtaTot() {
        return this.internalQtaTot;
    }


    public interface Listener {
        void onDataChanged();

        void onError(Exception ex);
    }


    public Listener getListener() {
        return mListener;
    }

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


    public void save(RunnableArgs<ArticoloOrdine> onComplete) {
        if (UtilityBigDecimal.equalsOrLowerThan(this.internalQtaTot, BigDecimal.ZERO)) {
            this.mListener.onError(new Exception("La quantità ordinata deve essere maggiore di 0"));
            return;
        }

        this.mCurrentArticolo.setQtaOrd(this.internalQtaTot.floatValue());

        try {
            mArticoliOrdineRepository.saveArticoloToOrdine(this.mCurrentArticolo, () -> {
                onComplete.run(this.mCurrentArticolo);
            }, this.mListener::onError);
        } catch (Exception ex) {
            this.mListener.onError(ex);
        }
    }
}
