package amai.org.conventions;

import android.app.Activity;
import android.app.ActivityOptions;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.facebook.FacebookRequestError;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import amai.org.conventions.events.activities.EventActivity;
import amai.org.conventions.events.activities.ProgrammeActivity;
import amai.org.conventions.model.Convention;
import amai.org.conventions.model.Update;
import amai.org.conventions.navigation.NavigationPages;
import amai.org.conventions.networking.ModelRefresher;
import amai.org.conventions.networking.UpdatesRefresher;
import amai.org.conventions.updates.UpdatesActivity;
import amai.org.conventions.utils.CollectionUtils;

public class HomeActivity extends AppCompatActivity {

	private static final int NEW_UPDATES_NOTIFICATION_ID = 75457;
	private NavigationPages navigationPages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_home);

	    navigationPages = new NavigationPages(this);

	    // Initiate async downloading of the updated convention info in the background
	    new AsyncTask<Void, Void, Void>() {
		    @Override
		    protected Void doInBackground(Void... params) {
			    ModelRefresher modelRefresher = new ModelRefresher();
			    modelRefresher.refreshFromServer();

			    return null;
		    }
	    }.execute();



	    // Requests to Facebook must be initialized from the UI thread
	    new Handler().post(new Runnable() {
		   @Override
		   public void run() {
			   final int numberOfUpdatesBeforeRefresh = Convention.getInstance().getUpdates().size();

			   // Refresh and ignore all errors
			   UpdatesRefresher.getInstance(HomeActivity.this).refreshFromServer(null, true, new UpdatesRefresher.OnUpdateFinishedListener() {
				   @Override
				   public void onSuccess() {

					   List<Update> newUpdates = CollectionUtils.filter(Convention.getInstance().getUpdates(), new CollectionUtils.Predicate<Update>() {
						   @Override
						   public boolean where(Update item) {
							   return item.isNew();
						   }
					   });

					   Update latestUpdate = Collections.max(newUpdates, new Comparator<Update>() {
						   @Override
						   public int compare(Update lhs, Update rhs) {
							   return lhs.getDate().compareTo(rhs.getDate());
						   }
					   });

					   // We don't want to raise the notification if there are no new updates, or if this is the first time updates are downloaded to cache.
					   if (newUpdates.size() > 0 && numberOfUpdatesBeforeRefresh > 0
							   && UpdatesRefresher.getInstance(HomeActivity.this).shouldEnableNotificationAfterUpdate()) {
						   String notificationTitle = newUpdates.size() == 1
								   ? getString(R.string.new_update)
								   : getString(R.string.new_updates, newUpdates.size());

						   String notificationMessage = latestUpdate.getText().substring(0, Math.min(200,latestUpdate.getText().length())) + "...";

						   NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
						   Intent intent = new Intent(HomeActivity.this, UpdatesActivity.class);
						   Notification.Builder notificationBuilder = new Notification.Builder(HomeActivity.this)
								   .setSmallIcon(R.drawable.cami_logo_small_white)
								   .setLargeIcon(ImageHandler.getNotificationLargeIcon(HomeActivity.this))
								   .setContentTitle(notificationTitle)
								   .setContentText(notificationMessage)
								   .setAutoCancel(true)
								   .setContentIntent(PendingIntent.getActivity(HomeActivity.this, 0, intent, 0))
								   .setDefaults(Notification.DEFAULT_VIBRATE);


						   Notification notification = new Notification.BigTextStyle(notificationBuilder)
								   .bigText(notificationMessage)
								   .build();

						   notificationManager.notify(NEW_UPDATES_NOTIFICATION_ID, notification);
					   }
				   }

				   @Override
				   public void onError(FacebookRequestError error) {
				   }

				   @Override
				   public void onInvalidTokenError() {
				   }
			   });
		   }
	   });
    }

    public void onNavigationButtonClicked(View view) {
        int position = Integer.parseInt(view.getTag().toString());
	    Class<? extends Activity> activityType = navigationPages.getActivityType(position);
	    Intent intent = new Intent(this, activityType);

	    if (activityType == ProgrammeActivity.class) {
		    Bundle extras = new Bundle();
		    extras.putInt(ProgrammeActivity.EXTRA_DELAY_SCROLLING, 500);
		    intent.putExtras(extras);
	    }

	    ActivityOptions options = ActivityOptions.makeCustomAnimation(this, 0, R.anim.shrink_to_top_right);
	    startActivity(intent, options.toBundle());
    }
}

@@ -387,37 +387,8 @@
 iew;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -515,69 +515,8 @@
 t;%0A%0A
-import amai.org.conventions.events.activities.EventActivity;%0A
 impo
@@ -2384,247 +2384,8 @@
 );%0A%0A
-%09%09%09%09%09   Update latestUpdate = Collections.max(newUpdates, new Comparator%3CUpdate%3E() %7B%0A%09%09%09%09%09%09   @Override%0A%09%09%09%09%09%09   public int compare(Update lhs, Update rhs) %7B%0A%09%09%09%09%09%09%09   return lhs.getDate().compareTo(rhs.getDate());%0A%09%09%09%09%09%09   %7D%0A%09%09%09%09%09   %7D);%0A%0A
 %09%09%09%09
@@ -2690,24 +2690,270 @@
 Update()) %7B%0A
+%0A%09%09%09%09%09%09   Update latestUpdate = Collections.max(newUpdates, new Comparator%3CUpdate%3E() %7B%0A%09%09%09%09%09%09%09   @Override%0A%09%09%09%09%09%09%09   public int compare(Update lhs, Update rhs) %7B%0A%09%09%09%09%09%09%09%09   return lhs.getDate().compareTo(rhs.getDate());%0A%09%09%09%09%09%09%09   %7D%0A%09%09%09%09%09%09   %7D);%0A%0A
 %09%09%09%09%09%09   Str

Fix crash on app start when there are no updates