package com.malmstein.yahnac.analytics;

import android.content.Context;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.malmstein.yahnac.BuildConfig;
import com.malmstein.yahnac.R;
import com.malmstein.yahnac.model.Story;

public class UsageAnalytics {

    private Tracker analyticsTracker;

    private boolean isActive() {
        return BuildConfig.ENABLE_USAGE_ANALYTICS;
    }

    public void initTracker(Context context) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(context);
        analyticsTracker = analytics.newTracker(R.xml.global_tracker);
    }

    public void trackPage(String page) {
        if (isActive()) {
            analyticsTracker.setScreenName(page);
            analyticsTracker.send(new HitBuilders.ScreenViewBuilder().build());
        }
    }

    public void trackStory(String page, Story story) {
        if (isActive()) {
            analyticsTracker.setScreenName(page + ": " + story.getId());
            analyticsTracker.send(new HitBuilders.ScreenViewBuilder().build());
        }
    }

    public void trackEvent(String action) {
        if (isActive()) {
            analyticsTracker.send(new HitBuilders.EventBuilder()
                    .setCategory("Action")
                    .setAction(action)
                    .build());
        }
    }

    public void trackNavigateEvent(String action, Story story) {
        if (isActive()) {
            analyticsTracker.send(new HitBuilders.EventBuilder()
                    .setCategory("Navigate")
                    .setAction(action)
                    .setValue(story.getId())
                    .build());
        }
    }

    public void trackShareEvent(String action, Story story) {
        if (isActive()) {
            analyticsTracker.send(new HitBuilders.EventBuilder()
                    .setCategory("Share")
                    .setAction(action)
                    .setValue(story.getId())
                    .build());
        }
    }

    public void trackVoteEvent(String action, Story story) {
        if (isActive()) {
            analyticsTracker.send(new HitBuilders.EventBuilder()
                    .setCategory("Vote")
                    .setAction(action)
                    .setValue(story.getId())
                    .build());
        }
    }

    public void trackBookmarkEvent(String action, Story story) {
        if (isActive()) {
            analyticsTracker.send(new HitBuilders.EventBuilder()
                    .setCategory("Bookmark")
                    .setAction(action)
                    .setValue(story.getId())
                    .build());
        }
    }

}

@@ -755,36 +755,64 @@
   if (isActive()
+ && analyticsTracker != null
 ) %7B%0A
-
             anal
@@ -1011,32 +1011,60 @@
   if (isActive()
+ && analyticsTracker != null
 ) %7B%0A            
@@ -1279,32 +1279,60 @@
   if (isActive()
+ && analyticsTracker != null
 ) %7B%0A            
@@ -1593,32 +1593,60 @@
   if (isActive()
+ && analyticsTracker != null
 ) %7B%0A            
@@ -1951,32 +1951,60 @@
   if (isActive()
+ && analyticsTracker != null
 ) %7B%0A            
@@ -2305,32 +2305,60 @@
   if (isActive()
+ && analyticsTracker != null
 ) %7B%0A            
@@ -2640,32 +2640,32 @@
  Story story) %7B%0A
-
         if (isAc
@@ -2670,16 +2670,44 @@
 Active()
+ && analyticsTracker != null
 ) %7B%0A    

usage analytics can be null