package cgeo.geocaching;

import cgeo.geocaching.StaticMapsActivity_.IntentBuilder_;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.utils.Log;

import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.Extra;
import com.googlecode.androidannotations.annotations.OptionsItem;
import com.googlecode.androidannotations.annotations.OptionsMenu;

import org.apache.commons.collections.CollectionUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;

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

@EActivity
@OptionsMenu(R.menu.static_maps_activity_options)
public class StaticMapsActivity extends AbstractActivity {

    private static final String EXTRAS_WAYPOINT = "waypoint";
    private static final String EXTRAS_DOWNLOAD = "download";
    private static final String EXTRAS_GEOCODE = "geocode";

    @Extra(EXTRAS_DOWNLOAD) boolean download = false;
    @Extra(EXTRAS_WAYPOINT) Integer waypoint_id = null;
    @Extra(EXTRAS_GEOCODE) String geocode = null;

    private final List<Bitmap> maps = new ArrayList<Bitmap>();
    private LayoutInflater inflater = null;
    private ProgressDialog waitDialog = null;
    private LinearLayout smapsView = null;
    private final Handler loadMapsHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (waitDialog != null) {
                waitDialog.dismiss();
            }
            try {
                if (CollectionUtils.isEmpty(maps)) {
                    if (download) {
                        final boolean succeeded = downloadStaticMaps();
                        if (succeeded) {
                            startActivity(StaticMapsActivity.this.getIntent());
                        } else {
                            showToast(res.getString(R.string.err_detail_google_maps_limit_reached));
                        }
                    } else {
                        showToast(res.getString(R.string.err_detail_not_load_map_static));
                    }
                    finish();
                } else {
                    showStaticMaps();
                }
            } catch (Exception e) {
                Log.e("StaticMapsActivity.loadMapsHandler", e);
            }
        }
    };

    /**
     * Shows the static maps.
     */
    private void showStaticMaps() {
        if (inflater == null) {
            inflater = getLayoutInflater();
        }

        if (smapsView == null) {
            smapsView = (LinearLayout) findViewById(R.id.maps_list);
        }
        smapsView.removeAllViews();

        for (final Bitmap image : maps) {
            if (image != null) {
                final ImageView map = (ImageView) inflater.inflate(R.layout.staticmaps_activity_item, null);
                map.setImageBitmap(image);
                smapsView.addView(map);
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.staticmaps_activity);

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

        waitDialog = ProgressDialog.show(this, null, res.getString(R.string.map_static_loading), true);
        waitDialog.setCancelable(true);

        (new LoadMapsThread()).start();
    }

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

    }

    private class LoadMapsThread extends Thread {

        @Override
        public void run() {
            try {
                // try downloading 2 times
                for (int trials = 0; trials < 2; trials++) {
                    for (int level = 1; level <= 5; level++) {
                        try {
                            if (waypoint_id != null) {
                                final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
                                final Bitmap image = StaticMapsProvider.getWaypointMap(geocode, cache.getWaypointById(waypoint_id), level);
                                if (image != null) {
                                    maps.add(image);
                                }
                            } else {
                                final Bitmap image = StaticMapsProvider.getCacheMap(geocode, level);
                                if (image != null) {
                                    maps.add(image);
                                }
                            }
                        } catch (Exception e) {
                            Log.e("StaticMapsActivity.LoadMapsThread.run", e);
                        }
                    }
                    if (!maps.isEmpty()) {
                        break;
                    }
                }

                loadMapsHandler.sendMessage(Message.obtain());
            } catch (Exception e) {
                Log.e("StaticMapsActivity.LoadMapsThread.run", e);
            }
        }
    }

    @OptionsItem(R.id.menu_refresh)
    void refreshMaps() {
        downloadStaticMaps();
        restartActivity();
    }

    private boolean downloadStaticMaps() {
        final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
        if (waypoint_id == null) {
            showToast(res.getString(R.string.info_storing_static_maps));
            StaticMapsProvider.storeCacheStaticMap(cache, true);
            return cache.hasStaticMap();
        }
        final Waypoint waypoint = cache.getWaypointById(waypoint_id);
        if (waypoint != null) {
            showToast(res.getString(R.string.info_storing_static_maps));
            // refresh always removes old waypoint files
            StaticMapsProvider.removeWpStaticMaps(waypoint, geocode);
            StaticMapsProvider.storeWaypointStaticMap(cache, waypoint, true);
            return StaticMapsProvider.hasStaticMapForWaypoint(geocode, waypoint);
        }
        showToast(res.getString(R.string.err_detail_not_load_map_static));
        return false;
    }

    public static void startActivity(final Context activity, final String geocode, final boolean download, final Waypoint waypoint) {
        IntentBuilder_ builder = StaticMapsActivity_.intent(activity).geocode(geocode).download(download);
        if (waypoint != null) {
            builder.waypoint_id(waypoint.getId());
        }
        builder.start();
    }
}
@@ -23,67 +23,8 @@
 g;%0A%0A
-import cgeo.geocaching.StaticMapsActivity_.IntentBuilder_;%0A
 impo
@@ -6478,16 +6478,36 @@
         
+StaticMapsActivity_.
 IntentBu

fix import and compilation issue