package it.integry.integrywmsnative.gest.prod_versamento_materiale_su_mag_prossimita;

import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.math.BigDecimal;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.core.expansion.BaseFragment;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.secondary.GestioneEnum;
import it.integry.integrywmsnative.databinding.FragmentProdVersamentoMaterialeInBufferBinding;
import it.integry.integrywmsnative.view.dialogs.DialogConsts;
import it.integry.integrywmsnative.view.dialogs.ask_position_of_lu.DialogAskPositionOfLUView;
import it.integry.integrywmsnative.view.dialogs.base.DialogSimpleMessageView;
import it.integry.integrywmsnative.view.dialogs.input_quantity_to_return.DialogInputQuantityToReturnView;
import it.integry.integrywmsnative.view.dialogs.scan_or_create_lu.DialogScanOrCreateLUView;

public class ProdVersamentoMaterialeInBufferFragment extends BaseFragment implements ProdVersamentoMaterialeInBufferViewModel.Listener {

    @Inject
    ProdVersamentoMaterialeInBufferViewModel mViewModel;

    private FragmentProdVersamentoMaterialeInBufferBinding mBindings;


    public ProdVersamentoMaterialeInBufferFragment() {
        // Required empty public constructor
    }

    public static ProdVersamentoMaterialeInBufferFragment newInstance() {
        return new ProdVersamentoMaterialeInBufferFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setRetainInstance(true);

        mBindings = FragmentProdVersamentoMaterialeInBufferBinding.inflate(inflater, container, false);
        mBindings.setLifecycleOwner(this);

        MainApplication.appComponent
                .prodVersamentoMaterialeInBufferComponent()
                .create()
                .inject(this);

        mBindings.setViewmodel(mViewModel);
        mViewModel.setListener(this);

        return mBindings.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();

        this.openLU();
    }


    private void openLU() {
        DialogScanOrCreateLUView.newInstance(false, false, true, false, true, (mtbColt, created) -> {
            if (mtbColt == null) {
                popMe();
            } else if ((mtbColt.getGestioneEnum() == GestioneEnum.ACQUISTO ||
                    mtbColt.getGestioneEnum() == GestioneEnum.LAVORAZIONE ||
                    mtbColt.getGestioneEnum() == GestioneEnum.VENDITA) &&
                    mtbColt.getSegno() == 1) {

                if (mtbColt.getMtbColr() == null || mtbColt.getMtbColr().size() == 0) {
                    DialogSimpleMessageView.makeWarningDialog(
                                    new SpannableString(Html.fromHtml("E' stata scansionata una UL già vuota")),
                                    null, this::openLU)
                            .show((requireActivity()).getSupportFragmentManager(), "tag");

                } else {
                    mViewModel.setMtbColt(mtbColt);
                    choosePosition();
                }

            } else {
                this.onError(new Exception("Sono accettate solamente UL di <b>Acquisto</b> o <b>Lavorazione</b> di <b>CARICO</b>"));
            }
        }).show(requireActivity().getSupportFragmentManager(), "tag");
    }


    private void choosePosition() {
        DialogAskPositionOfLUView.makeBase(true, (status, mtbDepoPosizione) -> {

                    if (status == DialogConsts.Results.ABORT) {
                        popMe();
                    } else {
                        mViewModel.setPosizione(mtbDepoPosizione, () -> {
                            popMe();
                        });
                    }

                }, this::onError)
                .show(requireActivity().getSupportFragmentManager(), "tag");
    }

    @Override
    public void onRequestColliQuantityToConfirm(RunnableArgs<BigDecimal> onComplete) {
        DialogInputQuantityToReturnView.newInstance(mViewModel.getMtbColr().getMtbAart(),
                        BigDecimal.ZERO,
                        mViewModel.getMtbColr().getNumCnf(),
                        onComplete)
                .show(this.getParentFragmentManager(), DialogInputQuantityToReturnView.class.getName());
    }

    @Override
    public void onError(Exception ex, boolean requestClose) {
        super.onError(ex);
        if (requestClose) popMe();
    }
}