/*
*/
if (typeof GTS === 'undefined') {
    console.log("%c init GTS ", "background: #1960bc; color: #fff", );
    window.GTS = class GTS {
        static publicLog(text) {
            console.log("%c " + text, "background: #1960bc; color: #fff", );
        }
        static getStoreId() {
                return Shopify.shop;
            }
            /**
             * 
             * @param {number} amount 
             */
        static moneyFormat(money_format, amount) {
            var price = amount / 100;
            return String(money_format).replace('{{amount}}', price.toFixed(2)) + '';
        }
        static log(text) {
            var debug = localStorage.getItem("debug");
            if (debug == true) {
                console.log(text);
            }
        }
        static warn(text) {
            var debug = localStorage.getItem("debug");
            if (debug == true) {
                console.warn(text);
            }
        }
        static debug(val = true) {
            localStorage.setItem("debug", val);
        }
        static getIterateChild(lastElement) {
            var lastChild = lastElement.find('div,span');
            if (lastChild.length) {
                lastElement = this.getIterateChild(lastChild);
            }
            return lastElement;
        }
        static isMobile() {
            if (window.innerWidth <= 800 && window.innerHeight <= 900) {
                return true;
            } else {
                return false;
            }
        }
        static loadLink(url) {
            jQuery('head').append('');
        }
        static loadScriptList(app) {
            GTS.log('loadScriptList');
            if ((typeof jQuery === 'undefined')) {
                GTS.loadScript('https://cdn.shopify.com/s/files/1/0020/7624/6127/t/8/assets/jquery-2.2.3.min.js', function() {
                    //window.GTS.jQuery191 = jQuery.noConflict(true);
                    //  GTS.loadScript('//cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js');
                    //   GTS.loadScript('//cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/cart.min.js');
                    //   GTS.loadScript('//cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/rivets-cart.min.js');
                    //  GTS.loadScript('//rafaelcastrocouto.github.io/jQuery.DomOutline/demo/jquery.dom-outline-1.0.js');
                    app.myAppJavaScript(jQuery);
                });
            } else if ((parseFloat(jQuery.fn.jquery) < 1.7)) {
                GTS.loadScript('https://cdn.shopify.com/s/files/1/0020/7624/6127/t/8/assets/jquery-2.2.3.min.js', function() {
                    window.GTS.jQuery191 = jQuery.noConflict(true);
                    //  GTS.loadScript('//cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js');
                    //   GTS.loadScript('//cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/cart.min.js');
                    //   GTS.loadScript('//cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/rivets-cart.min.js');
                    //  GTS.loadScript('//rafaelcastrocouto.github.io/jQuery.DomOutline/demo/jquery.dom-outline-1.0.js');
                    app.myAppJavaScript(window.GTS.jQuery191);
                });
            } else {
                GTS.loadScript('//code.jquery.com/ui/1.12.1/jquery-ui.min.js');
                GTS.loadScript('//cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/cart.min.js');
                GTS.loadScript('//cdnjs.cloudflare.com/ajax/libs/shopify-cartjs/0.4.1/rivets-cart.min.js');
                GTS.loadScript('//rafaelcastrocouto.github.io/jQuery.DomOutline/demo/jquery.dom-outline-1.0.js');
                app.myAppJavaScript(jQuery);
            }
        }
        static loadScript(url, callback) {
            if (callback == undefined) {
                callback = function() {}
            }
            var script = document.createElement('script');
            script.type = 'text/javascript';
            // If the browser is Internet Explorer.
            if (script.readyState) {
                script.onreadystatechange = function() {
                    if (script.readyState === 'loaded' || script.readyState === 'complete') {
                        script.onreadystatechange = null;
                        callback();
                    }
                };
                // For any other browser.
            } else {
                script.onload = function() {
                    callback();
                };
            }
            script.src = url;
            document.getElementsByTagName('head')[0].appendChild(script);
        }
        static getLocalDayHour() {
            var date = new Date();
            return date.getDate() + '-' + date.getHours();
        }
        static checkCart() {
            if (window.GTS.cart != undefined) {
                return;
            }
            $("head").append('');
            var elem = $('meta[name="gts_cart:count"]');
            window.GTS.cart = { item_count: 0 };
            setInterval(function() {
                jQuery.getJSON('/cart.js', function(cart) {
                    if (window.GTS.cart.item_count != cart.item_count) {
                        window.GTS.cart = cart;
                        elem.attr('content', cart.item_count);
                    }
                });
            }, 1000);
        }
    }
}
/**
 * main obj
 */
