

$ = jQuery
context = window
context.JK ||= {};

class Tracking
  constructor: () ->
    @logger = context.JK.logger
    @rest = new context.JK.Rest();

  adTrack: (app) =>
    if !context.jamClient.IsNativeClient()
      if context.JK.currentUserId?
        app.user().done( (user) =>
            #  relative to 1 day ago (24 * 60 * 60 * 1000)
            if new Date(user.created_at).getTime() < new Date().getTime() - 86400000
              @logger.debug("existing user recorded")
              context.JK.GA.virtualPageView('/landing/jamtracks/existing-user/');
            else
              @logger.debug("new user recorded")
              context.JK.GA.virtualPageView('/landing/jamtracks/new-user/')
          )
      else if $.cookie('jamkazam_user')?
        @logger.debug("existing/logged out user")
        context.JK.GA.virtualPageView('/landing/jamtracks/existing-user/')
      else
        @logger.debug("new user recorded")
        context.JK.GA.virtualPageView('/landing/jamtracks/new-user/')

  jamtrackBrowseTrack: (app) =>
    if context.JK.currentUserId?
      app.user().done( (user) =>
        if context.jamClient.IsNativeClient()
          @logger.debug("client user recorded")
          context.JK.GA.virtualPageView('/client#/jamtrackBrowse/user-in-app')
        else
          if new Date(user.created_at).getTime() < new Date().getTime() - 86400000
            @logger.debug("existing user recorded")
            context.JK.GA.virtualPageView('/client#/jamtrackBrowse/existing-user')
          else
            @logger.debug("existing new recorded")
            context.JK.GA.virtualPageView('/client#/jamtrackBrowse/new-user')
      )
    else
      if context.jamClient.IsNativeClient()
        @logger.debug("client user recorded")
        context.JK.GA.virtualPageView('/client#/jamtrackBrowse/user-in-app')
      else
        @logger.debug("existing new recorded")
        context.JK.GA.virtualPageView('/client#/jamtrackBrowse/new-user')



  redeemSignupTrack: (app) =>
    if context.JK.currentUserId?
      app.user().done( (user) =>
        if context.jamClient.IsNativeClient()
          @logger.debug("client user recorded")
          context.JK.GA.virtualPageView('/client#/redeemSignup/user-in-app')
        else
          if new Date(user.created_at).getTime() < new Date().getTime() - 86400000
            @logger.debug("existing existing recorded")
            context.JK.GA.virtualPageView('/client#/redeemSignup/existing-user')
          else
            @logger.debug("existing new recorded")
            context.JK.GA.virtualPageView('/client#/redeemSignup/new-user')
      )
    else
      if context.jamClient.IsNativeClient()
        @logger.debug("client user recorded")
        context.JK.GA.virtualPageView('/client#/redeemSignup/user-in-app')
      else
        @logger.debug("existing new recorded")
        context.JK.GA.virtualPageView('/client#/redeemSignup/new-user')

  redeemCompleteTrack: () ->
    context.JK.GA.virtualPageView('/client#/redeemed-successful')


context.JK.Tracking = new Tracking()

