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

import android.content.Context;
import android.text.InputType;
import android.view.LayoutInflater;

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

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

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogInputGeneralContentBinding;

/**
 * Created by GiuseppeS on 22/03/2018.
 */

public class DialogSimpleInputHelper extends MaterialAlertDialogBuilder {


    public final ObservableField<String> inputValue = new ObservableField<>();

    public DialogSimpleInputHelper(@NonNull Context context, String titleText, String defaultTextValue, String hintText, RunnableArgs<String> onPositiveClick, Runnable onNegativeClick, Integer inputType) {
        super(context);

        var dialogBindings = DialogInputGeneralContentBinding.inflate(LayoutInflater.from(context));
        dialogBindings.setView(this);

        dialogBindings.executePendingBindings();

        this.setTitle(titleText);
//        this.setMessage("Inserisci la nuova quantità residua dell'ordine");
        this.setView(dialogBindings.getRoot());
        this.setCancelable(true);

        if (!UtilityString.isNullOrEmpty(hintText)) {
            dialogBindings.generalInputText.setHint(hintText);
        }

        if (!UtilityString.isNullOrEmpty(defaultTextValue)) {
            dialogBindings.generalInputText.setText(defaultTextValue);
        }
        if (inputType != null) {
            dialogBindings.generalInputText.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        this.setPositiveButton("Ok", (dialog, which) -> {
            if(onPositiveClick != null) onPositiveClick.run(this.inputValue.get());
        });

        this.setNegativeButton("Annulla", (dialog, which) -> {
            if(onNegativeClick != null) onNegativeClick.run();
        });


    }


    private static DialogSimpleInputHelper makeBase(Context mContext, String titleText, String defaultTextValue, String hintText, RunnableArgs<String> onPositiveClick, Runnable onNegativeClick, Integer inputType) {
        DialogSimpleInputHelper dialogSimpleInputHelper = new DialogSimpleInputHelper(
                mContext,
                titleText,
                defaultTextValue,
                hintText,
                onPositiveClick,
                onNegativeClick,
                inputType
        );

        return dialogSimpleInputHelper;

        //        Dialog dialog = new Dialog(mContext);
//        DialogSimpleInputViewModel viewModel = new DialogSimpleInputViewModel(dialog);
//
//
//        DialogInputGeneralBinding mBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.dialog_input_general, null, false);
//        viewModel
//                .setOnPositive(() -> {
//
//                    onPositiveClick.run(mBinding.generalInput.getText().toString());
//                })
//                .setOnNegative(onNegativeClick);
//
//        mBinding.setViewmodel(viewModel);
//
//        //Title VIEW
//        mBinding.titleText.setText(titleText);
//
//        if (!UtilityString.isNullOrEmpty(hintText)) {
//            mBinding.generalInput.setHint(hintText);
//        }
//
//        if (!UtilityString.isNullOrEmpty(defaultTextValue)) {
//            mBinding.generalInput.setText(defaultTextValue);
//        }
//        if (inputType != null) {
//            mBinding.generalInput.setInputType(InputType.TYPE_CLASS_NUMBER);
//        }
//        //Content View
//        dialog.setCancelable(true);
//        dialog.setCanceledOnTouchOutside(false);
//        dialog.setContentView(mBinding.getRoot());
//        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        UtilityDialog.setTo90PercentWidth(mContext, dialog);
//
//        return dialog;
    }


    public static DialogSimpleInputHelper makeInputDialog(Context mContext, String title, RunnableArgs<String> onPositiveClick, Runnable onNegativeClick) {

        return makeBase(mContext,
                title,
                null,
                null,
                onPositiveClick,
                onNegativeClick,
                null);

    }


    public static DialogSimpleInputHelper makeInputDialog(Context mContext, String title, String defaultTextValue, String hintText, RunnableArgs<String> onPositiveClick, Runnable onNegativeClick) {

        return makeBase(mContext,
                title,
                defaultTextValue,
                hintText,
                onPositiveClick,
                onNegativeClick,
                null);

    }

    public static DialogSimpleInputHelper makeInputDialog(Context mContext, String title, RunnableArgs<String> onPositiveClick, Runnable onNegativeClick, Integer inputType) {

        return makeBase(mContext,
                title,
                null,
                null,
                onPositiveClick,
                onNegativeClick,
                inputType);

    }


    public static DialogSimpleInputHelper makeInputDialog(Context mContext, String title, String defaultTextValue, String hintText, RunnableArgs<String> onPositiveClick, Runnable onNegativeClick, Integer inputType) {

        return makeBase(mContext,
                title,
                defaultTextValue,
                hintText,
                onPositiveClick,
                onNegativeClick,
                inputType);

    }


}
