package it.integry.integrywmsnative.gest.accettazione_picking;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.Menu;
import android.view.MenuItem;

import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.annimon.stream.Stream;
import com.tfb.fbtoast.FBToast;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.inject.Inject;

import it.integry.barcode_base_android_library.model.BarcodeScanDTO;
import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.CommonConst;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeCallbackDTO;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.data_cache.DataCache;
import it.integry.integrywmsnative.core.di.BindableBoolean;
import it.integry.integrywmsnative.core.expansion.BaseActivity;
import it.integry.integrywmsnative.core.expansion.OnGeneralChangedCallback;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.expansion.RunnableArgss;
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;
import it.integry.integrywmsnative.core.model.secondary.GestioneEnum;
import it.integry.integrywmsnative.core.report.ReportManager;
import it.integry.integrywmsnative.core.rest.model.VersamentoAutomaticoULResponseDTO;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.utility.UtilityBigDecimal;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityResources;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.core.utility.UtilityToast;
import it.integry.integrywmsnative.databinding.ActivityAccettazioneOrdineInevasoBinding;
import it.integry.integrywmsnative.gest.accettazione.dto.OrdineAccettazioneInevasoDTO;
import it.integry.integrywmsnative.gest.accettazione.dto.SitArtOrdDTO;
import it.integry.integrywmsnative.gest.accettazione_picking.dto.AccettazioneOrdineInevasoOrderBy;
import it.integry.integrywmsnative.gest.accettazione_picking.dto.PickingObjectDTO;
import it.integry.integrywmsnative.gest.accettazione_picking.filters.AccettazionePickingFiltroOrdineViewModel;
import it.integry.integrywmsnative.gest.accettazione_picking.filters.FilterCodArtLayoutView;
import it.integry.integrywmsnative.gest.accettazione_picking.filters.FilterDescrLayoutView;
import it.integry.integrywmsnative.gest.accettazione_picking.filters.FilterPosizioneLayoutView;
import it.integry.integrywmsnative.gest.accettazione_picking.rest.RecoverMtbColt;
import it.integry.integrywmsnative.gest.accettazione_picking.ui.AccettazioneListAdapter;
import it.integry.integrywmsnative.gest.accettazione_picking.ui.AccettazioneListModel;
import it.integry.integrywmsnative.gest.lista_bancali.ListaBancaliActivity;
import it.integry.integrywmsnative.gest.spedizione.exceptions.InvalidPesoKGException;
import it.integry.integrywmsnative.gest.spedizione.model.PickedQuantityDTO;
import it.integry.integrywmsnative.ui.filter_chips.FilterChipDTO;
import it.integry.integrywmsnative.ui.filter_chips.FilterChipView;
import it.integry.integrywmsnative.view.bottom_sheet__lu_content.BottomSheetFragmentLUContentView;
import it.integry.integrywmsnative.view.bottom_sheet__lu_content.BottomSheetFragmentLUContentViewModel;
import it.integry.integrywmsnative.view.dialogs.ask_should_versamento_automatico_ul.DialogAskShouldVersamentoAutomaticoULView;
import it.integry.integrywmsnative.view.dialogs.base.DialogSimpleMessageView;
import it.integry.integrywmsnative.view.dialogs.info_aggiuntive_lu.DialogInfoAggiuntiveLU;
import it.integry.integrywmsnative.view.dialogs.input_quantity_v2.DialogInputQuantityV2DTO;
import it.integry.integrywmsnative.view.dialogs.input_quantity_v2.DialogInputQuantityV2View;
import it.integry.integrywmsnative.view.dialogs.versamento_automatico_ul_done.DialogVersamentoAutomaticoULDoneView;

public class AccettazionePickingActivity extends BaseActivity implements AccettazionePickingViewModel.Listener, BottomSheetFragmentLUContentViewModel.Listener, BottomSheetFragmentLUContentView.Listener {

    private ActivityAccettazioneOrdineInevasoBinding mBindings;

    @Inject
    AccettazionePickingViewModel mViewModel;

    @Inject
    DialogInputQuantityV2View mDialogInputQuantityV2View;

    private final AccettazionePickingFiltroOrdineViewModel mAppliedFilterViewModel = new AccettazionePickingFiltroOrdineViewModel();

    private BottomSheetFragmentLUContentViewModel mBottomSheetFragmentLUContentViewModel;

    private final ObservableArrayList<AccettazioneListModel> mAccettazioneMutableData = new ObservableArrayList<>();


    public BindableBoolean noItemsToPick = new BindableBoolean(false);
    public BindableBoolean noLUPresent = new BindableBoolean(true);
    public BindableBoolean bottomSheetEnabled = new BindableBoolean(false);

    private int barcodeScannerIstanceID = -1;

    private List<OrdineAccettazioneInevasoDTO> mOrders;
    private List<SitArtOrdDTO> mSitArts;

    private boolean mShouldCloseActivity;
    private AccettazioneOrdineInevasoOrderBy.Enum mCurrentOrderBy;

    private final int PICK_UL_REQUEST = 1;

