package it.integry.integrywmsnative.core.di;

import androidx.databinding.BaseObservable;
import androidx.databinding.Observable;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;

public class BindableInteger  extends BaseObservable {
    Integer mValue;

    public BindableInteger() {}

    public BindableInteger(int startValue) {
        this.mValue = startValue;
    }

    public Integer get() {
        return get(true);
    }

    public Integer get(boolean defaultIfNull) {

        if(mValue == null){
            if(defaultIfNull) return 0;
            else return null;
        } else return mValue;
    }

    public void set(Integer value) {
        if (!Objects.equals(this.mValue, value)) {
            this.mValue = value;
            notifyChange();
        }
    }

    public boolean isEmpty() {
        return mValue == null;
    }

    public static void registerListener(BindableInteger bindableInteger, RunnableArgs<Integer> onValueChanged) {
        bindableInteger.addOnPropertyChangedCallback(new OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                if(onValueChanged != null) onValueChanged.run(bindableInteger.get());
            }
        });
    }

    public static void resetListeners(BindableInteger bindableInteger){
        Integer oldValue = bindableInteger.get();
        bindableInteger = new BindableInteger();
        bindableInteger.set(oldValue);
    }

}
