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

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;

import androidx.databinding.DataBindingUtil;

import com.annimon.stream.Optional;
import com.annimon.stream.Stream;

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.core.utility.UtilityProgress;
import it.integry.integrywmsnative.databinding.DialogInputPesoLuBinding;

public class DialogInputPeso {


    private Context mContext;
    private DialogInputPesoLuBinding mBindings;
    private DialogInputPesoViewModel mViewModel;
    private Dialog currentDialog;

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

    public static Dialog make(final Context context, String codTcol, BigDecimal netWeight, BigDecimal grossWeight, RunnableArgsss<String, BigDecimal, BigDecimal> onDialogDismiss) {
        return new DialogInputPeso(context, codTcol, netWeight, grossWeight, onDialogDismiss).currentDialog;
    }


    private DialogInputPeso(final Context context, String codTcol, BigDecimal netWeight, BigDecimal grossWeight, RunnableArgsss<String, BigDecimal, BigDecimal> onDialogDismiss) {
        this.mContext = context;


        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mBindings = DataBindingUtil.inflate(inflater, R.layout.dialog_input_peso_lu, null, false);

        currentDialog = new Dialog(context);
        currentDialog.setContentView(mBindings.getRoot());

        currentDialog.setCanceledOnTouchOutside(false);
        currentDialog.setCancelable(false);
        currentDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        mViewModel = new DialogInputPesoViewModel();
        mViewModel.codTcol.set(codTcol);
        mViewModel.netWeight.set(UtilityNumber.decimalToString(netWeight));
        mViewModel.grossWeight.set(UtilityNumber.decimalToString(grossWeight));

        mBindings.setViewModel(mViewModel);

        mBindings.buttonConfirm.setOnClickListener(v -> {
            currentDialog.dismiss();

            String newCodTcol = null;

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

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


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

            onDialogDismiss.run(newCodTcol, netWeightDecimal, grossWeightDecimal);
        });


        this.init();
    }




    private void init() {

        final Dialog progressDialog = UtilityProgress.createDefaultProgressDialog(mContext);

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

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

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

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

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

            progressDialog.dismiss();

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

    }


    public class DialogInputPesoViewModel {
        public BindableString codTcol = new BindableString();
        public BindableString netWeight = new BindableString();
        public BindableString grossWeight = new BindableString();

    }


}