    private boolean mShowSecondaryUntMis = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mOrders = DataCache.retrieveItem(getIntent().getStringExtra("keyOrders"));
        mSitArts = DataCache.retrieveItem(getIntent().getStringExtra("keySitArts"));

        mBindings = DataBindingUtil.setContentView(this, R.layout.activity_accettazione_ordine_inevaso);
        mBindings.setLifecycleOwner(this);
        mBindings.setAccettazioneView(this);

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

        setSupportActionBar(mBindings.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mBottomSheetFragmentLUContentViewModel = mBindings.bottomSheetLuContent.getViewModelInstance();
        mBindings.bottomSheetLuContent.setListener(this);
        mBottomSheetFragmentLUContentViewModel.setListener(this);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        mShowSecondaryUntMis = sharedPreferences.getBoolean("picking_ingresso_enable_sec_unt_mis", true);

        this.initVars();
        this.initBarcodeReader();
        this.initRecyclerView();
        this.initFilters();

        String codMdep = SettingsManager.i().getUserSession().getDepo().getCodMdep();
        boolean useQtaOrd = SettingsManager.iDB().isFlagAccettazioneUseQtaOrd();

        mViewModel.setListeners(this);
        mViewModel.init(
                mOrders,
                mSitArts,
                codMdep,
                useQtaOrd);
    }

    private void initFilters() {

        var onPredicateChanged = new OnGeneralChangedCallback() {
            @Override
            public void run() {
                refreshList();
            }
        };

        mAppliedFilterViewModel.getCurrentCodArtPredicate().addOnPropertyChangedCallback(onPredicateChanged);
        mAppliedFilterViewModel.getCurrentDescrPredicate().addOnPropertyChangedCallback(onPredicateChanged);
        mAppliedFilterViewModel.getCurrentPosPredicate().addOnPropertyChangedCallback(onPredicateChanged);

        List<FilterChipDTO> filterList = Stream.of(AccettazionePickingBindings.AVAILABLE_FILTERS.entrySet())
                .map(Map.Entry::getValue)
                .sortBy(FilterChipDTO::getPosizione)
                .toList();

        for (FilterChipDTO filterChipDTO : filterList) {

            FilterChipView filterChipView = new FilterChipView(
                    this,
                    filterChipDTO.getFilterChipText(),
                    v -> initBottomSheetDialogFilter(filterChipDTO));

            switch (filterChipDTO.getID()) {
                case AccettazionePickingBindings.COD_ART_FILTER_ID:
                    mAppliedFilterViewModel.getCurrentCodArtPredicate().addOnPropertyChangedCallback(new OnGeneralChangedCallback() {
                        @Override
                        public void run() {
                            if (mAppliedFilterViewModel.getCurrentCodArtPredicate().get() == null) {
                                mViewModel.resetMatchedRows();
                                filterChipView.disableCloseIcon();

                                mBindings.filterChipsGroup.removeView(filterChipView);
                                mBindings.filterChipsGroup.addView(filterChipView);
                                mBindings.filterChips.smoothScrollTo(0, 0);
                            } else {
                                filterChipView.enableCloseIcon();

                                mBindings.filterChipsGroup.removeView(filterChipView);
                                mBindings.filterChipsGroup.addView(filterChipView, 0);
                                mBindings.filterChips.smoothScrollTo(0, 0);
                            }
                        }
                    });
                    filterChipView.setOnResetClicked(() -> mAppliedFilterViewModel.getCurrentCodArtPredicate().set(null));
                    break;
                case AccettazionePickingBindings.DESCR_FILTER_ID:
                    mAppliedFilterViewModel.getCurrentDescrPredicate().addOnPropertyChangedCallback(new OnGeneralChangedCallback() {
                        @Override
                        public void run() {
                            if (mAppliedFilterViewModel.getCurrentDescrPredicate().get() == null) {
                                mViewModel.resetMatchedRows();
                                filterChipView.disableCloseIcon();

                                mBindings.filterChipsGroup.removeView(filterChipView);
                                mBindings.filterChipsGroup.addView(filterChipView);
                                mBindings.filterChips.smoothScrollTo(0, 0);
                            } else {
                                filterChipView.enableCloseIcon();

                                mBindings.filterChipsGroup.removeView(filterChipView);
                                mBindings.filterChipsGroup.addView(filterChipView, 0);
                                mBindings.filterChips.smoothScrollTo(0, 0);
                            }
                        }
                    });
                    filterChipView.setOnResetClicked(() -> mAppliedFilterViewModel.getCurrentDescrPredicate().set(null));
                    break;
                case AccettazionePickingBindings.POS_FILTER_ID:
                    mAppliedFilterViewModel.getCurrentPosPredicate().addOnPropertyChangedCallback(new OnGeneralChangedCallback() {
                        @Override
                        public void run() {
                            if (mAppliedFilterViewModel.getCurrentPosPredicate().get() == null) {
                                mViewModel.resetMatchedRows();
                                filterChipView.disableCloseIcon();

                                mBindings.filterChipsGroup.removeView(filterChipView);
                                mBindings.filterChipsGroup.addView(filterChipView);
                                mBindings.filterChips.smoothScrollTo(0, 0);
                            } else {
                                filterChipView.enableCloseIcon();

                                mBindings.filterChipsGroup.removeView(filterChipView);
                                mBindings.filterChipsGroup.addView(filterChipView, 0);
                                mBindings.filterChips.smoothScrollTo(0, 0);
                            }
                        }
                    });
                    filterChipView.setOnResetClicked(() -> mAppliedFilterViewModel.getCurrentPosPredicate().set(null));
                    break;
            }

            this.mBindings.filterChipsGroup.addView(filterChipView);
        }
    }


