(function(window) {
  //  console.log('pluto');
    var x           = 0;
    var repetitions = 30 ;
    var prd_data    = "{\"prd_complete\":true,\"prd_id\":\"8796036628797\",\"prd_url\":\"https:\\/\\/caldaiemuralii.myshopify.com\\/products\\/bolva-televisore-tv-bolva-39-led-hd-hotel-mode-dvb-tt2-bl-3966-ean-6971529343852\",\"prd_title\":\"Televisore TV Bolva 39\\\" LED HD Hotel Mode DVB-T\\/T2 BL-3966\",\"prd_price\":\"1.00\",\"prd_img\":\"https:\\/\\/cdn.shopify.com\\/s\\/files\\/1\\/0732\\/3756\\/1661\\/products\\/immagine-1-bolva-televisore-tv-bolva-39-led-hd-hotel-mode-dvb-tt2-bl-3966-ean-6971529343852.jpg\"}" ;
    var intervalID = window.setInterval(function () {
        var wrzp = document.getElementById("wrzpanel-container") ;
        if(!(window.Wrz && wrzp)) {
            if (++x === repetitions) {
                window.clearInterval(intervalID);
            }
            return ;
        }

        window.clearInterval(intervalID) ;

        /**********************************
        ** TRACE PRD VISIT
        ** vedere: 007-pagina.js
        **********************************/
        if(typeof hook_shopifyBeforePrdTrace === "function"){
			console.log('pluto');
            hook_shopifyBeforePrdTrace(prd_data) ;
        } else {
			console.log('pippo');
            wrz_trace('prdVisit', prd_data,'get_hist');
        }

        var cookie_data = JSON.parse(prd_data) ;
        var Properties  = window.Wrz.Properties ;
        var Server      = window.Wrz.Server ;
        var Cookies     = window.Wrz.Cookies ;
        var Functions   = window.Wrz.Functions ;
        var Events      = window.Wrz.Events ;

        //rating feedaty
        if(Properties.feedaty_show == 1){
            cookie_data = Functions.wrz_add_feedaty_rate(cookie_data,document);
        }

        var pid         = cookie_data.prd_id ;
        var prd_url     = cookie_data.prd_url;
        Events.trigger([Events.type.TIMEOUT_VISIT_PRODUCT], {prd_url: prd_url}) ;
        Events.trigger([
            Events.type.MULTIPLE_ENTER_PRODUCTS,
            Events.type.MULTIPLE_VISIT_PRODUCT
        ], {prd_id: pid}) ;

        /**
         * Se richiesto esegue operazioni specifiche di un sito
         * Esempio inserire il campo sconto per l'acquisto immediato.
         * vedere: worldztest.myshopify.com
         */
        // if (typeof hook_wrzPrdVisit === "function"){
        if(typeof hook_isShopifyAcquistoDiretto === "function" && hook_isShopifyAcquistoDiretto()) {
            hook_wrzPrdVisit(prd_url) ;
            console.log("demonnn");
            if(typeof hook_shopifyHandlerIncQuantity === "function"){
                hook_shopifyHandlerIncQuantity(hook_wrzPrdVisit) ;
            } else {
                $(".js-qty__adjust").on('click', function(evt){
                    hook_wrzPrdVisit(prd_url) ;
                }) ;
            }

            return ;
        }
        var cid     = Properties.cid ;
        var shop_id = Properties.shop_id ;
        var currentCart = Wrz.Cookies.get_cookie('worldzCart') ;

        // Functions.setIntervalX(function (intervalID) {
        //     if(hook_wrzDiscountShopify){
        //         window.clearInterval(intervalID);
        //         hook_wrzDiscountShopify(cid, shop_id, null, currentCart.discount) ;
        //     }
        // }, 300,10);
    }, 500);

    function hook_wrzPrdVisit(prd_url) {
        var cid         = Wrz.Properties.cid ;
        var shop_id     = Wrz.Properties.shop_id ;
        var tot         = (typeof hook_wrzPrdPrice === "function")?hook_wrzPrdPrice():0 ;
        if(!tot) return ;

        var formData = new FormData();
        formData.append('cid', cid);
        formData.append('ctot',tot );
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
            {
                //callback(xmlHttp.responseText)
                var discount_obj = JSON.parse(xmlHttp.responseText);
                discount_obj    = discount_obj.result;
                var fbPost      = discount_obj.fbpost || 0 ;
                var discountApplied = discount_obj.wallet_real_discount ;
                console.log("Discount",discountApplied);
                if(!fbPost) discountApplied = 0 ;
                if(shop_id == '3210729d2224940b5a36ba496566c132'){
                    hook_wrzDiscountShopify(cid, shop_id, '', 20) ;

                }else {
                    if (typeof hook_wrzDiscountShopify === "function") {
                        hook_wrzDiscountShopify(cid, shop_id, '', discountApplied);
                    }
                }
            }
        } ;
        xmlHttp.open("post", Wrz.Properties.apiurl_v + "/discount", true);
        xmlHttp.send(formData);

        var fetch   = window.fetch ;
        window.fetch = function(url, data) {
            if(url != "/wallets/checkouts.json" ) {
                return fetch.call(this, url, data) ;
            }
            var coupon  = wrzstoreL('WrzCouponShopify') ;
            var body    = JSON.parse(data.body) ;
            body.checkout.discount_code = coupon ;
            data.body = JSON.stringify(body) ;

            return fetch.call(this, url, data) ;
        } ;
    }



}.call(this, window));
