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

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ObservableField;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.expansion.RunnableArgss;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.model.MtbTCol;
import it.integry.integrywmsnative.core.utility.UtilityObservable;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogInfoAggiuntiveLuBinding;
import it.integry.integrywmsnative.view.dialogs.input_lu_prod.adapter.DialogInputLUProdTipoColloAdapter;
import it.integry.integrywmsnative.view.dialogs.input_lu_prod.adapter.DialogInputLUProdTipoColloListModel;

public class DialogInfoAggiuntiveLUView extends BaseDialogFragment implements DialogInfoAggiuntiveLUViewModel.Listener {

    @Inject
    DialogInfoAggiuntiveLUViewModel mViewModel;


    public ObservableField<String> additionalNotes = new ObservableField<>();
    public ObservableField<String> selectedMtbTcol = new ObservableField<>();
    public ObservableField<DialogInputLUProdTipoColloListModel> currentMtbTCol = new ObservableField<>();


    private DialogInfoAggiuntiveLuBinding mBindings;
    private Context mContext;


    private final RunnableArgss<String, MtbTCol> mOnComplete;
    private final Runnable mOnAbort;
    private final MtbColt currentMtbColt;


    public static DialogInfoAggiuntiveLUView newInstance(@NotNull RunnableArgss<String, MtbTCol> onComplete, @NotNull Runnable onAbort) {
        return new DialogInfoAggiuntiveLUView(null, onComplete, onAbort);
    }

    public static DialogInfoAggiuntiveLUView newInstance(MtbColt mtbColt, @NotNull RunnableArgss<String, MtbTCol> onComplete, @NotNull Runnable onAbort) {
        return new DialogInfoAggiuntiveLUView(mtbColt, onComplete, onAbort);
    }

    private DialogInfoAggiuntiveLUView(MtbColt mtbColt, @NotNull RunnableArgss<String, MtbTCol> onComplete, @NotNull Runnable onAbort) {
        super();
        this.mOnComplete = onComplete;
        this.mOnAbort = onAbort;
        this.currentMtbColt = mtbColt;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        this.mContext = requireContext();

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

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

        mBindings.setView(this);
        mBindings.setViewModel(mViewModel);
        mViewModel.setListener(this);

        setCancelable(false);

        var alertDialog = new MaterialAlertDialogBuilder(this.mContext)
                .setView(mBindings.getRoot())
                .setCancelable(isCancelable())
                .setPositiveButton(R.string.confirm, (dialogInterface, i) -> {
                    mOnComplete.run(additionalNotes.get(), currentMtbTCol.get() != null ? currentMtbTCol.get().getOriginalModel() : null);
                })
                .setNegativeButton(R.string.abort, (dialogInterface, i) -> {
                    this.mOnAbort.run();
                })
                .create();

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


    @Override
    public void onShow(DialogInterface dialogInterface) {
        super.onShow(dialogInterface);

        this.init();
        this.mViewModel.init();
    }


    private void init() {
        if (this.currentMtbColt != null)
            this.additionalNotes.set(currentMtbColt.getAnnotazioni());

        var codTcolArrayAdapter = new DialogInputLUProdTipoColloAdapter(requireContext(),
                R.layout.array_adapter_single_item,
                this.mViewModel.getTipiCollo());

        mBindings.filledExposedDropdownCodTcol.setAdapter(codTcolArrayAdapter);

        this.mViewModel.getTipiCollo().observe(this, tipiCollo -> {

            if (currentMtbColt != null && !UtilityString.isNullOrEmpty(currentMtbColt.getCodTcol()) && UtilityString.isNullOrEmpty(this.selectedMtbTcol.get())) {
                tipiCollo.stream()
                        .filter(x -> x.getCodTcol().equalsIgnoreCase(currentMtbColt.getCodTcol()))
                        .findFirst()
                        .ifPresent(tipoPedana -> this.selectedMtbTcol.set(tipoPedana.getCodTcol() + " - " + tipoPedana.getDescrizione()));
            }
        });


        UtilityObservable.addPropertyChanged(this.selectedMtbTcol, (value) -> {
            if (codTcolArrayAdapter.getDataset() != null) {
                codTcolArrayAdapter.getDataset().stream()
                        .filter(x -> x.toString().equals(value))
                        .findFirst()
                        .ifPresent(tipoPedana -> {
                            currentMtbTCol.set(tipoPedana);
                        });

            }
        });


        this.mBindings.filledExposedDropdownCodTcol.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                currentMtbTCol.set(codTcolArrayAdapter.getItem(i));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                currentMtbTCol.set(null);
            }
        });



    }
}
