package it.integry.integrywmsnative.view.dialogs.input_peso_lu;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import java.math.BigDecimal;
import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.di.BindableString;
import it.integry.integrywmsnative.core.expansion.RunnableArgsss;
import it.integry.integrywmsnative.core.model.MtbTCol;
import it.integry.integrywmsnative.core.rest.consumers.MagazzinoRESTConsumer;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityNumber;
import it.integry.integrywmsnative.databinding.DialogInputPesoLuBinding;

public class DialogInputPesoLuView extends DialogFragment {

    private DialogInputPesoLuBinding mBindings;
    private RunnableArgsss<String, BigDecimal, BigDecimal> onDialogDismiss;

    public BindableString codTcol = new BindableString();
    public BindableString netWeight = new BindableString();
    public BindableString grossWeight = new BindableString();


    private List<MtbTCol> codTcolList = null;
    private ArrayAdapter<String> codTcolArrayAdapter = null;


    public DialogInputPesoLuView(String codTcol, BigDecimal netWeight, BigDecimal grossWeight, RunnableArgsss<String, BigDecimal, BigDecimal> onDialogDismiss) {
        this.onDialogDismiss = onDialogDismiss;

        this.codTcol.set(codTcol);
        this.netWeight.set(UtilityNumber.decimalToString(netWeight));
        this.grossWeight.set(UtilityNumber.decimalToString(grossWeight));
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        mBindings = DialogInputPesoLuBinding.inflate(LayoutInflater.from(requireContext()), null, false);
        mBindings.setView(this);

        this.init();

        var alertDialog =  new MaterialAlertDialogBuilder(requireContext())
                .setView(mBindings.getRoot())
                .setCancelable(false)
                .setPositiveButton(R.string.confirm, (dialog, which) -> {
                    String newCodTcol = null;

                    if(codTcol.get() != null) {
                        Optional<MtbTCol> newCodTcolOptional = Stream.of(codTcolList)
                                .filter(x -> (x.getCodTcol() + " - " + x.getDescrizione()).equalsIgnoreCase(codTcol.get()))
                                .findSingle();

                        if(newCodTcolOptional.isPresent()) newCodTcol = newCodTcolOptional.get().getCodTcol();
                    }


                    BigDecimal netWeightDecimal = new BigDecimal(netWeight.get());
                    BigDecimal grossWeightDecimal = new BigDecimal(grossWeight.get());

                    onDialogDismiss.run(newCodTcol, netWeightDecimal, grossWeightDecimal);
                })
                .create();
        alertDialog.setCanceledOnTouchOutside(false);
        return alertDialog;
    }

    private void init() {

//        final DialogProgressView progressDialog = UtilityProgress.createDefaultProgressDialog(mContext);

        MagazzinoRESTConsumer.getTipiColloStatic(codTcols -> {
            this.codTcolList = codTcols;

            List<String> codTcolDescriptions = Stream.of(codTcols)
                    .map(codTcol -> codTcol.getCodTcol() + " - " + codTcol.getDescrizione())
                    .toList();

            if(codTcol.get() != null) {
                Optional<String> newCodTcol = Stream.of(codTcols)
                        .filter(x -> x.getCodTcol().equalsIgnoreCase(codTcol.get()))
                        .map(codTcol -> codTcol.getCodTcol() + " - " + codTcol.getDescrizione())
                        .findSingle();

                if(newCodTcol.isPresent()) codTcol.set(newCodTcol.get());
            }

            codTcolArrayAdapter = new ArrayAdapter(requireContext(), R.layout.array_adapter_single_item);
            codTcolArrayAdapter.addAll(codTcolDescriptions);
            mBindings.filledExposedDropdownCodTcol.setAdapter(codTcolArrayAdapter);

//            progressDialog.dismiss();

        }, ex -> {
            UtilityExceptions.defaultException(requireContext(), ex);
        });

    }


}
