var SERVbridgeFn = {
    aid: 'SERV-bridge',
    cartJSONData: '',
    init: function () {
        // Upgrades
        if (window.location.href.indexOf('https://shop.vodafone.com.au/upgrades/new_upgrade_cart') != -1 || window.location.href.indexOf('https://shop.vodafone.com.au/upgrades/checkout') != -1) {
            $.getJSON('https://shop.vodafone.com.au/upgrades/new_upgrade_cart/web/ajax/order_data_ajax_call.jsp', function (data) {
                SERVbridgeFn.cartJSONData = data;
            });
            // New AQ
        } else if (window.location.href.indexOf('https://shop.vodafone.com.au/cart') != -1 || window.location.href.indexOf('https://shop.vodafone.com.au/checkout') != -1) {
            $.getJSON('https://shop.vodafone.com.au/cart/web/modules/cartasjson.jsp', function (data) {
                SERVbridgeFn.cartJSONData = data;
            });
        }
        SERVbridgeFn.cartInfoHandler();
    },
    cartInfoHandler: function () {
        if (window.location.href.indexOf('https://shop.vodafone.com.au/cart') > -1 && window.location.search.indexOf('cartSavedReenter') <= -1) {
            SERVbridgeFn.setCookie('dcp_cohort', 'cartAbandon', 'bimonth');
            // TODO: read from API or from DOM??? Discount Presentation MFA..
            var cartInfoFinder = setInterval(function () {
                if (SERVbridgeFn.cartJSONData != '') {
                    clearInterval(cartInfoFinder);
                    cartInfoFinder = null;
                    // set cart info as cookie
                    var bundledItems = [];
                    SERVbridgeFn.cartJSONData.bundledItems.each(function (item, index) {
                        bundledItems.push({
                            'bundleSkuID': item.bundleSkuID,
                            'cartLabel': item.cartLabel,
                            'device': {
                                'displayName': item.device.selectedSku.displayName,
                                'color': item.device.selectedSku.color,
                                'skuId': item.device.selectedSku.skuId,
                                'capacity': item.device.selectedSku.capacity,
                                'deviceTerm': item.device.deviceTerm,
                                'monthlyAmountInclGst': item.device.price.formattedMonthlyAmountInclGst,
                                'smallColorPreviewUrl': item.device.selectedSku.smallColorPreviewUrl,
                                'smallImageUrl': item.device.selectedSku.smallImageUrl,
                                'friendlyUrlId': item.device.selectedSku.friendlyUrlId
                            },
                            'plan': {
                                'displayName': item.plan.displayName,
                                'skuId': item.plan.id,
                                'bundleSaveEligible': item.plan.bundleSaveEligible,
                                'smallImageUrl': item.plan.smallImageUrl,
                                'planMonthlyPrice': item.plan.planMonthlyPrice
                            }
                        })
                    })
                    var today = new Date().toDateString();
                    var cartInfo = {
                        creationDate: today,
                        sessionId: utag ? utag.data.tealium_session_id : null,
                        bundleSavePlanCount: SERVbridgeFn.cartJSONData.bundleSavePlanCount ? SERVbridgeFn.cartJSONData.bundleSavePlanCount : 0,
                        bundleSavePercentage: SERVbridgeFn.cartJSONData.bundleSavePercentage ? SERVbridgeFn.cartJSONData.bundleSavePercentage : 0,
                        bundledItems: bundledItems,
                        totalMonthlyPrice: SERVbridgeFn.cartJSONData.totalMonthlyPrice
                    }
                    SERVbridgeFn.setCookie('__cartSessionInfo', JSON.stringify(cartInfo), '15mins');
                    SERVbridgeFn.setCookie('__cartSavedInfo', JSON.stringify(cartInfo), 'week');
                } else {
                    SERVbridgeFn.deleteCookie('__cartSessionInfo');
                    SERVbridgeFn.deleteCookie('__cartSavedInfo');
                }
            }, 1000);
        } else if (window.location.href.indexOf('/upgrades') > -1) {
            SERVbridgeFn.deleteCookie('__cartSessionInfo');
        } else if (window.location.href.indexOf('confirmation') > -1) {
            SERVbridgeFn.deleteCookie('__cartSessionInfo');
            SERVbridgeFn.deleteCookie('__cartSavedInfo');
            if (SERVbridgeFn.getCookie('dcp_cohort') == 'cartAbandon') {
                SERVbridgeFn.deleteCookie('dcp_cohort');
            }
        }
    },
    getCookie: function (name) {
        var value = '; ' + document.cookie;
        var parts = value.split('; ' + name + '=');
        if (parts.length == 2) return parts.pop().split(';').shift();
    },
    deleteCookie: function (name) {
        document.cookie = name + '=;Domain=.vodafone.com.au;Path=/;';
    },
    setCookie: function (name, val, type) {
        var cookieName = name;;
        var cookieValue = val;
        var myDate = new Date();
        switch (type) {
            case 'session':
                myDate = 0;
                break;
            case '15mins':
                myDate.setTime(myDate.getTime() + 15 * 60 * 1000); //set expire 15 mins later
                break;
            case '30mins':
                myDate.setTime(myDate.getTime() + 30 * 60 * 1000); //set expire 30 mins later
                break;
            case 'week':
                myDate.setDate(myDate.getDate() + 7); // set expire 1 week later
                break;
            case 'biweek':
                myDate.setDate(myDate.getDate() + 14); // set expire 2 weeks later
                break;
            case 'month':
                myDate.setMonth(myDate.getMonth() + 1); // set expire 1 mth later
                break;
            case 'bimonth':
                myDate.setMonth(myDate.getMonth() + 2); // set expire 2 mths later
                break;
            case 'year':
                myDate.setMonth(myDate.getMonth() + 12); // set expire 12 mths later
                break;
            default:
                myDate = 0;
        }
        if (type == 'session' || !type) {
            document.cookie = cookieName + '=' + cookieValue + ';domain=.vodafone.com.au;path=/';
        } else {
            var now_utc = Date.UTC(myDate.getUTCFullYear(), myDate.getUTCMonth(), myDate.getUTCDate(), myDate.getUTCHours(), myDate.getUTCMinutes(), myDate.getUTCSeconds());
            myDate = new Date(now_utc);
            document.cookie = cookieName + '=' + cookieValue + ';expires=' + myDate + ';domain=.vodafone.com.au;path=/';
        }
    },
}
SERVbridgeFn.init();