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

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 javax.inject.Inject;

import it.integry.barcode_base_android_library.model.BarcodeScanDTO;
import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeCallbackDTO;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.databinding.DialogAskLineaProdBinding;

public class DialogAskLineaProdView extends BaseDialogFragment implements DialogAskLineaProdViewModel.Listener {

    @Inject
    DialogAskLineaProdViewModel mViewModel;

    private final RunnableArgs<String> onComplete;
    private final Runnable onAbort;

    private DialogAskLineaProdBinding mBindings;
    private Context mContext;


    private int mBarcodeScannerIstanceID;

    //Pass here all external parameters
    public static DialogAskLineaProdView newInstance(RunnableArgs<String> onComplete, Runnable onAbort) {
        return new DialogAskLineaProdView(onComplete, onAbort);
    }

    private DialogAskLineaProdView(RunnableArgs<String> onComplete, Runnable onAbort) {
        super();
        this.onComplete = onComplete;
        this.onAbort = onAbort;
    }


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

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

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

        mViewModel.setListener(this);

        setCancelable(true);

        var alertDialog = new MaterialAlertDialogBuilder(this.mContext)
                .setView(mBindings.getRoot())
                .setCancelable(isCancelable())
                .setNegativeButton(R.string.abort, (dialogInterface, i) -> {
                    if(this.onAbort != null) this.onAbort.run();
                })
                .create();

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


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

        this.initBarcode();
    }

    private void initBarcode() {
        mBarcodeScannerIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessful(onScanSuccessful)
                .setOnScanFailed(this::onError));

        BarcodeManager.enable();
    }

    private final RunnableArgs<BarcodeScanDTO> onScanSuccessful = data -> {
        BarcodeManager.disable();
        this.mViewModel.processBarcodeDTO(data, BarcodeManager::enable);
    };

    @Override
    public void onProductionLineSelected(String codJfas) {
        this.onComplete.run(codJfas);

        requireActivity().runOnUiThread(this::dismiss);
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        BarcodeManager.removeCallback(mBarcodeScannerIstanceID);
        super.onDismiss(dialog);
    }
}