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

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

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

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

import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;

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.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.utility.UtilityFocus;
import it.integry.integrywmsnative.databinding.DialogInputQuantityToReturnBinding;

public class DialogInputQuantityToReturnView extends BaseDialogFragment {

    @Inject
    DialogInputQuantityToReturnViewModel mViewModel;

    private DialogInputQuantityToReturnBinding mBindings;
    private Context mContext;


    private final MtbAart mtbAart;
    private final BigDecimal minQuantity;
    private final BigDecimal maxQuantity;
    private final RunnableArgs<BigDecimal> onDialogDismiss;

    public static DialogInputQuantityToReturnView newInstance(@NotNull MtbAart mtbAart, BigDecimal minQuantity, BigDecimal maxQuantity, RunnableArgs<BigDecimal> onDialogDismiss) {
        return new DialogInputQuantityToReturnView(mtbAart, minQuantity, maxQuantity, onDialogDismiss);
    }

    private DialogInputQuantityToReturnView(@NotNull MtbAart mtbAart, BigDecimal minQuantity, BigDecimal maxQuantity, RunnableArgs<BigDecimal> onDialogDismiss) {
        super();

        this.mtbAart = mtbAart;
        this.minQuantity = minQuantity;
        this.maxQuantity = maxQuantity;
        this.onDialogDismiss = onDialogDismiss;
    }


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

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

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

        mViewModel.init(mtbAart, minQuantity, maxQuantity);
        mBindings.setViewModel(mViewModel);

        setCancelable(false);

        var alertDialog = new MaterialAlertDialogBuilder(this.mContext)
                .setView(mBindings.getRoot())
                .setCancelable(isCancelable())
                .setPositiveButton(R.string.confirm, (dialogInterface, i) -> {
                    onDialogDismiss.run(mViewModel.getInputQuanity());
                })
                .setNegativeButton(R.string.abort, (dialogInterface, i) -> {
                    onDialogDismiss.run(null);
                })
                .create();

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

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

        UtilityFocus.focusTextBox(requireContext(), mBindings.additionalNotesText);
    }
}