package it.integry.integrywmsnative.gest.rettifica_giacenze;

import android.content.Context;
import android.content.res.ColorStateList;
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 com.google.android.material.bottomsheet.BottomSheetBehavior;

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.FragmentMainRettificaGiacenzeBinding;
import it.integry.integrywmsnative.gest.rettifica_giacenze.core.RettificaGiacenzeHelper;
import it.integry.integrywmsnative.gest.rettifica_giacenze.viewmodel.RettificaGiacenzeViewModel;

public class RettificaGiacenzeFragment extends Fragment implements ITitledFragment {

    private RettificaGiacenzeHelper mHelper;
    private FragmentMainRettificaGiacenzeBinding mBinding = null;

    private RettificaGiacenzeViewModel mRettificaGiacenzeViewModel = null;

    private int barcodeScannerIstanceID = -1;

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


    public static RettificaGiacenzeFragment newInstance() {
        RettificaGiacenzeFragment fragment = new RettificaGiacenzeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_main_rettifica_giacenze, container, false);
        mRettificaGiacenzeViewModel = new RettificaGiacenzeViewModel();

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

        init();

        return mBinding.getRoot();
    }

    private void init() {
        mHelper = new RettificaGiacenzeHelper(getActivity());
        mRettificaGiacenzeViewModel.init(getActivity(), mBinding, mHelper);

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

        mHelper.loadListaFornitori(listaFornitori -> {
            mRettificaGiacenzeViewModel.setupSearchFornitori(listaFornitori);
        });


        mBinding.autoCompleteFornitori.setOnFocusChangeListener((v, hasFocus) -> {
            setUIToForn();
        });
        mBinding.inputCodArtDescrForn.setOnFocusChangeListener((v, hasFocus) -> {
            setUIToForn();
        });

        mBinding.inputCodArtDescrInt.setOnFocusChangeListener((v, hasFocus) -> {
            setUIToIntArt();
        });

        mRettificaGiacenzeViewModel.openLU();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        mRettificaGiacenzeViewModel.closeLU(false, false);
        BarcodeManager.removeCallback(barcodeScannerIstanceID);
        super.onDetach();
    }


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




    public void setUIToForn() {
        mBinding.rettificaGiacenzeFornCheckBox.setChecked(true);
        mBinding.rettificaGiacenzeFornLayout.setBackground(getActivity().getResources().getDrawable(R.drawable.circular_background_left));
        mBinding.rettificaGiacenzeFornLayout.setBackgroundTintList(ColorStateList.valueOf(getActivity().getResources().getColor(R.color.alpha_blue_500)));

        mBinding.rettificaGiacenzeArtIntCheckBox.setChecked(false);
        mBinding.rettificaGiacenzeArtIntLayout.setBackground(null);
        mBinding.rettificaGiacenzeArtIntLayout.setBackgroundTintList(null);


    }


    public void setUIToIntArt() {
        mBinding.rettificaGiacenzeArtIntCheckBox.setChecked(true);
        mBinding.rettificaGiacenzeArtIntLayout.setBackground(getActivity().getResources().getDrawable(R.drawable.circular_background_left));
        mBinding.rettificaGiacenzeArtIntLayout.setBackgroundTintList(ColorStateList.valueOf(getActivity().getResources().getColor(R.color.alpha_blue_500)));

        mBinding.rettificaGiacenzeFornCheckBox.setChecked(false);
        mBinding.rettificaGiacenzeFornLayout.setBackground(null);
        mBinding.rettificaGiacenzeFornLayout.setBackgroundTintList(null);


    }
}
