$ = jQuery
context = window
logger = context.JK.logger

@SubscriptionStore = Reflux.createStore(
  {
    listenables: @SubscriptionActions

    subscription: null


    init: ->
      this.listenTo(context.AppStore, this.onAppInit)

    onAppInit: (@app) ->

    onUpdateSubscription: (subscription) ->

      console.log("refreshing subscription info")
      rest.getSubscription().done (subscription) =>
        @subscription = subscription
        console.log("subscription store update", subscription)
        @trigger(@subscription)
      .fail(() =>
        @app.layout.notify({
          title: "Unable to fetch subscription status",
          text: "Please contact support@jamkazam.com"
        })
      )

    onForceUpdate: (subscription) ->
      @subscription = subscription
      @trigger(@subscription)

    onCancelSubscription: () ->
      rest.cancelSubscription().done((result) =>
        @subscription = {}
        console.log("cancelled successfully")
        @trigger(@subscription)
      )
      .fail((jqXHR) =>
        @app.layout.notify({
          title: "Subscription Cancelled",
          text: "Thanks for being a supporter!"
        })
      )
  }
)