package it.integry.integrywmsnative.core.di;

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

import org.parceler.Parcel;

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

@Parcel
public class BindableString extends BaseObservable {
    String value;

    public String get() {
        return value != null ? value : "";
    }

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

    public boolean isEmpty() {
        return value == null || value.isEmpty();
    }

    public void refresh() {
        notifyChange();
    }




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

    public static void resetListeners(BindableString bindableString){
        String oldValue = bindableString.get();
        bindableString = new BindableString();
        bindableString.set(oldValue);
    }
}
