package com.rehivetech.beeeon.gui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.rehivetech.beeeon.R;
import com.rehivetech.beeeon.controller.Controller;
import com.rehivetech.beeeon.gcm.INotificationReceiver;
import com.rehivetech.beeeon.gcm.notification.IGcmNotification;
import com.rehivetech.beeeon.threading.CallbackTaskManager;
import com.rehivetech.beeeon.util.Log;

/**
 * Abstract parent for application activities that requires logged in user and better using of tasks.
 * <p/>
 * When user is not logged in, it will switch to LoginActivity automatically.
 * Provides useful methods for using CallbackTasks.
 */
public abstract class BaseApplicationActivity extends BaseActivity implements INotificationReceiver {

	private static String TAG = BaseApplicationActivity.class.getSimpleName();
	private boolean triedLoginAlready = false;

	public CallbackTaskManager callbackTaskManager;

	protected boolean isPaused = false;

	private View mProgressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		callbackTaskManager = new CallbackTaskManager(this);
	}

	@Override
	public void onResume() {
		super.onResume();

		Controller controller = Controller.getInstance(this);

		if (!controller.isLoggedIn()) {
			if (!triedLoginAlready) {
				triedLoginAlready = true;
				redirectToLogin(this);
			} else {
				finish();
			}
			return;
		} else {
			triedLoginAlready = false;
		}

		controller.getGcmModel().registerNotificationReceiver(this);

		isPaused = false;
		onAppResume();

		mProgressBar = findViewById(R.id.toolbar_progress);
	}

	@Override
	public void onPause() {
		super.onPause();

		Controller controller = Controller.getInstance(this);
		controller.getGcmModel().unregisterNotificationReceiver(this);

		isPaused = true;
		onAppPause();
	}

	@Override
	public void onStop() {
		super.onStop();

		// Cancel and remove all remembered tasks
		callbackTaskManager.cancelAndRemoveAll();
	}

	public static void redirectToLogin(Context context) {
		Log.d(TAG, "Redirecting to login");
		Intent intent = new Intent(context, LoginActivity.class);
		intent.putExtra(LoginActivity.BUNDLE_REDIRECT, true);
		intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK);

		context.startActivity(intent);
	}

	/**
	 * This is called after onResume(), but only when user is correctly logged in
	 */
	protected void onAppResume() {
		// Empty default method
	}

	/**
	 * This is called after onPause()
	 */
	protected void onAppPause() {
		// Empty default method
	}

	public void setBeeeOnProgressBarVisibility(boolean visible) {
		if (mProgressBar == null) {
			// This activity probably doesn't have progressbar in layout
			return;
		}

		mProgressBar.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
	}

	/**
	 * Method that receives Notifications.
	 */
	public boolean receiveNotification(final IGcmNotification notification) {
		// FIXME: Leo (or someone else?) should implement correct handling of notifications (showing somewhere in activity or something like that?)

		return false;
	}
}

@@ -1637,63 +1637,8 @@
 e();
-%0A%0A%09%09mProgressBar = findViewById(R.id.toolbar_progress);
 %0A%09%7D%0A
@@ -2733,69 +2733,291 @@
 %0A%09%09%09
-// This activity probably doesn't have progressbar in layout%0A
+mProgressBar = findViewById(R.id.toolbar_progress);%0A%0A%09%09%09if (mProgressBar == null) %7B%0A%09%09%09%09// This activity probably doesn't have progressbar in layout%0A%09%09%09%09Log.w(TAG, String.format(%22Can't set visibility of progressbar in '%25s, it wasn't found in layout.%22, getClass().getSimpleName()));%0A%09
 %09%09%09r
@@ -3023,16 +3023,21 @@
 return;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09m

Fix showing progress bar on some special circumstances