package arez.dom;

import arez.annotations.Action;
import arez.annotations.ArezComponent;
import arez.annotations.Memoize;
import arez.annotations.Observable;
import arez.annotations.OnActivate;
import arez.annotations.OnDeactivate;
import elemental2.dom.DomGlobal;
import elemental2.dom.EventListener;
import java.util.Date;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * An observable model that declares state that tracks when the user is "online".
 * The online state is essentially a reflection of the browsers "navigator.onLine"
 * value. If an observer is observing the model, the model listens for changes from
 * the browser and updates the online state as appropriate. However if there is no
 * observer for the state, the model will not listen to to the browser events so as
 * not to have any significant performance impact.
 *
 * <h1>A very simple example</h1>
 * <pre>{@code
 * import com.google.gwt.core.client.EntryPoint;
 * import elemental2.dom.DomGlobal;
 * import arez.Arez;
 * import arez.networkstatus.NetworkStatus;
 *
 * public class NetworkStatusExample
 *   implements EntryPoint
 * {
 *   public void onModuleLoad()
 *   {
 *     final NetworkStatus networkStatus = NetworkStatus.create();
 *     Arez.context().observer( () -> {
 *       final String message = "Network Status: " + ( networkStatus.isOnLine() ? "Online" : "Offline" );
 *       DomGlobal.console.log( message );
 *       if ( !networkStatus.isOnLine() )
 *       {
 *         DomGlobal.console.log( "Offline since: " + networkStatus.getLastChangedAt() );
 *       }
 *     } );
 *   }
 * }
 * }</pre>
 */
@ArezComponent( nameIncludesId = false )
public abstract class NetworkStatus
{
  private final EventListener _listener;
  private boolean _rawOnLine;
  @Nullable
  private Date _lastChangedAt;

  /**
   * Create an instance of NetworkStatus.
   *
   * @return the NetworkStatus instance.
   */
  @Nonnull
  public static NetworkStatus create()
  {
    return new Arez_NetworkStatus();
  }

  NetworkStatus()
  {
    _listener = e -> updateOnlineStatus();
    _rawOnLine = DomGlobal.navigator.onLine;
    _lastChangedAt = new Date();
  }

  /**
   * Return true if the browser is online, false otherwise.
   *
   * @return true if the browser is online, false otherwise.
   */
  @Memoize
  public boolean isOnLine()
  {
    return isRawOnLine();
  }

  /**
   * Return the last time at which online status changed.
   * This will default to the time the component was created, otherwise
   * the time at which the online status was changed.
   *
   * @return the last time at which online status changed.
   */
  @Observable
  @Nullable
  public Date getLastChangedAt()
  {
    return _lastChangedAt;
  }

  @OnActivate
  final void onOnLineActivate()
  {
    DomGlobal.window.addEventListener( "online", _listener );
    DomGlobal.window.addEventListener( "offline", _listener );
  }

  @OnDeactivate
  final void onOnLineDeactivate()
  {
    DomGlobal.window.removeEventListener( "online", _listener );
    DomGlobal.window.removeEventListener( "offline", _listener );
  }

  @Observable
  boolean isRawOnLine()
  {
    return _rawOnLine;
  }

  void setRawOnLine( final boolean rawOnLine )
  {
    _rawOnLine = rawOnLine;
  }

  void setLastChangedAt( @Nullable final Date lastChangedAt )
  {
    _lastChangedAt = lastChangedAt;
  }

  @Action
  void updateOnlineStatus()
  {
    setRawOnLine( DomGlobal.navigator.onLine );
    setLastChangedAt( new Date() );
  }
}

@@ -12,16 +12,45 @@
 z.dom;%0A%0A
+import arez.ComputableValue;%0A
 import a
@@ -112,16 +112,93 @@
 ponent;%0A
+import arez.annotations.ComputableValueRef;%0Aimport arez.annotations.DepType;%0A
 import a
@@ -1850,38 +1850,8 @@
 er;%0A
-  private boolean _rawOnLine;%0A
   @N
@@ -2155,53 +2155,8 @@
 ();%0A
-    _rawOnLine = DomGlobal.navigator.onLine;%0A
     
@@ -2337,16 +2337,54 @@
 @Memoize
+( depType = DepType.AREZ_OR_EXTERNAL )
 %0A  publi
@@ -2419,29 +2419,42 @@
  return 
-isRawO
+DomGlobal.navigator.o
 nLine
-()
 ;%0A  %7D%0A%0A 
@@ -2803,24 +2803,111 @@
 gedAt;%0A  %7D%0A%0A
+  @ComputableValueRef%0A  abstract ComputableValue%3CBoolean%3E getOnLineComputableValue();%0A%0A
   @OnActivat
@@ -3268,162 +3268,8 @@
  %7D%0A%0A
-  @Observable%0A  boolean isRawOnLine()%0A  %7B%0A    return _rawOnLine;%0A  %7D%0A%0A  void setRawOnLine( final boolean rawOnLine )%0A  %7B%0A    _rawOnLine = rawOnLine;%0A  %7D%0A%0A
   vo
@@ -3421,49 +3421,57 @@
     
-s
+g
 et
-Raw
 OnLine
-( DomGlobal.navigator.onLine 
+ComputableValue().reportPossiblyChanged(
 );%0A 

Remove the need for internal observable now that ComputableValue can depend upon external dependencies