(async function ($) {
    $(document).ready(function () {

        var now = new Date();
        var day = ("0" + now.getDate()).slice(-2);
        var month = ("0" + (now.getMonth() + 1)).slice(-2);
        var today = now.getFullYear()+"-"+(month)+"-"+(day) ;

        $('#datePicker').val(today);

        if ($('.rest-gallery').length > 0) {
            $('.gallery').slick({
                slidesToShow: 1,
                slidesToScroll: 1,
                arrows: true,
                infinite: true,
                fade: true,
                prevArrow: '<div class="slick-prev slick-nav"><svg class="icon"><use xlink:href="#icon-left"></use></svg></div>',
                nextArrow: '<div class="slick-next slick-nav"><svg class="icon"><use xlink:href="#icon-right"></use></svg></div>'
            });

            $(".gallery").lightGallery({
                selector: '.img',
                download: false
            });
        }

        if ($('.rest-recommended').length > 0) {
            $('.rest-recommended-items').slick({
                slidesToShow: 4,
                slidesToScroll: 1,
                arrows: true,
                infinite: true,
                prevArrow: '<div class="slick-prev slick-nav"><svg class="icon"><use xlink:href="#icon-left"></use></svg></div>',
                nextArrow: '<div class="slick-next slick-nav"><svg class="icon"><use xlink:href="#icon-right"></use></svg></div>',
                responsive: [
                    {
                        breakpoint: 1210,
                        settings: {
                            slidesToShow: 2
                        }
                    }
                ]
            });
        }

        // if($('#menu').length > 0) {
        //     var tabs_menu = tabs({
        //         el: '#menu',
        //         tabNavigationLinks: '.c-tabs-nav__link',
        //         tabContentContainers: '.c-tab'
        //     });
        //     tabs_menu.init();
        // }

        // if($('#faq').length > 0) {
        //     var tabs_faq = tabs({
        //         el: '#faq',
        //         tabNavigationLinks: '.c-tabs-nav__link',
        //         tabContentContainers: '.c-tab'
        //     });
        //     tabs_faq.init();
        // }

        if($('#order').length > 0) {
            var tabs_order = tabs({
                el: '#order',
                tabNavigationLinks: '.c-tabs-nav__link',
                tabContentContainers: '.c-tab'
            });
            tabs_order.init();
        }
        
        if($('#main-tabs').length > 0) {
            var tabs_order = tabs({
                el: '#main-tabs',
                tabNavigationLinks: '.c-tabs-nav__link',
                tabContentContainers: '.c-tab'
            });
            tabs_order.init();
        }

        $('.faq .open-ask-form').click(function () {
            $('.faq .ask-form').show();
            $('.faq .close-ask-form').removeClass('hidden');
            $('.faq .open-ask-form').addClass('hidden');
        });
        
        $('.faq .close-ask-form').click(function () {
            $('.faq .ask-form').hide();
            $('.faq .open-ask-form').removeClass('hidden');
            $('.faq .close-ask-form').addClass('hidden');
        });

        $('.review .open-review-form').click(function () {
            $('.review .review-form').show();
            $('.review .close-review-form').removeClass('hidden');
            $('.review .open-review-form').addClass('hidden');
        });
        
        $('.review .close-review-form').click(function () {
            $('.review .review-form').hide();
            $('.review .open-review-form').removeClass('hidden');
            $('.review .close-review-form').addClass('hidden');
        });
        
        $('.all-privilege').click(function () {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active');
                $('.rest-privilege-content').removeClass('full');
                $(this).html("View all privilege");
            } else {
                $(this).addClass('active');
                $(this).html("View less");
                $('.rest-privilege-content').addClass('full');
            }
        });
        
        // $('.open-full-menu').click(function () {
        //     if ($(this).hasClass('active')) {
        //         $(this).removeClass('active');
        //         $('.menu_wrapper').removeClass('full');
        //         $(this).html("View all menu");
        //     } else {
        //         $(this).addClass('active');
        //         $(this).html("Hide all menu");
        //         $('.menu_wrapper').addClass('full');
        //     }
        // });
        
        $('.open-order').click(function (e) {
            e.preventDefault();
            $('.overlay').fadeIn('fast', function () {
                $('#nonebox-order').animate({
                    'top': '50%'
                }, 500);
                $('body').css('overflow', 'hidden');
            });
        });

        $('.open-reserve').click(function (e) {
            e.preventDefault();
            $('.overlay').fadeIn('fast', function () {
                $('#nonebox-reserve').animate({
                    'top': '50%'
                }, 500);
                $('body').css('overflow', 'hidden');
            });
        });

        $('.open-map').click(function (e) {
            e.preventDefault();
            $('.overlay').fadeIn('fast', function () {
                $('#nonebox-map').animate({
                    'top': '50%'
                }, 500);
                $('body').css('overflow', 'hidden');
            });
        });

        $('.box-close, .overlay').click(function (e) {
            e.preventDefault();
            $('.nonebox').animate({
                'top': '-1100px'
            }, 500, function () {
                $('.overlay').fadeOut('fast');
                $('body').css('overflow', 'visible');
            });
        });

        $('.open-sub-item').click(function () {
            $(this).next().toggleClass('active');
            $(this).toggleClass('active');
        });

        $('.rest-phone .hide-phone').click(function () {
            $(this).hide();
            $(this).next('.show-phone').show();
        });

        //Rating-line
        $('#rating .item').on('mouseover', function () {
            var onStar = parseInt($(this).data('value'), 10); 
            $(this).parent().children('.item').each(function (e) {
                if (e < onStar) {
                    $(this).addClass('hover');
                } else {
                    $(this).removeClass('hover');
                }
            });
        }).on('mouseout', function () {
            $(this).parent().children().each(function (e) {
                $(this).removeClass('hover');
            });
        });
        $('#rating .item').on('click', function () {
            var onStar = parseInt($(this).data('value'), 5);
            var stars = $(this).parent().children('.item');
            for (i = 0; i < stars.length; i++) {
                $(stars[i]).removeClass('selected');
            }
            for (i = 0; i < onStar; i++) {
                $(stars[i]).addClass('selected');
            }
            var ratingValue = parseInt($('#rating .item.selected').last().data('value'), 5);
            $('.rating-value').val(ratingValue);
        });

        $('#send_form_qa').click(function(e){
            e.preventDefault();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                url: "/qa/",
                method: 'post',
                data: $('#ask-form').serialize(),
                success: function(response){
                    $('#ask-form p').html("");
                    $('.error').removeClass("error");
                    if(response.errors) {
                        Object.entries(response.errors).forEach((item, index) => {
                            $('#ask-form [name="' + item[0] + '"]' ).addClass('error')
                            tippy('#ask-form [name="' + item[0] + '"]', {
                                content: item[1][0],
                                maxWidth: '280',
                                showOnCreate: true,
                            });
                        })
                    } else {
                        $('.overlay').fadeIn('fast', function () {
                            $('#nonebox-ask').animate({
                                'top': '50%'
                            }, 500);
                            $('body').css('overflow', 'hidden');
                        });

                        document.getElementById("ask-form").reset();
                        setTimeout(function () {
                            $('.nonebox').animate({
                                'top': '-1100px'
                            }, 500, function () {
                                $('.overlay').fadeOut('fast');
                                $('body').css('overflow', 'visible');
                            });
                        }, 3000);
                    }
                }});
        });

        $('#reservation-form-send').click(function(e){
            e.preventDefault();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                url: "/reservation/",
                method: 'post',
                data: $('#reservation-form').serialize(),
                success: function(response){
                    $('#reservation-form p').html("");
                    $('.error').removeClass("error");
                    if(response.errors) {
                        Object.entries(response.errors).forEach((item, index) => {
                            $('#reservation-form [name="' + item[0] + '"]' ).addClass('error')
                            tippy('#reservation-form [name="' + item[0] + '"]', {
                                content: item[1][0],
                                maxWidth: '280',
                                showOnCreate: true,
                            });
                        })
                    } else {
                        $('#nonebox-reserve').animate({
                            'top': '-1950px'
                        }, 500);
                        $('.overlay').fadeIn('fast', function () {
                            $('#nonebox-takeout').animate({
                                'top': '50%'
                            }, 500);
                            $('body').css('overflow', 'hidden');
                        });

                        document.getElementById("reservation-form").reset();
                        setTimeout(function () {
                            $('.nonebox').animate({
                                'top': '-1100px'
                            }, 500, function () {
                                $('.overlay').fadeOut('fast');
                                $('body').css('overflow', 'visible');
                            });
                        }, 3000);
                    }
                }});
        });

        $('.order-sub').click(function(e){
            e.preventDefault();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                url: "/order/",
                method: 'post',
                data: $('#order-form').serialize(),
                success: function(response){
                    $('#order-form p').html("");
                    $('.error').removeClass("error");
                    if(response.errors) {
                        Object.entries(response.errors).forEach((item, index) => {
                            $('#order-form [name="' + item[0] + '"]' ).addClass('error')
                            tippy('#order-form [name="' + item[0] + '"]', {
                                content: item[1][0],
                                maxWidth: '280',
                                showOnCreate: true,
                            });
                        })
                    } else {
                        $('#nonebox-order').animate({
                            'top': '-1950px'
                        }, 500);
                        $('.overlay').fadeIn('fast', function () {
                            $('#nonebox-takeout').animate({
                                'top': '50%'
                            }, 500);
                            $('body').css('overflow', 'hidden');
                        });

                        document.getElementById("order-form").reset();
                        setTimeout(function () {
                            $('.nonebox').animate({
                                'top': '-1100px'
                            }, 500, function () {
                                $('.overlay').fadeOut('fast');
                                $('body').css('overflow', 'visible');
                            });
                        }, 3000);
                    }
                },
                error: function (resp) {
                    console.log(resp.data)
                }
            });
        });

        $('.order-sub-2').click(function(e){
            e.preventDefault();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                url: "/order/",
                method: 'post',
                data: $('#order-form-2').serialize(),
                success: function(response){
                    $('#order-form-2 p').html("");
                    $('.error').removeClass("error");
                    if(response.errors) {
                        Object.entries(response.errors).forEach((item, index) => {
                            $('#order-form-2 [name="' + item[0] + '"]' ).addClass('error')
                            tippy('#order-form-2 [name="' + item[0] + '"]', {
                                content: item[1][0],
                                maxWidth: '280',
                                showOnCreate: true,
                            });
                        })
                    } else {
                        $('#nonebox-order').animate({
                            'top': '-1950px'
                        }, 500);
                        $('.overlay').fadeIn('fast', function () {
                            $('#nonebox-takeout').animate({
                                'top': '50%'
                            }, 500);
                            $('body').css('overflow', 'hidden');
                        });

                        document.getElementById("order-form").reset();
                        setTimeout(function () {
                            $('.nonebox').animate({
                                'top': '-1100px'
                            }, 500, function () {
                                $('.overlay').fadeOut('fast');
                                $('body').css('overflow', 'visible');
                            });
                        }, 3000);
                    }
                },
                error: function (resp) {
                    console.log(resp.data)
                }
            });
        });

        $('#send_form_comment').click(function(e){
            e.preventDefault();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                url: "/comment/",
                method: 'post',
                data: $('#comment-form').serialize(),
                success: function(response){
                    $('#comment-form p').html("");
                    $('.error').removeClass("error");
                    if(response.errors) {
                        Object.entries(response.errors).forEach((item, index) => {
                            if(item[0] !== 'rate') {
                                $('#comment-form [name="' + item[0] + '"]').addClass('error');
                                tippy('#comment-form [name="' + item[0] + '"]', {
                                    content: item[1][0],
                                    maxWidth: '280',
                                    showOnCreate: true,
                                });
                            }
                            else {
                                $('#comment-form .rest-rating').addClass('error');
                                tippy('#comment-form .rest-rating', {
                                    content: item[1][0],
                                    maxWidth: '280',
                                    showOnCreate: true,
                                });
                            }

                        })
                    } else {
                        $('.overlay').fadeIn('fast', function () {
                            $('#nonebox-review').animate({
                                'top': '50%'
                            }, 500);
                            $('body').css('overflow', 'hidden');
                        });
                        document.getElementById("comment-form").reset();
                        setTimeout(function () {
                            $('.nonebox').animate({
                                'top': '-1100px'
                            }, 500, function () {
                                $('.overlay').fadeOut('fast');
                                $('body').css('overflow', 'visible');
                            });
                        }, 3000);
                    }
                }});
        });

        /*tippy('.user-mail.error', {
            content: 'The email field is required.',
            maxWidth: '280',
            showOnCreate: true,
        });
        tippy('.user-comment.error', {
            content: 'The comment field is required.',
            maxWidth: '280',
            showOnCreate: true,
        });
        tippy('.rest-rating.error', {
            content: 'The rating field is required.',
            maxWidth: '280',
            showOnCreate: true,
        });
        tippy('.user-question.error', {
            content: 'The question field is required.',
            maxWidth: '280',
            showOnCreate: true,
        });*/


    });

    
    async function checkForAdBlocker() {
        let Blocked;
    
        async function RequestT() {
        try {
            return fetch(
            new Request(
    "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", {
                method: 'HEAD',
                mode: 'no-cors'
            }))
            .then(function(response) {
                // There is no AdBlocker
                Blocked = false;
                return Blocked;
            }).catch(function(e) {
                // Failed, Because of an AdBlocker
                Blocked = true;
                return Blocked;
            });
        } catch (error) {
            console.log(error);
            Blocked = true;
            return Blocked;
        }
        }
    
        return Blocked !== undefined ? Blocked : await RequestT();
    }
    
    const adBlock = await checkForAdBlocker();
    console.log(adBlock? "Shit is adBlocked" : 'Ok we ready');
    if(adBlock === false) {
        if($(".adsbygoogle").length < 0) {
            console.log('Gooogle not load #1');
        } else {
            console.log('Gooogle start load #1 - ' + $(".adsbygoogle").length);
        }

        if($("ins[data-ad-client='ca-pub-8169175608100594']").length < 0){
            console.log('Gooogle not load #2');
        } else {
            console.log('Gooogle start load #2 - ' + $("ins[data-ad-client='ca-pub-8169175608100594']").length);
        }
    }

})(jQuery);
