package com.cube.storm.content.lib.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import com.cube.storm.content.model.UpdateContentProgress;
import io.reactivex.Observable;

/**
 * {@link UpdateManager} which ensures updates only occur on a wi-fi connection, otherwise delegating to another
 * UpdateManager.
 */
public class WifiOnlyUpdateManager implements UpdateManager
{
	private ConnectivityManager connectivityManager;
	private SharedPreferences preferences;
	private UpdateManager delegate;

	public WifiOnlyUpdateManager(Context context, UpdateManager delegate)
	{
		connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		preferences = PreferenceManager.getDefaultSharedPreferences(context);
		this.delegate = delegate;
	}

	@Override
	public void cancelPendingRequests()
	{
		delegate.cancelPendingRequests();
	}

	@Override
	public Observable<UpdateContentProgress> checkForBundle()
	{
		if (!canDownload())
		{
			return Observable.error(new IllegalStateException("Not connected to wifi"));
		}
		return delegate.checkForBundle();
	}

	@Override
	public Observable<UpdateContentProgress> checkForUpdates(long lastUpdate)
	{
		if (!canDownload())
		{
			return Observable.error(new IllegalStateException("Not connected to wifi"));
		}
		return delegate.checkForUpdates(lastUpdate);
	}

	private boolean canDownload()
	{
		return !preferences.getBoolean("wifi_only", false) || isConnectedToWifi();
	}

	/**
	 * Checks if the user is connected to wifi or not. Ensure you provide the {@link android.Manifest.permission#ACCESS_WIFI_STATE} permission in
	 * your manifest.
	 *
	 * @return True if connected, or the check failed. False if not connected. May return true even if the user has no
	 * active internet connection with their selected wifi service
	 */
	private boolean isConnectedToWifi()
	{
		try
		{
			NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			return networkInfo.isConnected();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		// Assume they are.
		return true;
	}

	@Override
	public void scheduleBackgroundUpdates()
	{
		delegate.scheduleBackgroundUpdates();
	}

	@Override
	public Observable<Observable<UpdateContentProgress>> updates()
	{
		return delegate.updates();
	}
}

@@ -225,24 +225,122 @@
 nceManager;%0A
+import androidx.annotation.NonNull;%0Aimport com.cube.storm.content.lib.worker.ContentUpdateWorker;%0A
 import com.c
@@ -376,24 +376,23 @@
 eContent
-Progress
+Request
 ;%0Aimport
@@ -1129,35 +1129,24 @@
 ide%0A%09public 
-Observable%3C
 UpdateConten
@@ -1142,33 +1142,31 @@
 pdateContent
-Progress%3E
+Request
  checkForBun
@@ -1202,32 +1202,167 @@
 )%0A%09%09%7B%0A%09%09%09return 
+new UpdateContentRequest(%0A%09%09%09%09Long.toString(System.currentTimeMillis()),%0A%09%09%09%09ContentUpdateWorker.UpdateType.FULL_BUNDLE,%0A%09%09%09%09null,%0A%09%09%09%09
 Observable.error
@@ -1405,32 +1405,37 @@
 ected to wifi%22))
+%0A%09%09%09)
 ;%0A%09%09%7D%0A%09%09return d
@@ -1483,27 +1483,16 @@
 %09public 
-Observable%3C
 UpdateCo
@@ -1496,25 +1496,23 @@
 eContent
-Progress%3E
+Request
  checkFo
@@ -1576,16 +1576,151 @@
 %09return 
+new UpdateContentRequest(%0A%09%09%09%09Long.toString(System.currentTimeMillis()),%0A%09%09%09%09ContentUpdateWorker.UpdateType.DELTA,%0A%09%09%09%09lastUpdate,%0A%09%09%09%09
 Observab
@@ -1779,16 +1779,21 @@
  wifi%22))
+%0A%09%09%09)
 ;%0A%09%09%7D%0A%09%09
@@ -1956,16 +1956,404 @@
 ();%0A%09%7D%0A%0A
+%09@Override%0A%09public UpdateContentRequest downloadUpdates(@NonNull String endpoint)%0A%09%7B%0A%09%09if (!canDownload())%0A%09%09%7B%0A%09%09%09return new UpdateContentRequest(%0A%09%09%09%09Long.toString(System.currentTimeMillis()),%0A%09%09%09%09ContentUpdateWorker.UpdateType.DIRECT_DOWNLOAD,%0A%09%09%09%09null,%0A%09%09%09%09Observable.error(new IllegalStateException(%22Not connected to wifi%22))%0A%09%09%09);%0A%09%09%7D%0A%09%09return delegate.downloadUpdates(endpoint);%0A%09%7D%0A%0A
 %09/**%0A%09 *
@@ -3101,27 +3101,16 @@
 %09public 
-Observable%3C
 Observab
@@ -3129,17 +3129,15 @@
 tent
-Progress%3E
+Request
 %3E up

Update WifiUpdater to match new interface