package cgeo.geocaching;

import cgeo.geocaching.activity.AbstractActionBarActivity;
import cgeo.geocaching.models.Image;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.storage.DataStore;
import cgeo.geocaching.ui.ImagesList;
import cgeo.geocaching.ui.ImagesList.ImageType;

import org.apache.commons.collections4.CollectionUtils;

import rx.Subscription;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class ImagesActivity extends AbstractActionBarActivity {

    private boolean offline;
    private List<Image> imageNames;
    private ImageType imgType = ImageType.SpoilerImages;
    private ImagesList imagesList;
    private Subscription subscription;

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

        // get parameters
        final Bundle extras = getIntent().getExtras();

        String geocode = null;
        if (extras != null) {
            geocode = extras.getString(Intents.EXTRA_GEOCODE);
            imgType = (ImageType) extras.getSerializable(Intents.EXTRA_TYPE);
        }

        if (extras == null || geocode == null) {
            showToast("Sorry, c:geo forgot for what cache you want to load spoiler images.");
            finish();
            return;
        }

        // init
        setTheme();
        setContentView(R.layout.images_activity);
        setTitle(res.getString(imgType.getTitle()));

        imagesList = new ImagesList(this, geocode);

        imageNames = extras.getParcelableArrayList(Intents.EXTRA_IMAGES);
        if (CollectionUtils.isEmpty(imageNames)) {
            showToast(res.getString(R.string.warn_load_images));
            finish();
            return;
        }

        offline = DataStore.isOffline(geocode, null) && (imgType == ImageType.SpoilerImages
                || Settings.isStoreLogImages());

    }

    @Override
    public void onStart() {
        super.onStart();
        subscription = imagesList.loadImages(findViewById(R.id.spoiler_list), imageNames, offline);
    }

    @Override
    public void onStop() {
        // Reclaim native memory faster than the finalizers would
        subscription.unsubscribe();
        super.onStop();
    }

    public static void startActivityLogImages(final Context fromActivity, final String geocode, final List<Image> logImages) {
        startActivity(fromActivity, geocode, logImages, ImageType.LogImages);
    }

    @SuppressWarnings("deprecation")
    private static void startActivity(final Context fromActivity, final String geocode, final List<Image> logImages, final ImageType imageType) {
        final Intent logImgIntent = new Intent(fromActivity, ImagesActivity.class);
        // if resuming our app within this activity, finish it and return to the cache activity
        logImgIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
                .putExtra(Intents.EXTRA_GEOCODE, geocode)
                .putExtra(Intents.EXTRA_TYPE, imageType);

        // avoid forcing the array list as parameter type
        final ArrayList<Image> arrayList = new ArrayList<>(logImages);
        logImgIntent.putParcelableArrayListExtra(Intents.EXTRA_IMAGES, arrayList);
        fromActivity.startActivity(logImgIntent);
    }

    public static void startActivitySpoilerImages(final Context fromActivity, final String geocode, final List<Image> spoilers) {
        startActivity(fromActivity, geocode, spoilers, ImageType.SpoilerImages);
    }

    @Override
    public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        imagesList.onCreateContextMenu(menu, v);
    }

    @Override
    public boolean onContextItemSelected(final MenuItem item) {
        if (imagesList.onContextItemSelected(item)) {
            return true;
        }
        return super.onContextItemSelected(item);
    }
}

@@ -2934,179 +2934,8 @@
 ass)
-;%0A        // if resuming our app within this activity, finish it and return to the cache activity%0A        logImgIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
 %0A   

fix #5452: confused activity stack when looking at images