package bartil.string;

import io.baratine.core.Modify;
import io.baratine.core.OnLoad;
import io.baratine.core.OnSave;
import io.baratine.core.Result;
import io.baratine.pubsub.PubSubService;
import io.baratine.store.Store;

public class StringServiceImpl implements StringService
{
  private String _url;
  private Store<String> _store;
  private PubSubService<String> _pubsub;

  private String _value;

  public StringServiceImpl(String url,
                           Store<String> store,
                           PubSubService<String> pubsub)
  {
    _url = url;

    _store = store;

    _pubsub = pubsub;
  }

  @Override
  public void get(Result<String> result)
  {
    result.complete(_value);
  }

  @Modify
  @Override
  public void set(String value, Result<Boolean> result)
  {
    _value = value;

    notifyWatchers();

    result.complete(true);
  }

  @Modify
  @Override
  public void delete(Result<Boolean> result)
  {
    _value = null;

    notifyWatchers();

    result.complete(true);
  }

  @Override
  public void exists(Result<Boolean> result)
  {
    result.complete(_value != null);
  }

  private void notifyWatchers()
  {
    if (_pubsub != null) {
      _pubsub.publish(_value);
    }
  }

  @OnLoad
  public void onLoad(Result<Boolean> result)
  {
    System.out.println(getClass().getSimpleName() + ".onLoad0");

    _store.get(_url, value -> {
      System.out.println(getClass().getSimpleName() + ".onLoad1: " + _url + " . " + value);

      _value = value;

      result.complete(true);
    });
  }

  @OnSave
  public void onSave(Result<Boolean> result)
  {
    System.out.println(getClass().getSimpleName() + ".onSave0: " + _url + " . " + _value);

    if (_value != null) {
      _store.put(_url, _value);
    }
    else {
      _store.remove(_url);
    }

    result.complete(true);
  }
}

@@ -1366,21 +1366,110 @@
 rl, 
-value -%3E %7B
+result.from(value -%3E onLoadComplete(value)));%0A  %7D%0A%0A  private boolean onLoadComplete(String value)
 %0A  
+%7B%0A
     
@@ -1559,18 +1559,16 @@
 );%0A%0A    
-  
 _value =
@@ -1584,39 +1584,19 @@
     
-  result.complete(true);%0A    %7D)
+return true
 ;%0A  

use Result.from() for @OnLoad