package it.integry.integrywmsnative.gest.vendita.viewmodel;

import android.app.Activity;
import android.app.ProgressDialog;
import android.text.Html;
import android.text.SpannableString;

import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;

import java.util.ArrayList;
import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.REST.consumers.BarcodeRESTConsumer;
import it.integry.integrywmsnative.core.REST.consumers.ColliMagazzinoRESTConsumer;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.DtbOrdt;
import it.integry.integrywmsnative.core.model.secondary.GestioneEnum;
import it.integry.integrywmsnative.core.utility.UtilityBarcode;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityLogger;
import it.integry.integrywmsnative.core.utility.UtilityPosizione;
import it.integry.integrywmsnative.core.utility.UtilityProgress;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.core.utility.UtilityToast;
import it.integry.integrywmsnative.databinding.FragmentMainVenditaBinding;
import it.integry.integrywmsnative.gest.vendita.MainVenditaFragment;
import it.integry.integrywmsnative.gest.vendita.core.VenditaHelper;
import it.integry.integrywmsnative.gest.vendita.dialogs.DialogVenditaFiltroAvanzatoViewModel;
import it.integry.integrywmsnative.gest.vendita.dto.OrdineVenditaInevasoDTO;
import it.integry.integrywmsnative.gest.vendita.dto.PickingObjectDTO;
import it.integry.integrywmsnative.view.dialogs.DialogSimpleMessageHelper;
import it.integry.plugins.barcode_base_library.model.BarcodeScanDTO;

public class VenditaViewModel {

    private Activity mContext;
    private MainVenditaFragment mFragment;
    private FragmentMainVenditaBinding mBinding;
    private VenditaHelper mHelper;

    private List<OrdineVenditaInevasoDTO> mOrdini = new ArrayList<>();

    public VenditaViewModel(Activity context, MainVenditaFragment fragment, FragmentMainVenditaBinding binding, VenditaHelper helper) {
        mContext = context;
        mFragment = fragment;
        mBinding = binding;
        mHelper = helper;
    }

    public void setListOrder(List<OrdineVenditaInevasoDTO> ordini) {
        mOrdini = ordini;
    }


    public RunnableArgs<BarcodeScanDTO> onScanSuccessfull = data -> {

        BarcodeManager.disable();
        final ProgressDialog progressDialog = UtilityProgress.createDefaultProgressDialog(mContext);

        if(UtilityBarcode.isEtichetta128(data)){
            this.executeEtichettaEan128(data, progressDialog);
        } else {
            progressDialog.dismiss();
            BarcodeManager.enable();
        }

    };



    private void executeEtichettaEan128(BarcodeScanDTO barcodeScanDTO, ProgressDialog progressDialog) {
        BarcodeRESTConsumer.decodeEan128(barcodeScanDTO, ean128Model -> {

            String barcodeProd = null;

            if(!UtilityString.isNullOrEmpty(ean128Model.Sscc)) barcodeProd = ean128Model.Sscc;
            if(!UtilityString.isNullOrEmpty(ean128Model.Gtin)) barcodeProd = ean128Model.Gtin;
            if(!UtilityString.isNullOrEmpty(ean128Model.Content)) barcodeProd = ean128Model.Content;


            if(!UtilityString.isNullOrEmpty(barcodeProd)) {

                if(!UtilityString.isNullOrEmpty(ean128Model.Sscc)){
                    this.executeEtichettaLU(ean128Model.Sscc, progressDialog);
                } else {
                    showNoULFound();
                    progressDialog.dismiss();
                    BarcodeManager.enable();

                }


            } else {
                //EAN 128 non completo o comunque mancano i riferimenti al prodotto
                showNoULFound();
                progressDialog.dismiss();
                BarcodeManager.enable();
            }
        }, ex-> {
            UtilityExceptions.defaultException(mContext, ex, progressDialog);
            BarcodeManager.enable();
        });
    }


    private void executeEtichettaLU(String SSCC, ProgressDialog progressDialog) {
        ColliMagazzinoRESTConsumer.getBySSCC(SSCC, true, false, mtbColt -> {

            if(mtbColt != null && mtbColt.getMtbColr() != null && mtbColt.getMtbColr().size() > 0) {

                if(mtbColt.getGestioneEnum() == GestioneEnum.ACQUISTO) {
                    //GET BY COMMESSA COLLO
                    mHelper.getOrdiniInCommessaCollo(mtbColt, orderList -> {

                        if(orderList != null && orderList.size() > 0) {
                            UtilityToast.showToast("Caricati " + orderList.size() + " ordini");

                            List<Integer> numOrds = Stream.of(orderList)
                                    .map(DtbOrdt::getNumOrd)
                                    .toList();

                            List<OrdineVenditaInevasoDTO> filteredOrders = Stream.of(mOrdini)
                                    .filter(x -> numOrds.contains(x.getNumOrd())).toList();

                            mFragment.groupOrdini(filteredOrders);
                            mFragment.refreshRecyclerView();
                        } else {
                            showNoOrderFound();
                        }

                        progressDialog.dismiss();
                        BarcodeManager.enable();



                    }, ex -> {
                        UtilityExceptions.defaultException(mContext, ex, progressDialog);
                        BarcodeManager.enable();
                    });

                    progressDialog.dismiss();
                    BarcodeManager.enable();

                } else {
                    progressDialog.dismiss();
                    BarcodeManager.enable();
                    showWrongGestioneUL();
                }

            } else {
                progressDialog.dismiss();
                BarcodeManager.enable();
                showNoULFound();
            }


        }, ex -> {
            UtilityExceptions.defaultException(mContext, ex, progressDialog);
            BarcodeManager.enable();
        });
    }



    private void showNoOrderFound() {
        DialogSimpleMessageHelper.makeWarningDialog(mContext,
                new SpannableString(mContext.getResources().getText(R.string.no_orders_found_message)),
                null, null).show();
    }


    private void showNoULFound() {
        DialogSimpleMessageHelper.makeWarningDialog(mContext,
                new SpannableString(mContext.getResources().getText(R.string.no_lu_found_message)),
                null, null).show();
    }


    private void showWrongGestioneUL() {
        DialogSimpleMessageHelper.makeWarningDialog(mContext,
                new SpannableString(Html.fromHtml(mContext.getResources().getText(R.string.gestione_V_not_accepted_message).toString())),
                null, null).show();
    }


}
