package it.integry.integrywmsnative.gest.inventario.dialogs.ask_info_inventario;

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

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

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

import org.apache.commons.lang3.StringUtils;

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.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogAskInfoInventarioBinding;

public class DialogAskInfoInventarioView extends BaseDialogFragment {

    @Inject
    DialogAskInfoInventarioViewModel mViewModel;

    private final RunnableArgss<Long, String> onConfirmed;
    private final Runnable onAbort;

    public ObservableField<Long> inventoryId = new ObservableField<>();
    public ObservableField<String> zone = new ObservableField<>();

    private DialogAskInfoInventarioBinding mBindings;
    private Context mContext;

    //Pass here all external parameters
    public static DialogAskInfoInventarioView newInstance(RunnableArgss<Long, String> onConfirmed, Runnable onAbort) {
        return new DialogAskInfoInventarioView(onConfirmed, onAbort);
    }

    private DialogAskInfoInventarioView(RunnableArgss<Long, String> onConfirmed, Runnable onAbort) {
        super();

        this.onConfirmed = onConfirmed;
        this.onAbort = onAbort;
    }


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

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

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

        var cancelable = true;

        var alertDialog = new MaterialAlertDialogBuilder(this.mContext)
                .setView(mBindings.getRoot())
                .setCancelable(cancelable)
                .setPositiveButton(R.string.ok, (dialog, which) -> {
                    var zone = UtilityString.empty2null(this.zone.get());
                    if(zone != null) zone = StringUtils.capitalize(zone);


                    if (this.onConfirmed != null)
                        this.onConfirmed.run(this.inventoryId.get(), zone);
                })
                .setNegativeButton(R.string.abort, (dialog, which) -> {
                    if (this.onAbort != null) this.onAbort.run();
                })
                .create();

        alertDialog.setCanceledOnTouchOutside(cancelable);
        return alertDialog;
    }
}