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

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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 javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.di.BindableString;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
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 BaseDialogFragment {

    @Inject
    DialogInputPesoLUViewModel mViewModel;

    @Inject
    MagazzinoRESTConsumer magazzinoRESTConsumer;

    private DialogInputPesoLuBinding mBindings;
    private Context mContext;

    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;


    //Pass here all external parameters
    public static DialogInputPesoLUView newInstance(String codTcol, BigDecimal netWeight, BigDecimal grossWeight, RunnableArgsss<String, BigDecimal, BigDecimal> onDialogDismiss) {
        return new DialogInputPesoLUView(codTcol, netWeight, grossWeight, onDialogDismiss);
    }

    private DialogInputPesoLUView(String codTcol, BigDecimal netWeight, BigDecimal grossWeight, RunnableArgsss<String, BigDecimal, BigDecimal> onDialogDismiss) {
        super();
        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) {
        this.mContext = requireContext();

        mBindings = DialogInputPesoLuBinding.inflate(LayoutInflater.from(this.mContext), null, false);
        mBindings.setLifecycleOwner(this);
        mBindings.setView(this);

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

        this.init();

        setCancelable(false);

        var alertDialog = new MaterialAlertDialogBuilder(this.mContext)
                .setView(mBindings.getRoot())
                .setCancelable(isCancelable())
                .create();

        alertDialog.setCanceledOnTouchOutside(isCancelable());
        alertDialog.setOnShowListener(this);
        return alertDialog;
    }



    private void init() {

        magazzinoRESTConsumer.getTipiCollo(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);
        });

    }

}