package be.ugent.zeus.hydra;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import be.ugent.zeus.hydra.data.services.HTTPIntentService;
import be.ugent.zeus.hydra.data.services.UpdaterService;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.zubhium.ZubhiumSDK;
import java.util.Locale;

/**
 *
 * @author Thomas Meire
 */
public class Hydra extends AbstractSherlockActivity {

    ZubhiumSDK sdk;
    public static final Locale LOCALE = new Locale("nl", "BE");
    private static final boolean DEBUG = true;
    private static final boolean BETA = true;

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

        // Center the image using a custom layout
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.abs_main);

        // Zubhium
        if (!DEBUG) {
            Log.d("Zubhium:", "Enable bugtracking");
            sdk = ZubhiumSDK.getZubhiumSDKInstance(getApplicationContext(), "4837990a007ee67c597d1059742293");
            if (sdk != null) {
                // We are registering update receiver
                sdk.registerUpdateReceiver(Hydra.this);
            }
        }
        
        // Google Analytics
//         if (BETA || DEBUG) {
//            Log.d("GAnalytics:", "Tracking disabled");
//            GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(getApplicationContext());
//            googleAnalytics.setAppOptOut(true);
//        }

        // Home screen: disable the button
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(false);

        setContentView(R.layout.hydra);
        setTitle("");

        link(R.id.home_btn_news, News.class);
        link(R.id.home_btn_calendar, Calendar.class);
        link(R.id.home_btn_info, Info.class);
        link(R.id.home_btn_menu, RestoMenu.class);
        link(R.id.home_btn_urgent, Urgent.class);
        link(R.id.home_btn_schamper, SchamperDaily.class);

        Intent intent = new Intent(this, UpdaterService.class);
        intent.putExtra(HTTPIntentService.FORCE_UPDATE, true);
        startService(intent);
    }

    private void link(int id, final Class activity) {
        findViewById(id).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(Hydra.this, activity);
                intent.putExtra("class", Hydra.class.getCanonicalName());
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.hydra, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.feedbackButton:
                setupFeedback();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onDestroy() {
        if (sdk != null) {
            sdk.unRegisterUpdateReceiver();     // Don't forget to unregister receiver
        }
        super.onDestroy();
    }

    protected void setupFeedback() {
        /**
         * Now lets listen to users, by enabling in app help desk. *
         */
        if (sdk != null) {
            sdk.openFeedbackDialog(Hydra.this);
        }
    }
}

@@ -80,16 +80,61 @@
 Bundle;%0A
+import android.preference.PreferenceManager;%0A
 import a
@@ -176,16 +176,45 @@
 w.View;%0A
+import android.widget.Toast;%0A
 import b
@@ -679,19 +679,16 @@
 e;%0A%0A/**%0A
- *%0A
  * @auth
@@ -694,20 +694,16 @@
 hor 
-Thomas Meire
+Zeus WPI
 %0A */
@@ -1021,24 +1021,603 @@
 e(icicle);%0A%0A
+        // First run? If so: set the default preferences%0A        boolean firstrun = getSharedPreferences(%22PREFERENCE%22, MODE_PRIVATE).getBoolean(%22firstrun%22, true);%0A        if (firstrun) %7B%0A            Toast.makeText(this, %22First run%22, Toast.LENGTH_LONG).show();%0A            // Save the state%0A            getSharedPreferences(%22PREFERENCE%22, MODE_PRIVATE)%0A                .edit()%0A                .putBoolean(%22firstrun%22, false)%0A                .commit();%0A            // Set the default preference%0A            PreferenceManager.setDefaultValues(this, R.xml.settings, false);%0A        %7D%0A%0A
         // C
@@ -2169,24 +2169,16 @@
       %7D%0A
-        
 %0A       

Put the default preferences on first run