// TODO: put in extra safeguard to not display this to people who have purchased, e.g. delete on confirmation
// TODO: also set a cookie to never retarget them with same thing
var DCP3240eStoreFn = {
  aid: 'DCP-3240eStore',
  init: function() {

    // If on cart, check for items to potentially  save to cookie
    if (window.location.href.indexOf('/cart') != -1) {
      DCP3240eStoreFn.checkCart();

    // If on confirmation, check for cookie to delete if present
  } else if (window.location.href.indexOf('confirmation') != -1) {
      DCP3240eStoreFn.deleteCookie();

    // If on entry page (setup via target), and cookie is present, as well as last cohort action is cart abandon, and is on VCA, display popup
  } else if (DCP3240eStoreFn.getCookie('dcp__3240_cart_abandon_cohort_items') && (window.location.href.indexOf('www.vodafone.com.au') != -1)  && (DCP3240eStoreFn.getCookie('dcp_cohort') == 'cartAbandon')) {
      DCP3240eStoreFn.displayPreviousItem();
    }
  },
  checkCart: function() {
    var DCP3240eStore_cartJSONDataMax = 300;
    var DCP3240eStore_cartJSONDataHolder = setInterval(function() {
      if (cartJSONData) {
        clearInterval(DCP3240eStore_cartJSONDataHolder);
        DCP3240eStore_cartJSONDataHolder = null;
        DCP3240eStoreFn.checkLastAddedItem();
      }
      if (DCP3240eStore_cartJSONDataMax === 0) {
        DCP3240eStore_cartJSONDataHolder
        clearInterval(DCP3240eStore_cartJSONDataHolder);
        DCP3240eStore_cartJSONDataHolder = null;
      }
      DCP3240eStore_cartJSONDataMax--;
    }, 500);
  },
  checkLastAddedItem: function() {
    var lastIndex = cartJSONData.bundledItems.length - 1;
    if (cartJSONData.bundledItems[lastIndex].cartLabel == 'Phone and plan' || cartJSONData.bundledItems[lastIndex].cartLabel == 'Mobile broadband service' || cartJSONData.bundledItems[lastIndex].cartLabel == 'Tablet and plan') {
			var lastItem = cartJSONData.bundledItems[lastIndex];

			var displayName = lastItem.device.displayName;
			var color = lastItem.device.selectedSku.color;
			var capacity = lastItem.device.selectedSku.capacity;

			var smallImageUrl = lastItem.device.selectedSku.smallImageUrl;
			var friendlyUrlId = lastItem.device.selectedSku.friendlyUrlId;
      switch(cartJSONData.bundledItems[lastIndex].cartLabel) {
        case 'Phone and plan':
          friendlyUrlId = 'mobile-details/'+lastItem.device.selectedSku.friendlyUrlId;
          break;
        case 'Mobile broadband service':
          friendlyUrlId = 'broadband-details/'+lastItem.device.selectedSku.friendlyUrlId;
          break;
        case 'Tablet and plan':
          friendlyUrlId = 'tablet-details/'+lastItem.device.selectedSku.friendlyUrlId;
          break;
      }

      var bundleSkuID = lastItem.bundleSkuID;
      var mppTerm = lastItem.bundleSkuID;


			var cookie = displayName + '|' + color + '|' + capacity + '|' + smallImageUrl + '|' + friendlyUrlId + '|' + bundleSkuID + '|' + mppTerm;
      var now = new Date();
      var days = 7;
      now.setTime(now.getTime() + (days * 24 * 60 * 60 * 1000));
			document.cookie = 'dcp__3240_cart_abandon_cohort_items='+cookie+'; Domain=.vodafone.com.au;Path=/; expires=' + now.toUTCString() + ';';
      document.cookie = 'dcp_cohort=cartAbandon;Domain=.vodafone.com.au;Path=/; expires=' + now.toUTCString() + ';';

		}
  },
  getCookie: function(a) {
    var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
    return b ? b.pop() : '';
  },
  deleteCookie: function() {
    document.cookie = 'dcp__3240_cart_abandon_cohort_items=;Domain=.vodafone.com.au;Path=/;';
    document.cookie = 'dcp_cohort=;Domain=.vodafone.com.au;Path=/;';


  },
  displayPreviousItem: function() {
		if (DCP3240eStoreFn.getCookie('dcp__3240_cart_abandon_cohort_items')) {
      document.cookie = 'dcp_cohort=;Domain=.vodafone.com.au;Path=/;';

			var cookie = DCP3240eStoreFn.getCookie('dcp__3240_cart_abandon_cohort_items');
			var split = cookie.split('|');
      document.cookie = 'dcp__3240_cart_abandon_cohort_items=;Domain=.vodafone.com.au;Path=/;';
      console.log('cookie deleted');
      croWD.croWDUtils.sendTracking(
          "cohort-cartRetarget",
          "cohort-cartRetarget-popup-show",
          ""
      );
			var displayName = split[0];
			var color = split[1];
			var capacity = split[2];
			var smallImageUrl = split[3];
			var friendlyUrlId = split[4];
      var bundleSkuID = split[5];
      var mppTerm = split[6];

			$('#DCP3240eStoreHolder .lightbox .deviceImage').attr('src','https://shop.vodafone.com.au'+smallImageUrl);
			$('#DCP3240eStoreHolder .lightbox .deviceName').text(displayName);
			$('#DCP3240eStoreHolder .lightbox .deviceColour').text(color);
			$('#DCP3240eStoreHolder .lightbox .deviceCapacity').text(capacity);
			$('#DCP3240eStoreHolder .lightbox .deviceLink').attr('href','https://shop.vodafone.com.au/'+friendlyUrlId);

      var SERVpopupFnOptions = {
        popupid: 'cohort-cartRetarget',
        template: 'bannerFloat',
        dismiss: ['timeout', 10],
        css: {
          size: ['410px', '360px'],
          background: '#ffffff'
        },
        html: $('#DCP3240eStoreHolder')[0].innerHTML
      };
      SERVpopupFn.options = SERVpopupFnOptions;
      SERVpopupFn.init();



		}
	}
}
DCP3240eStoreFn.init();