package it.integry.integrywmsnative.gest.picking_libero;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
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.utility.UtilityExceptions;
import it.integry.integrywmsnative.databinding.ActivityPickingLiberoBinding;
import it.integry.integrywmsnative.gest.picking_libero.core.PickingLiberoHelper;
import it.integry.integrywmsnative.gest.picking_libero.viewmodel.PickingLiberoViewModel;

import android.os.Bundle;

public class PickingLiberoActivity extends AppCompatActivity {

    private ActivityPickingLiberoBinding mBindings;

    private PickingLiberoHelper mHelper;
    private PickingLiberoViewModel mViewModel;

    private int barcodeScannerIstanceID = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBindings = DataBindingUtil.setContentView(this, R.layout.activity_picking_libero);

        mHelper = new PickingLiberoHelper(this);

        mViewModel = new PickingLiberoViewModel();
        mViewModel.init(this, mBindings, mHelper);

        mBindings.setViewmodel(mViewModel);

        setSupportActionBar(this.mBindings.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        barcodeScannerIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessfull(mViewModel.onScanSuccessfull)
                .setOnScanFailed(ex -> UtilityExceptions.defaultException(this, ex, false)));


        mBindings.waterfallToolbar.setNestedScrollView(mBindings.scrollView);
    }

    public void setTitle(String title) {
        mBindings.toolbarTitle.setText(title);
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @Override
    public void onBackPressed() {
        mViewModel.onBackPressed();

        BarcodeManager.removeCallback(barcodeScannerIstanceID);
        super.onBackPressed();
    }
}
