(function (context, $) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.CheckoutOrderScreen = function (app) {

    var EVENTS = context.JK.EVENTS;
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var jamTrackUtils = context.JK.JamTrackUtils;
    var checkoutUtils = context.JK.CheckoutUtilsInstance;

    var $screen = null;
    var $navigation = null;
    var $templateOrderContent = null;
    var $orderPanel = null;
    var $orderContent = null;
    var userDetail = null;
    var step = null;
    var $backBtn = null;
    var $orderPrompt = null;
    var $emptyCartPrompt = null;
    var $noAccountInfoPrompt = null;


    function beforeShow() {

    }


    function afterShow(data) {
      beforeShowOrder();
    }


    function beforeHide() {

    }

    function beforeShowOrder() {
      $orderPrompt.addClass('hidden')
      $emptyCartPrompt.addClass('hidden')
      $noAccountInfoPrompt.addClass('hidden')
      $orderPanel.removeClass("hidden")
      $screen.find(".place-order").addClass('disabled').off('click', placeOrder)
      $("#order_error").text('').addClass("hidden")
      step = 3;
      renderNavigation();
      populateOrderPage();
    }


    function populateOrderPage() {
      clearOrderPage();

      rest.getShoppingCarts()
        .done(function(carts) {
          rest.getBillingInfo()
            .done(function(billingInfo) {
              renderOrderPage(carts, billingInfo)
            })
            .fail(function(jqXHR) {
              if(jqXHR.status == 404) {
                // no account for this user
                $noAccountInfoPrompt.removeClass('hidden')
                app.notify({   title: "No account information",
                    text: "Please restart the checkout process." },
                  null,
                  true);
              }
            })
        })
        .fail(app.ajaxError);
    }


    function renderOrderPage(carts, recurlyAccountInfo) {
      logger.debug("rendering order page")
      var data = {}

      var sub_total = 0.0
      var taxes = 0.0
      $.each(carts, function(index, cart) {
        sub_total += parseFloat(cart.product_info.real_price)
      });
      if(carts.length == 0) {
        data.grand_total = '-.--'
        data.sub_total = '-.--'
        data.taxes = '-.--'
        data.shipping_handling = '-.--'
      }
      else {
        data.grand_total = 'Calculating...'
        data.sub_total = '$' + sub_total.toFixed(2)
        data.taxes = 'Calculating...'
        data.shipping_handling = '$0.00'
      }

      data.carts = carts
      data.billing_info = recurlyAccountInfo.billing_info
      data.shipping_info = recurlyAccountInfo.address

      data.shipping_as_billing = true; //jamTrackUtils.compareAddress(data.billing_info, data.shipping_info);

      var orderContentHtml = $(
        context._.template(
          $templateOrderContent.html(),
          data,
          {variable: 'data'}
        )
      )

      $orderContent.append(orderContentHtml)
      $orderPanel.find(".change-payment-info").on('click', moveToPaymentInfo)

      var $placeOrder = $screen.find(".place-order")
      if(carts.length == 0) {
        $orderPrompt.addClass('hidden')
        $emptyCartPrompt.removeClass('hidden')
        $noAccountInfoPrompt.addClass('hidden')
        $placeOrder.addClass('disabled').unbind('click').on('click', false)
      }
      else {
        logger.debug("cart has " + carts.length + " items in it")
        $orderPrompt.removeClass('hidden')
        $emptyCartPrompt.addClass('hidden')
        $noAccountInfoPrompt.addClass('hidden')
        if(gon.global.purchases_enabled || context.JK.currentUserAdmin) {
          $placeOrder.removeClass('disabled').unbind('click').on('click', placeOrder)
        }
        else {
          $placeOrder.addClass('disabled').unbind('click').on('click', false)
        }
        estimateTaxes(carts, recurlyAccountInfo.billing_info);
      }
    }

    function displayTax(totalTax, totalPrice) {

      $screen.find('.order-right-page .order-items-value.taxes').text('$' + totalTax.toFixed(2))
      $screen.find('.order-right-page .order-items-value.grand-total').text('$' + totalPrice.toFixed(2))
    }

    function estimateTaxes(carts, billing_info) {
      var planPricing = {}

      var priceElement = $screen.find('.order-right-page .plan.jamtrack')

      if(priceElement.length == 0) {
        logger.error("unable to find price element for jamtrack");
        app.notify({title: "Error Encountered", text: "Unable to find plan info for jam track"})
        return false;
      }
      logger.debug("creating recurly pricing element for plan: " + gon.recurly_tax_estimate_jam_track_plan)

      var effectiveQuantity = 0

      context._.each(carts, function(cart) {
        effectiveQuantity += cart.product_info.quantity - cart.product_info.marked_for_redeem
      })

      if (gon.global.estimate_taxes) {
        var state = billing_info.state;
        var country = billing_info.country;
        if (state) {
          state = state.toLowerCase();
        }
        if (country) {
          country = country.toLowerCase();
        }
        var taxRate = 0;
        if (state && country && (state == 'tx' || state == 'texas') && country == 'us') {
          taxRate = 0.0825;
        }

        var estimatedTax = 0;
        var estimatedTotal = 0;

        context._.each(carts, function(cart) {
          var cart_quantity = cart.product_info.quantity - cart.product_info.marked_for_redeem
          estimatedTax += cart.product_info.price * cart_quantity * taxRate;
          estimatedTotal += cart.product_info.price * cart_quantity;
        })

        displayTax(Math.round(estimatedTax*100)/100, Math.round((estimatedTotal + estimatedTax)*100)/100)
      }
      else {
        checkoutUtils.configureRecurly()
        var pricing = context.recurly.Pricing();
        pricing.plan_code = gon.recurly_tax_estimate_jam_track_plan;
        pricing.resolved = false;
        pricing.effective_quantity = 1

        // this is called when the plan is resolved against Recurly. It will have tax info, which is the only way we can get it.
        pricing.on('change', function(price) {
          logger.debug("pricing", pricing)
          displayTax(effectiveQuantity, Number(pricing.price.now.tax), Number(pricing.price.now.total));

        })

        pricing.attach(priceElement.eq(0))
      }
    }

    function moveToPaymentInfo() {
      context.location = '/client#/checkoutPayment';
      return false;
    }

    function placeOrder(e) {
      e.preventDefault();
      $screen.find(".place-order").off('click').addClass('disabled')
      $("#order_error").text('').addClass("hidden")
      rest.placeOrder()
        .done(moveToThanks)
        .fail(orderErrorHandling);
    }


    function orderErrorHandling(xhr, ajaxOptions, thrownError) {
      if (xhr && xhr.responseJSON) {
        var message = "Error submitting payment: "
        $.each(xhr.responseJSON.errors, function (key, error) {
          message += key + ": " + error
        })
        $("#order_error").text(message).removeClass("hidden")
      }
      else {
        $("#order_error").text(xhr.responseText).removeClass("hidden")
      }
      $screen.find(".place-order").on('click', placeOrder).removeClass('disabled')
    }

    function moveToThanks(purchaseResponse) {
      checkoutUtils.setLastPurchase(purchaseResponse)
      checkoutUtils.deletePreserveBillingInfo()
      $("#order_error").addClass("hidden")
      $orderPanel.addClass("hidden")
      checkoutUtils.deletePreserveBillingInfo()
      //$thanksPanel.removeClass("hidden")
      jamTrackUtils.checkShoppingCart()
      app.refreshUser()

      window.location = '/client#/checkoutComplete'
    }


    function clearOrderPage() {
      $orderContent.empty();
    }

    function renderNavigation() {
      $navigation.html("");
      var navigationHtml = $(
        context._.template(
          $('#template-checkout-navigation').html(),
          {current: step, purchases_disable_class: gon.global.purchases_enabled ? 'hidden' : ''},
          {variable: 'data'}
        )
      );

      $navigation.append(navigationHtml);
    }

    function events() {
      $backBtn.on('click', function(e) {
        e.preventDefault();

        context.location = '/client#/checkoutPayment'
      })
    }

    function initialize() {
      var screenBindings = {
        'beforeShow': beforeShow,
        'afterShow': afterShow,
        'beforeHide': beforeHide
      };
      app.bindScreen('checkoutOrder', screenBindings);

      $screen                     = $("#checkoutOrderScreen");
      $navigation                 = $screen.find(".checkout-navigation-bar");
      $templateOrderContent       = $("#template-order-content");
      $orderPanel                 = $screen.find(".order-panel");
      $backBtn                    = $screen.find('.back');
      $orderPrompt                = $screen.find('.order-prompt');
      $emptyCartPrompt            = $screen.find('.empty-cart-prompt');
      $noAccountInfoPrompt        = $screen.find('.no-account-info-prompt');
      $orderContent = $orderPanel.find(".order-content");

      if ($screen.length == 0) throw "$screen must be specified";
      if ($navigation.length == 0) throw "$navigation must be specified";

      events();
    }

    this.initialize = initialize;

    return this;
  }
})
(window, jQuery);