    private void initBottomSheetDialogFilter(FilterChipDTO filterChipDTO) {

        filterChipDTO
                .getFilterLayoutView()
                .setFilterName(filterChipDTO.getFilterChipText())
                .setContext(this);

        var filterLayoutView = filterChipDTO
                .getFilterLayoutView();

        switch (filterChipDTO.getID()) {
            case AccettazionePickingBindings.COD_ART_FILTER_ID:
                ((FilterCodArtLayoutView) filterLayoutView)
                        .setAll(mAppliedFilterViewModel.getAllCodArts())
                        .setAvailable(mAppliedFilterViewModel.getAvailableCodArts())
                        .setOnFilterApplied(mAppliedFilterViewModel::setCodArtFilter)
                        .setPreselected(Stream.of(Objects.requireNonNull(mViewModel.getPickingList().getValue()))
                                .filter(mAppliedFilterViewModel.getCurrentCodArtPredicate().get() == null ?
                                        x -> false :
                                        mAppliedFilterViewModel.getCurrentCodArtPredicate().get())
                                .map(x -> x.getSitArtOrdDTO().getCodMart())
                                .toList());
                break;
            case AccettazionePickingBindings.DESCR_FILTER_ID:
                ((FilterDescrLayoutView) filterLayoutView)
                        .setAll(mAppliedFilterViewModel.getAllDescrs())
                        .setAvailable(mAppliedFilterViewModel.getAvailableDescrs())
                        .setOnFilterApplied(mAppliedFilterViewModel::setDescrFilter)
                        .setPreselected(Stream.of(Objects.requireNonNull(mViewModel.getPickingList().getValue()))
                                .filter(mAppliedFilterViewModel.getCurrentDescrPredicate().get() == null ?
                                        x -> false :
                                        mAppliedFilterViewModel.getCurrentDescrPredicate().get())
                                .map(x -> x.getSitArtOrdDTO().getDescrizioneEstesaArt())
                                .toList());
                break;
            case AccettazionePickingBindings.POS_FILTER_ID:
                ((FilterPosizioneLayoutView) filterLayoutView)
                        .setAll(mAppliedFilterViewModel.getAllPos())
                        .setAvailable(mAppliedFilterViewModel.getAvailablePos())
                        .setOnFilterApplied(mAppliedFilterViewModel::setPosFilter)
                        .setPreselected(Stream.of(Objects.requireNonNull(mViewModel.getPickingList().getValue()))
                                .filter(mAppliedFilterViewModel.getCurrentPosPredicate().get() == null ?
                                        x -> false :
                                        mAppliedFilterViewModel.getCurrentPosPredicate().get())
                                .map(x -> x.getMtbAart().getPosizione())
                                .toList());
                break;

        }

        if (!filterLayoutView.isAdded())
            filterLayoutView.show(getSupportFragmentManager(), "TAG");
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @Override
    public void onBackPressed() {
        if (this.mBindings.bottomSheetLuContent.isExpanded()) {
            this.mBindings.bottomSheetLuContent.collapse();
        } else if (!noLUPresent.get()) {
            this.mShouldCloseActivity = true;
            BarcodeManager.removeCallback(barcodeScannerIstanceID);
            this.mViewModel.closeLU();
        } else {
            BarcodeManager.removeCallback(barcodeScannerIstanceID);
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_accettazione_ordine_inevaso, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_filter:
                showOrderByDialog();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void initVars() {
        this.noLUPresent.addOnPropertyChangedCallback(() -> {
            this.bottomSheetEnabled.set(!this.noLUPresent.get());
        });

        mCurrentOrderBy = AccettazioneOrdineInevasoOrderBy.Enum.fromInt(SettingsManager.i().getUserSession().getDefaultOrdinamentoPickingAccettazione());
    }

    private void initBarcodeReader() {
        barcodeScannerIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessful(onScanSuccessful)
                .setOnScanFailed(ex -> UtilityExceptions.defaultException(this, ex, false)));
    }

    private void initRecyclerView() {
        AccettazioneListAdapter accettazioneListAdapter = new AccettazioneListAdapter(this, mAccettazioneMutableData, mShowSecondaryUntMis);
        this.mBindings.accettazionePickingList.setAdapter(accettazioneListAdapter);
        this.mBindings.accettazionePickingList.setLayoutManager(new LinearLayoutManager(this));

        this.mViewModel.getPickingList().observe(this, updatedData -> {
            mAppliedFilterViewModel.init(updatedData);

            refreshList();
        });

        accettazioneListAdapter.setOnItemClicked(clickedItem -> {
            this.mViewModel.dispatchOrdineRow(clickedItem);
        });
    }

    private void refreshList() {
        //this.onLoadingStarted();

        //new Thread(() -> {

        runOnUiThread(() -> {
            List<PickingObjectDTO> tmpList;

            if (mAppliedFilterViewModel != null) {
                mAppliedFilterViewModel.applyAllTests();
                tmpList = mAppliedFilterViewModel.getMutableFilteredOrderList().getValue();
            } else {
                tmpList = mViewModel.getPickingList().getValue();
            }

            var list = convertDataModelToListModel(tmpList);

            this.mAccettazioneMutableData.clear();
            this.mAccettazioneMutableData.addAll(list);
            this.noItemsToPick.set(!isThereAnyItemToPick(tmpList));

            //this.onLoadingEnded();
        });
    }

    private List<AccettazioneListModel> convertDataModelToListModel(List<PickingObjectDTO> dataList) {
        List<PickingObjectDTO> tmpList = Stream.of(dataList)
                .filter(x -> !x.isHidden() && UtilityBigDecimal.greaterThan(x.getSitArtOrdDTO().getQtaDaEvadere(), BigDecimal.ZERO))
                .toList();

        List<AccettazioneListModel> sortedList = null;

        switch (mCurrentOrderBy) {
            case COD_ART_FOR:
                sortedList = convertDataModelToListModel__CodArtForn(tmpList);
                break;
            case DESCR_ART:
                sortedList = convertDataModelToListModel__DescrArt(tmpList);
                break;
            case RAG_SOC_COM:
                sortedList = convertDataModelToListModel__RagSocCom(tmpList);
                break;
        }

        return sortedList;
    }

    private List<AccettazioneListModel> convertDataModelToListModel__CodArtForn(List<PickingObjectDTO> dataList) {
        ArrayList<AccettazioneListModel> list = new ArrayList<>();
        Stream.of(dataList)
                .sortBy(x -> UtilityString.isNull(x.getSitArtOrdDTO().getCodArtFor(), x.getSitArtOrdDTO().getCodMart()) +
                        (UtilityString.isNullOrEmpty(x.getMtbAart().getDescrizioneEstesa()) ? "" : " - " + x.getMtbAart().getDescrizioneEstesa()))
                .forEach(x -> {
                    AccettazioneListModel accettazioneListModel = new AccettazioneListModel();
                    accettazioneListModel.setActive(true);

                    accettazioneListModel.setGroupTitle(UtilityString.isNull(x.getSitArtOrdDTO().getCodArtFor(), x.getSitArtOrdDTO().getCodMart()) +
                            (UtilityString.isNullOrEmpty(x.getMtbAart().getDescrizioneEstesa()) ? "" : " - " + x.getMtbAart().getDescrizioneEstesa()));

                    accettazioneListModel.setBadge1(UtilityString.isNull(x.getSitArtOrdDTO().getCodJcom(), CommonConst.Config.COMMESSA_MAG));
                    accettazioneListModel.setBadge2(String.valueOf(x.getSitArtOrdDTO().getNumOrd()));

                    accettazioneListModel.setDescrizione(x.getSitArtOrdDTO().getDescrizioneCommessa());
                    if (x.getMtbAart() != null)
                        accettazioneListModel.setPosizione(x.getMtbAart().getPosizione());

                    //Calc Num CNF
                    BigDecimal numCnfEvasa = BigDecimal.ZERO;

                    if (x.getWithdrawMtbColrs().size() > 0) {
                        numCnfEvasa = Stream.of(x.getWithdrawMtbColrs())
                                .map(MtbColr::getNumCnf)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                    }

                    //Calc qta col
                    BigDecimal qtaEvasa = BigDecimal.ZERO;

                    if (x.getWithdrawMtbColrs().size() > 0) {
                        qtaEvasa = Stream.of(x.getWithdrawMtbColrs())
                                .map(MtbColr::getQtaCol)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                    }


                    if (SettingsManager.iDB().isFlagForceAllToColli() || (x.getMtbAart() == null || !x.getMtbAart().isFlagQtaCnfFissaBoolean())) {
                        accettazioneListModel.setQtaEvasa(numCnfEvasa);
                        accettazioneListModel.setQtaTot(x.getSitArtOrdDTO().getNumCnfDaEvadere());
                        accettazioneListModel.setUntMis(UtilityResources.getString(R.string.unt_mis_col));

                        accettazioneListModel.setSecQtaEvasa(qtaEvasa);
                        accettazioneListModel.setSecQtaTot(x.getSitArtOrdDTO().getQtaDaEvadere());
                        if (x.getMtbAart() != null)
                            accettazioneListModel.setSecUntMis(x.getMtbAart().getUntMis());
                    } else {
                        accettazioneListModel.setQtaEvasa(qtaEvasa);
                        accettazioneListModel.setQtaTot(x.getSitArtOrdDTO().getQtaDaEvadere());
                        if (x.getMtbAart() != null)
                            accettazioneListModel.setUntMis(x.getMtbAart().getUntMis());

                        accettazioneListModel.setSecQtaEvasa(numCnfEvasa);
                        accettazioneListModel.setSecQtaTot(x.getSitArtOrdDTO().getNumCnfDaEvadere());
                        accettazioneListModel.setSecUntMis(UtilityResources.getString(R.string.unt_mis_col));
                    }

                    accettazioneListModel.setOriginalModel(x);

                    list.add(accettazioneListModel);
                });

        return list;
    }

    private List<AccettazioneListModel> convertDataModelToListModel__DescrArt(List<PickingObjectDTO> dataList) {
        ArrayList<AccettazioneListModel> list = new ArrayList<>();
        Stream.of(dataList)
                .sortBy(x -> x.getMtbAart().getDescrizioneEstesa())
                .forEach(x -> {
                    AccettazioneListModel accettazioneListModel = new AccettazioneListModel();
                    accettazioneListModel.setActive(true);

                    accettazioneListModel.setGroupTitle(UtilityString.isNull(x.getSitArtOrdDTO().getCodArtFor(), x.getSitArtOrdDTO().getCodMart())
                            + (UtilityString.isNullOrEmpty(x.getMtbAart().getDescrizioneEstesa()) ? "" : " - " + x.getMtbAart().getDescrizioneEstesa()));

                    accettazioneListModel.setBadge1(UtilityString.isNull(x.getSitArtOrdDTO().getCodJcom(), CommonConst.Config.COMMESSA_MAG));
                    accettazioneListModel.setBadge2(String.valueOf(x.getSitArtOrdDTO().getNumOrd()));

                    accettazioneListModel.setDescrizione(x.getSitArtOrdDTO().getDescrizioneCommessa());
                    if (x.getMtbAart() != null)
                        accettazioneListModel.setPosizione(x.getMtbAart().getPosizione());

                    //Calc Num CNF
                    BigDecimal numCnfEvasa = BigDecimal.ZERO;

                    if (x.getWithdrawMtbColrs().size() > 0) {
                        numCnfEvasa = Stream.of(x.getWithdrawMtbColrs())
                                .map(MtbColr::getNumCnf)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                    }

                    //Calc qta col
                    BigDecimal qtaEvasa = BigDecimal.ZERO;

                    if (x.getWithdrawMtbColrs().size() > 0) {
                        qtaEvasa = Stream.of(x.getWithdrawMtbColrs())
                                .map(MtbColr::getQtaCol)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                    }


                    if (SettingsManager.iDB().isFlagForceAllToColli() || (x.getMtbAart() == null || !x.getMtbAart().isFlagQtaCnfFissaBoolean())) {
                        accettazioneListModel.setQtaEvasa(numCnfEvasa);
                        accettazioneListModel.setQtaTot(x.getSitArtOrdDTO().getNumCnfDaEvadere());
                        accettazioneListModel.setUntMis(UtilityResources.getString(R.string.unt_mis_col));

                        accettazioneListModel.setSecQtaEvasa(qtaEvasa);
                        accettazioneListModel.setSecQtaTot(x.getSitArtOrdDTO().getQtaDaEvadere());
                        if (x.getMtbAart() != null)
                            accettazioneListModel.setSecUntMis(x.getMtbAart().getUntMis());
                    } else {
                        accettazioneListModel.setQtaEvasa(qtaEvasa);
                        accettazioneListModel.setQtaTot(x.getSitArtOrdDTO().getQtaDaEvadere());
                        if (x.getMtbAart() != null)
                            accettazioneListModel.setUntMis(x.getMtbAart().getUntMis());

                        accettazioneListModel.setSecQtaEvasa(numCnfEvasa);
                        accettazioneListModel.setSecQtaTot(x.getSitArtOrdDTO().getNumCnfDaEvadere());
                        accettazioneListModel.setSecUntMis(UtilityResources.getString(R.string.unt_mis_col));
                    }

                    accettazioneListModel.setOriginalModel(x);

                    list.add(accettazioneListModel);
                });

        return list;
    }

    private List<AccettazioneListModel> convertDataModelToListModel__RagSocCom(List<PickingObjectDTO> dataList) {
        ArrayList<AccettazioneListModel> list = new ArrayList<>();

        Stream.of(dataList)
                .filter(x -> !UtilityString.isNullOrEmpty(x.getSitArtOrdDTO().getDescrizioneCommessa()) && !x.getSitArtOrdDTO().getCodJcom().equalsIgnoreCase(CommonConst.Config.COMMESSA_MAG))
                .sortBy(x -> x.getSitArtOrdDTO().getDescrizioneCommessa())
                .forEach(x -> {
                    AccettazioneListModel accettazioneListModel = new AccettazioneListModel();
                    accettazioneListModel.setActive(true);

                    accettazioneListModel.setGroupTitle(x.getSitArtOrdDTO().getCodJcom() + (!UtilityString.isNullOrEmpty(x.getSitArtOrdDTO().getDescrizioneCommessa()) ? " - " + x.getSitArtOrdDTO().getDescrizioneCommessa() : ""));

                    accettazioneListModel.setBadge1(UtilityString.isNull(x.getSitArtOrdDTO().getCodArtFor(), x.getSitArtOrdDTO().getCodMart()));
                    accettazioneListModel.setBadge2(String.valueOf(x.getSitArtOrdDTO().getNumOrd()));

                    //Calc Num CNF
                    BigDecimal numCnfEvasa = BigDecimal.ZERO;

                    if (x.getWithdrawMtbColrs().size() > 0) {
                        numCnfEvasa = Stream.of(x.getWithdrawMtbColrs())
                                .map(MtbColr::getNumCnf)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                    }

                    //Calc qta col
                    BigDecimal qtaEvasa = BigDecimal.ZERO;

                    if (x.getWithdrawMtbColrs().size() > 0) {
                        qtaEvasa = Stream.of(x.getWithdrawMtbColrs())
                                .map(MtbColr::getQtaCol)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                    }


                    if (SettingsManager.iDB().isFlagForceAllToColli() || (x.getMtbAart() == null || !x.getMtbAart().isFlagQtaCnfFissaBoolean())) {
                        accettazioneListModel.setQtaEvasa(numCnfEvasa);
                        accettazioneListModel.setQtaTot(x.getSitArtOrdDTO().getNumCnfDaEvadere());
                        accettazioneListModel.setUntMis(UtilityResources.getString(R.string.unt_mis_col));

                        accettazioneListModel.setSecQtaEvasa(qtaEvasa);
                        accettazioneListModel.setSecQtaTot(x.getSitArtOrdDTO().getQtaDaEvadere());
                        if (x.getMtbAart() != null)
                            accettazioneListModel.setSecUntMis(x.getMtbAart().getUntMis());
                    } else {
                        accettazioneListModel.setQtaEvasa(qtaEvasa);
                        accettazioneListModel.setQtaTot(x.getSitArtOrdDTO().getQtaDaEvadere());
                        if (x.getMtbAart() != null)
                            accettazioneListModel.setUntMis(x.getMtbAart().getUntMis());

                        accettazioneListModel.setSecQtaEvasa(numCnfEvasa);
                        accettazioneListModel.setSecQtaTot(x.getSitArtOrdDTO().getNumCnfDaEvadere());
                        accettazioneListModel.setSecUntMis(UtilityResources.getString(R.string.unt_mis_col));
                    }

                    if (x.getMtbAart() != null) {
                        accettazioneListModel.setDescrizione(x.getMtbAart().getDescrizioneEstesa());
                        accettazioneListModel.setPosizione(x.getMtbAart().getPosizione());
                    }

                    accettazioneListModel.setOriginalModel(x);

                    list.add(accettazioneListModel);
                });

        Stream.of(dataList)
                .filter(x -> UtilityString.isNullOrEmpty(x.getSitArtOrdDTO().getDescrizioneCommessa()) || x.getSitArtOrdDTO().getCodJcom().equalsIgnoreCase(CommonConst.Config.COMMESSA_MAG))
                .forEach(x -> {
                    AccettazioneListModel accettazioneListModel = new AccettazioneListModel();
                    accettazioneListModel.setActive(true);

                    accettazioneListModel.setGroupTitle(UtilityString.isNull(x.getSitArtOrdDTO().getCodJcom(), CommonConst.Config.COMMESSA_MAG));

                    accettazioneListModel.setBadge1(UtilityString.isNull(x.getSitArtOrdDTO().getCodArtFor(), x.getSitArtOrdDTO().getCodMart()));
                    accettazioneListModel.setBadge2(String.valueOf(x.getSitArtOrdDTO().getNumOrd()));


                    //Calc Num CNF
                    BigDecimal numCnfEvasa = BigDecimal.ZERO;

                    if (x.getWithdrawMtbColrs().size() > 0) {
                        numCnfEvasa = Stream.of(x.getWithdrawMtbColrs())
                                .map(MtbColr::getNumCnf)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                    }

                    //Calc qta col
                    BigDecimal qtaEvasa = BigDecimal.ZERO;

                    if (x.getWithdrawMtbColrs().size() > 0) {
                        qtaEvasa = Stream.of(x.getWithdrawMtbColrs())
                                .map(MtbColr::getQtaCol)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                    }


                    if (SettingsManager.iDB().isFlagForceAllToColli() || (x.getMtbAart() == null || !x.getMtbAart().isFlagQtaCnfFissaBoolean())) {
                        accettazioneListModel.setQtaEvasa(numCnfEvasa);
                        accettazioneListModel.setQtaTot(x.getSitArtOrdDTO().getNumCnfDaEvadere());
                        accettazioneListModel.setUntMis(UtilityResources.getString(R.string.unt_mis_col));

                        accettazioneListModel.setSecQtaEvasa(qtaEvasa);
                        accettazioneListModel.setSecQtaTot(x.getSitArtOrdDTO().getQtaDaEvadere());
                        if (x.getMtbAart() != null)
                            accettazioneListModel.setSecUntMis(x.getMtbAart().getUntMis());
                    } else {
                        accettazioneListModel.setQtaEvasa(qtaEvasa);
                        accettazioneListModel.setQtaTot(x.getSitArtOrdDTO().getQtaDaEvadere());
                        if (x.getMtbAart() != null)
                            accettazioneListModel.setUntMis(x.getMtbAart().getUntMis());

                        accettazioneListModel.setSecQtaEvasa(numCnfEvasa);
                        accettazioneListModel.setSecQtaTot(x.getSitArtOrdDTO().getNumCnfDaEvadere());
                        accettazioneListModel.setSecUntMis(UtilityResources.getString(R.string.unt_mis_col));
                    }


                    if (x.getMtbAart() != null) {
                        accettazioneListModel.setDescrizione(x.getMtbAart().getDescrizioneEstesa());
                        accettazioneListModel.setUntMis(x.getMtbAart().getUntMis());
                    }

                    accettazioneListModel.setOriginalModel(x);

                    list.add(accettazioneListModel);
                });

        return list;
    }

    private boolean isThereAnyItemToPick(List<PickingObjectDTO> dataList) {
        return Stream.of(dataList)
                .anyMatch(x -> !x.isHidden());
    }

    private final RunnableArgs<BarcodeScanDTO> onScanSuccessful = data -> {
        this.onLoadingStarted();

        this.mViewModel.processBarcodeDTO(data, () -> {
            this.onLoadingEnded();
        });
    };

    public void removeListFilter() {
        this.mViewModel.resetMatchedRows();
    }


    public void createNewLU() {
        this.mBindings.accettazioneOrdineInevasoFab.close(true);
        this.onLoadingStarted();

        this.mViewModel.createNewLU(null, null, false, () -> {
            this.onLoadingEnded();
        });
    }

    public void showCreatedUL() {
        this.mBindings.accettazioneOrdineInevasoFab.close(true);

        this.mViewModel.retrieveExistentLU(this::startListaBancaliRegistratiActivity);
    }

    private void showOrderByDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle(this.getText(R.string.action_orderBy))
                .setSingleChoiceItems(AccettazioneOrdineInevasoOrderBy.descriptions, mCurrentOrderBy.getVal(), (dialog12, which) -> {
                    mCurrentOrderBy = AccettazioneOrdineInevasoOrderBy.Enum.fromInt(which);
                    SettingsManager.i().getUserSession().setDefaultOrdinamentoPickingAccettazione(which);
                })
                .setPositiveButton(getText(R.string.ok), (dialog1, which) -> this.refreshList())
                .create();
        dialog.show();
    }

    public void startListaBancaliRegistratiActivity(ArrayList<RecoverMtbColt> mtbColts) {

        Intent myIntent = ListaBancaliActivity.createIntent(this,
                Stream.of(mtbColts).map(x -> (MtbColt) x).toList(),
                input -> ((RecoverMtbColt) input).isFlagCanBeRecovered(),
                ReportManager.getReportNameLUFromGestione(GestioneEnum.ACQUISTO));
        this.startActivityForResult(myIntent, PICK_UL_REQUEST);
    }

    @Override
    public void onLUSuccessullyPrinted() {
        Resources res = getResources();
        String errorMessage = res.getText(R.string.alert_print_completed_message).toString();
        DialogSimpleMessageView
                .makeSuccessDialog(res.getText(R.string.completed).toString(), new SpannableString(errorMessage), null, null)
                .show(getSupportFragmentManager(), "tag");
    }

    @Override
    public void onLUPrintRequest(RunnableArgs<Boolean> onComplete) {
    }

    @Override
    public void onLUPrintError(Exception ex, Runnable onComplete) {
        this.onLoadingEnded();
        DialogSimpleMessageView.makeErrorDialog(
                new SpannableString(ex.getMessage()),
                null,
                null,
                R.string.button_ignore_print,
                onComplete)
                .show(getSupportFragmentManager(), "tag");
    }

    @Override
    public void onMtbColrEdit(MtbColr mtbColr) {
        this.mViewModel.dispatchRowEdit(mtbColr);
    }

    @Override
    public void onMtbColrDelete(MtbColr mtbColr) {
        this.mViewModel.deleteRow(mtbColr);
    }

    @Override
    public void onBottomSheetLUClose() {
        this.mViewModel.closeLU();
    }

    @Override
    public void onVersamentoAutomaticoULRequest(RunnableArgs<Boolean> onComplete) {
        runOnUiThread(() -> {
            DialogAskShouldVersamentoAutomaticoULView.newInstance(onComplete)
                    .show(getSupportFragmentManager(), "tag");
        });
    }

    @Override
    public void onInfoAggiuntiveRequest(RunnableArgss<String, MtbTCol> onComplete) {
        runOnUiThread(() -> {
            DialogInfoAggiuntiveLU.newInstance(onComplete).show(getSupportFragmentManager(), "InfoAggiuntiveLUDialog");
        });
    }

    @Override
    public void onError(Exception ex) {
        this.onLoadingEnded();

        if (ex instanceof InvalidPesoKGException) {
            UtilityToast.showToast(ex.getMessage());
        } else {
            UtilityExceptions.defaultException(this, ex);
        }

        BarcodeManager.enable();
    }

    @Override
    public void onItemDispatched(PickingObjectDTO pickingObjectDTO, MtbAart mtbAart, BigDecimal initialNumCnf, BigDecimal initialQtaCnf, BigDecimal initialQtaTot, BigDecimal totalQtaOrd, BigDecimal totalNumCnfOrd, BigDecimal qtaCnfOrd, BigDecimal totalQtaToBeTaken, BigDecimal totalNumCnfToBeTaken, BigDecimal qtaCnfToBeTaken, BigDecimal totalQtaAvailable, BigDecimal totalNumCnfAvailable, BigDecimal qtaCnfAvailable, String partitaMag, Date dataScad, boolean canPartitaMagBeChanged, boolean canOverflowQuantity, RunnableArgss<PickedQuantityDTO, Boolean> onComplete) {
        DialogInputQuantityV2DTO dialogInputQuantityV2DTO = new DialogInputQuantityV2DTO()
                .setMtbAart(mtbAart)
                .setInitialNumCnf(initialNumCnf)
                .setInitialQtaCnf(initialQtaCnf)
                .setInitialQtaTot(initialQtaTot)
                .setTotalQtaOrd(totalQtaOrd)
                .setTotalNumCnfOrd(totalNumCnfOrd)
                .setQtaCnfOrd(qtaCnfOrd)
                .setTotalQtaToBeTaken(totalQtaToBeTaken)
                .setTotalNumCnfToBeTaken(totalNumCnfToBeTaken)
                .setQtaCnfToBeTaken(qtaCnfToBeTaken)
                .setTotalQtaAvailable(totalQtaAvailable)
                .setTotalNumCnfAvailable(totalNumCnfAvailable)
                .setQtaCnfAvailable(qtaCnfAvailable)
                .setPartitaMag(partitaMag)
                .setDataScad(dataScad)
                .setCanPartitaMagBeChanged(canPartitaMagBeChanged)
                .setCanLUBeClosed(true)
                .setCanOverflowOrderQuantity(canOverflowQuantity);

        if (!mDialogInputQuantityV2View.isVisible())
            mDialogInputQuantityV2View
                    .setDialogInputQuantityV2DTO(dialogInputQuantityV2DTO)
                    .setOnComplete((resultDTO, shouldCloseLU) -> {
                        PickedQuantityDTO pickedQuantityDTO = new PickedQuantityDTO()
                                .setNumCnf(resultDTO.getNumCnf())
                                .setQtaCnf(resultDTO.getQtaCnf())
                                .setQtaTot(resultDTO.getQtaTot())
                                .setPartitaMag(resultDTO.getPartitaMag())
                                .setDataScad(resultDTO.getDataScad());
                        onComplete.run(pickedQuantityDTO, shouldCloseLU);
                    })
                    .setOnAbort(() -> this.mViewModel.resetMatchedRows())
                    .show(getSupportFragmentManager(), "tag");
    }

    @Override
    public void onRowSaved() {
        runOnUiThread(() -> {
            FBToast.successToast(this, getResources().getString(R.string.data_saved), FBToast.LENGTH_SHORT);
        });
    }

    @Override
    public void onFilterCodMartApplied(String codMartToFilter) {
        runOnUiThread(() -> {
            var codMarts = new ArrayList<String>();
            codMarts.add(codMartToFilter);

            this.mAppliedFilterViewModel.setCodArtFilter(codMarts);
        });
    }

    @Override
    public void onFilterPosizioneApplied(String posizioneToFilter) {
        runOnUiThread(() -> {
            var posizioni = new ArrayList<String>();
            posizioni.add(posizioneToFilter);

            this.mAppliedFilterViewModel.setPosFilter(posizioni);
        });
    }

    @Override
    public void onULVersata(VersamentoAutomaticoULResponseDTO versamentoAutomaticoULResponseDTO, Runnable onComplete) {

        runOnUiThread(() -> {
            DialogVersamentoAutomaticoULDoneView
                    .newInstance(versamentoAutomaticoULResponseDTO, onComplete)
                    .show(getSupportFragmentManager(), "tag");
        });

    }

    @Override
    public void onMtbColrDeleteRequest(RunnableArgs<Boolean> onComplete) {
        runOnUiThread(() -> {
            String text = getResources().getString(R.string.alert_delete_mtb_colr);
            DialogSimpleMessageView.makeWarningDialog(new SpannableString(text),
                    null,
                    () -> onComplete.run(true),
                    () -> onComplete.run(false)
            ).show(getSupportFragmentManager(), "tag");
        });
    }

    @Override
    public void onLUOpened(MtbColt mtbColt) {
        runOnUiThread(() -> {
            noLUPresent.set(false);
            FBToast.successToast(this, getResources().getString(R.string.data_saved), FBToast.LENGTH_SHORT);

            this.mBottomSheetFragmentLUContentViewModel.setMtbColt(mtbColt);
        });
    }

    @Override
    public void onLUClosed() {
        runOnUiThread(() -> {
            noLUPresent.set(true);
            this.mBottomSheetFragmentLUContentViewModel.setMtbColt(null);

            if (this.mShouldCloseActivity) super.onBackPressed();
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Check which request we're responding to
        if (requestCode == PICK_UL_REQUEST) {

            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                MtbColt recoveredMtbColt = DataCache.retrieveItem(data.getStringExtra("key"));
                this.mViewModel.recoverLU(recoveredMtbColt);
            }
        }
    }
}
