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

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.DialogBindProductBarcodeWithPackageBinding;

public class DialogBindProductBarcodeWithPackageView extends BaseDialogFragment {


    @Inject
    DialogBindProductBarcodeWithPackageViewModel mViewModel;

    private DialogBindProductBarcodeWithPackageBinding mBindings;
    private Context mContext;

    private final String unknownBarcode;
    private final Runnable onAbort;


    private int mBarcodeScannerIstanceID;

    //Pass here all external parameters
    public static DialogBindProductBarcodeWithPackageView newInstance(String unknownBarcode,Runnable onAbort) {
        return new DialogBindProductBarcodeWithPackageView(unknownBarcode, onAbort);
    }

    private DialogBindProductBarcodeWithPackageView(String unknownBarcode, Runnable onAbort) {
        super();

        this.unknownBarcode = unknownBarcode;
        this.onAbort = onAbort;
    }


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

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

        setCancelable(false);

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

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

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

        initBarcode();
        mViewModel.init(unknownBarcode);

        return alertDialog;
    }


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




    private void initBarcode() {
        mBarcodeScannerIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessful(onScanSuccessfull)
                .setOnScanFailed(this::onError));
        BarcodeManager.enable();
        setBarcodeListener(true);
    }


    private final RunnableArgs<BarcodeScanDTO> onScanSuccessfull = data -> {
        this.onLoadingStarted();

        this.mViewModel.processBarcodeDTO(data, () -> {
            this.onLoadingEnded();

            dismiss();
        }, this::onError);
    };


    @Override
    public void onError(Exception ex) {
        super.onError(ex);
        BarcodeManager.enable();
    }
}