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

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import it.integry.barcode_base_android_library.model.BarcodeScanDTO;
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.core.utility.UtilityBarcode;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogStartProductionBinding;

public class DialogStartProduction extends BaseDialogFragment {
    private int barcodeScannerIstanceID = -1;
    private RunnableArgs<BarcodeScanDTO> onScanSuccessful;
    private DialogStartProductionBinding mBinding;
    private String title;
    private String label;

    public static DialogStartProduction newInstance(RunnableArgs<BarcodeScanDTO> onScanSuccessful) {

        Bundle args = new Bundle();

        DialogStartProduction fragment = new DialogStartProduction();
        fragment.setArguments(args);
        fragment.setOnScanSuccessful(onScanSuccessful);
        return fragment;
    }

    public String getTitle() {
        return title;
    }

    public DialogStartProduction setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getLabel() {
        return label;
    }

    public DialogStartProduction setLabel(String label) {
        this.label = label;
        return this;
    }

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

    public DialogStartProduction setOnScanSuccessful(RunnableArgs<BarcodeScanDTO> onScanSuccessful) {
        this.onScanSuccessful = onScanSuccessful;
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DialogStartProductionBinding.inflate(inflater, container, false);
        mBinding.setLifecycleOwner(this);
        this.initBarcodeReader();
        setCancelable(true);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        if (!UtilityString.isNullOrEmpty(title)) {
            mBinding.titleText.setText(title);
        }

        if (!UtilityString.isNullOrEmpty(label)) {
            mBinding.scanLabel.setText(label);
        }
        return mBinding.getRoot();
    }


    private void initBarcodeReader() {
        barcodeScannerIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessful(this::processBarcode)
                .setOnScanFailed(this::onError));
        BarcodeManager.enable();
    }

    private void processBarcode(BarcodeScanDTO barcodeScanDTO) {
        if (UtilityBarcode.isEan128(barcodeScanDTO)) {
            this.dismiss();
            this.onScanSuccessful.run(barcodeScanDTO);
        } else {
            this.onError(new Exception("Il barcode scansionato non è valido!"));
        }
    }
}
