package it.integry.integrywmsnative.gest.versamento_merce;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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.interfaces.ITitledFragment;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.databinding.FragmentMainVersamentoMerceBinding;
import it.integry.integrywmsnative.gest.versamento_merce.core.VersamentoMerceHelper;
import it.integry.integrywmsnative.gest.versamento_merce.viewmodel.VersamentoMerceViewModel;

public class VersamentoMerceFragment extends Fragment implements ITitledFragment {

    private FragmentMainVersamentoMerceBinding mBinding;
    private VersamentoMerceViewModel mVersamentoMerceViewModel;
    private VersamentoMerceHelper mHelper;


    private int barcodeScannerIstanceID = -1;

    public VersamentoMerceFragment() {
        // Required empty public constructor
    }


    public static VersamentoMerceFragment newInstance() {
        VersamentoMerceFragment fragment = new VersamentoMerceFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            //mParam1 = getArguments().getString(ARG_PARAM1);
            //mParam2 = getArguments().getString(ARG_PARAM2);
        }

        mVersamentoMerceViewModel = new VersamentoMerceViewModel();
        barcodeScannerIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessfull(mVersamentoMerceViewModel.onScanSuccessfull)
                .setOnScanFailed(ex -> UtilityExceptions.defaultException(getActivity(), ex, false)));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_main_versamento_merce, container, false);

        mBinding.setView(this);
        mBinding.setViewmodel(mVersamentoMerceViewModel);

        init();
        return mBinding.getRoot();
    }

    private void init() {
        mHelper = new VersamentoMerceHelper(getActivity());
        mVersamentoMerceViewModel.init(getActivity(), mBinding, mHelper);




        mVersamentoMerceViewModel.openLU();

    }

    @Override
    public void onDestroy() {
        BarcodeManager.removeCallback(barcodeScannerIstanceID);
        super.onDestroy();
    }

    @Override
    public String getTitle(Context context) {
        return context.getResources().getString(R.string.versamento_merce_fragment_title);
    }
}
