package com.mapswithme.maps;

import java.io.File;

import com.mapswithme.maps.R;
import com.mapswithme.maps.location.LocationService;
import com.nvidia.devtech.NvEventQueueActivity;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.util.Log;

public class MWMActivity extends NvEventQueueActivity implements LocationService.Listener
{
  VideoTimer m_timer;
  
  private static String TAG = "MWMActivity";
  private final static String PACKAGE_NAME = "com.mapswithme.maps";
  
  private int m_locationIconRes;
//  private boolean m_isLocationServicePaused = false;
  
  private LocationService m_locationService = null; 

  private String getAppBundlePath() throws NameNotFoundException
  {
    PackageManager packMgmr = getApplication().getPackageManager();
    ApplicationInfo appInfo = packMgmr.getApplicationInfo(PACKAGE_NAME, 0);
    return appInfo.sourceDir;
  }

  private String getDataStoragePath()
  {
    String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    return storagePath.concat(String.format("/%s/", PACKAGE_NAME));
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    
    final String storagePath = getDataStoragePath();
    // create folder if it doesn't exist
    File f = new File(storagePath);
    f.mkdirs();

    try
    {
      nativeInit(getAppBundlePath(), storagePath);
    }
    catch (NameNotFoundException e)
    {
      e.printStackTrace();
    }

    m_timer = new VideoTimer();
    m_locationIconRes = R.drawable.ic_menu_location;
    m_locationService = new LocationService(this);
//    m_isLocationServicePaused = false;
  }
  
  public void onLocationStatusChanged(int newStatus)
  {
    switch (newStatus)
    {
    case LocationService.FIRST_EVENT:
      m_locationIconRes = R.drawable.ic_menu_location_found;
      break;
    case LocationService.STARTED:
      m_locationIconRes = R.drawable.ic_menu_location_search;
      break;
    default:
      m_locationIconRes = R.drawable.ic_menu_location;
    }
    nativeLocationStatusChanged(newStatus);
  }
  
  @Override
  public void onLocationUpdated(long time, double lat, double lon, float accuracy)
  {
    nativeLocationUpdated(time, lat, lon, accuracy);
  }

  @Override
  public void onCompassUpdated(long time, double magneticNorth, double trueNorth, float accuracy)
  {
    nativeCompassUpdated(time, magneticNorth, trueNorth, accuracy);
  }

  @Override
  protected void onPause()
  {
    // @TODO
//    if (m_locationService.isActive())
//    {
//      m_locationService.enterBackground();
//      m_isLocationServicePaused = true;
//    }

    super.onPause();
  }

  @Override
  protected void onResume()
  {
    // @TODO
//    if (m_isLocationServicePaused)
//    {
//      m_locationService.enterForeground();
//      m_isLocationServicePaused = false;
//    }
    super.onResume();    
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    // temprorarily disable downloader in the menu
    //menu.removeItem(R.id.download_maps);
    return true;
  }
  
  @Override 
  public boolean onPrepareOptionsMenu(Menu menu)
  {
    menu.findItem(R.id.my_position).setIcon(m_locationIconRes);
    return super.onPrepareOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
    case R.id.my_position:
      if (m_locationService.isSubscribed(this))
        m_locationService.stopUpdate(this);
      else
        m_locationService.startUpdate(this);
      return true;

    case R.id.download_maps:
      Intent intent = new Intent(this, DownloadUI.class);
      startActivity(intent);
      return true;

    default:
      return super.onOptionsItemSelected(item);
    }
  }

  static
  {
    System.loadLibrary("mapswithme");
  }
  
  private native void nativeInit(String apkPath, String storagePath);
  private native void nativeLocationStatusChanged(int newStatus);
  private native void nativeLocationUpdated(long time, double lat, double lon, float accuracy);
  private native void nativeCompassUpdated(long time, double magneticNorth, double trueNorth, float accuracy);
}

@@ -800,63 +800,8 @@
 es;%0A
-//  private boolean m_isLocationServicePaused = false;%0A
   %0A 
@@ -1849,49 +1849,8 @@
 s);%0A
-//    m_isLocationServicePaused = false;%0A
   %7D%0A
@@ -2679,384 +2679,156 @@
     
-// @TODO%0A//    if (m_locationService.isActive())%0A//    %7B%0A//      m_locationService.enterBackground();%0A//      m_isLocationServicePaused = true;%0A//    %7D%0A%0A    super.onPause();%0A  %7D%0A%0A  @Override%0A  protected void onResume()%0A  %7B%0A    // @TODO%0A//    if (m_isLocationServicePaused)%0A//    %7B%0A//      m_locationService.enterForeground();%0A//      m_isLocationServicePaused = false;%0A//    %7D
+m_locationService.stopUpdate(this);%0A%0A    super.onPause();%0A  %7D%0A%0A  @Override%0A  protected void onResume()%0A  %7B%0A    m_locationService.startUpdate(this);%0A
 %0A   

Stop location updates when app is in the background