GTSB5 = function() {
    this.init = function() {
        if (localStorage.getItem('top_banner_close')) {
            GTS.log('off');
            // return;
        }
        GTS.log('init 0t');
        if (window.GTSB5_init == undefined) {
            window.GTSB5_init = this;
            window.sbfive = { total_price: null, total_price_update: null, cart: null, item_count: 0 };
            GTS.loadScriptList(this);
            checkCart();
        }
    }
    function checkCart() {
        setInterval(function() {
            jQuery.getJSON('/cart.js', function(cart) {
                let count = 0;
                if (cart.item_count != undefined) {
                    count = cart.item_count;
                }
                if (window.sbfive.item_count != count) {
                    window.sbfive.cart = cart;
                    if (typeof GTcartAjax != 'undefined') {
                        new GTcartAjax().get_total(cart);
                    }
                }
            });
        }, 3000);
    }
    function getBaseUrl() {
        return 'https://sb5.tengrowth.com/';
    }
    /**
     * 
     * @param {*} $ 
     */
    this.myAppJavaScript = function($) {
        GTS.loadLink(getBaseUrl() + "css/template.css");
        GTS.log('pre ajax');
        showBanner(1);
    }
    function showBanner(load) {
        GTS.log('showBanner');
        setTimeout(function() {
            GTS.log('total_price:' + window.sbfive.total_price);
            if (window.sbfive.cart === undefined || window.sbfive.cart == null) {
                return showBanner(1);
            }
            console.log(window.sbfive.cart);
            if (window.sbfive.total_price != window.sbfive.cart.total_price ||
                load ||
                (typeof GTcartAjax != 'undefined' && window.sbfive.total_price_update == window.gtapps_qd.discounted_price_total)
            ) {
                window.sbfive.total_price = window.sbfive.cart.total_price;
                var total = window.sbfive.total_price;
                if (typeof GTcartAjax != 'undefined') {
                    window.sbfive.total_price_update = window.gtapps_qd.discounted_price_total;
                    total = window.sbfive.total_price_update;
                }
                console.log('--------');
                console.log(total);
                var init = 0;
                if (localStorage.getItem('init') == null) {
                    init = 1;
                    localStorage.setItem('init', 1);
                }
                jQuery.ajax({
                    type: "GET",
                    url: getBaseUrl() + "group/config",
                    data: {
                        store_id: GTS.getStoreId(),
                        url: window.location.href,
                        price: total,
                        is_mobile: GTS.isMobile(),
                        init: init
                    },
                    crossDomain: true,
                    success: function(res) {
                        group = JSON.parse(res);
                        if (typeof group.settings == 'undefined') {
                            return;
                        }
                        settings = JSON.parse(group.settings);
                        GTS.log('group');
                        GTS.log(settings);
                        GTS.log(settings.target.position);
                        jQuery('.top_banner').remove();
                        switch (settings.target.position) {
                            case '0':
                                jQuery('body').prepend(group.template);
                                break;
                            case '4':
                                jQuery(group.template).appendTo("body");
                                jQuery('.top_banner').addClass('bottom-fix');
                                break;
                            case '5':
                                jQuery(group.template).appendTo("#bs5_placeholder");
                                break;
                            default:
                                // code block
                        }
                        jQuery('.top_banner .congrats').html(group.message_before);
                        jQuery('.top_banner .close').click(function() {
                            localStorage.setItem("top_banner_close", true);
                            jQuery('.top_banner').remove();
                        });
                    }
                });
            }
            if (localStorage.getItem('top_banner_close')) {
                return;
            }
            showBanner(0);
        }, 2000);
    }
}
new GTSB5().init();