package it.integry.integrywmsnative.gest.pv_ordini_acquisto.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;

import androidx.databinding.DataBindingUtil;

import it.integry.barcode_base_android_library.model.BarcodeScanDTO;
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.data_store.db.entity.Griglia;
import it.integry.integrywmsnative.core.data_store.db.repository.GrigliaRepository;
import it.integry.integrywmsnative.core.expansion.BaseDialog;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityProgress;
import it.integry.integrywmsnative.databinding.DialogScanCodiceGrigliaBinding;
import it.integry.integrywmsnative.gest.pv_ordini_acquisto.helper.OrdineAcquistoPvHelper;

public class DialogScanLisA {

    private Activity mContext;

    private BaseDialog mDialog;

    private DialogScanCodiceGrigliaBinding mBinding;

    private RunnableArgs<Griglia> mOnDialogDismiss;

    private OrdineAcquistoPvHelper mHelper;

    private GrigliaRepository mGrigliaRepository;
    private Griglia mGriglia;

    private DialogScanLisA(Activity context, GrigliaRepository grigliaRepository, OrdineAcquistoPvHelper helper, RunnableArgs<Griglia> onDialogDismiss) {
        mOnDialogDismiss = null;
        mContext = context;
        mGrigliaRepository = grigliaRepository;
        mHelper = helper;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mGriglia = null;
        mBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_scan_codice_griglia, null, false);

        mDialog = new BaseDialog(context);
        mDialog.setContentView(mBinding.getRoot());
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mOnDialogDismiss = onDialogDismiss;
        initBarcode();
    }

    public static Dialog make(Activity context, GrigliaRepository grigliaRepository, OrdineAcquistoPvHelper helper, RunnableArgs<Griglia> onDialogDismiss) {
        return new DialogScanLisA(context, grigliaRepository, helper, onDialogDismiss).mDialog;
    }


    private void initBarcode() {

        int barcodeIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessfull(onScanSuccessfull)
                .setOnScanFailed(ex -> UtilityExceptions.defaultException(mContext, ex, false)));

        mDialog.setBarcodeListener(true);
        mDialog.setOnDismissListener(dialog -> {
            BarcodeManager.removeCallback(barcodeIstanceID);
            BarcodeManager.enable();
            mOnDialogDismiss.run(mGriglia);
        });
    }


    private final RunnableArgs<BarcodeScanDTO> onScanSuccessfull = data -> {
        BarcodeManager.disable();

        final Dialog progressDialog = UtilityProgress.createDefaultProgressDialog(mContext);
        Context context = mContext;
        RunnableArgs<Exception> mOnError = exception -> {
            mContext.runOnUiThread(() -> {
                progressDialog.dismiss();
                UtilityExceptions.defaultException(context, exception);
                BarcodeManager.enable();
            });
        };

        RunnableArgs<Griglia> onGrigliaAvailable = griglia -> {
            mContext.runOnUiThread(() -> {
                progressDialog.dismiss();
                mGriglia = griglia;
                mDialog.dismiss();
            });
        };


        mGrigliaRepository.findByCodAlis(data.getStringValue(), griglia -> {
            if (griglia != null) {
                griglia.getGriglia().setCountArticoli(griglia.getCountArticoli());
                mHelper.updateGriglia(griglia.getGriglia(), onGrigliaAvailable, mOnError);
            } else {
                mHelper.downloadAndSaveLisA(data.getStringValue(), onGrigliaAvailable, mOnError);
            }
        }, mOnError);


    };

}
