;(function ($) {
    'use strict'

    var tfl_Wish_List_Remove = function () {
        $(document).on('click', '.tfl-call-wishlist.added', function (ev) {
            console.log('remove wishlist');


            var $this = $(this),
                product_id = $this.data('product_id'),
                row_id = $this.data('row_id'),
                wishlist_id = $this.data('wishlist_id'),
                wishlist_token = $this.data('wishlist_token'),
                wishlist_view = $this.data('view'),
                el_wrap = $this.closest('.tfl-btn-wishlist');
            $this.addClass('loading');

            window.location = wishlist_view;
        });
    }

    var tfl_Wish_List_Adding = function () {

        $(document).on('click', '.tfl-call-wishlist:not(.added)', function (ev) {
            console.log('add wishlist');
            var $this = $(this),
                product_id = $this.data('product_id'),
                el_wrap = $this.closest('.tfl-btn-wishlist'),
                data = {
                    action: yith_wcwl_l10n.actions.add_to_wishlist_action,
                    nonce: yith_wcwl_l10n.nonce.add_to_wishlist_nonce,
                    context: 'frontend',
                    add_to_wishlist: product_id
                };

            $this.addClass('loading');

            ev.preventDefault();


            $.ajax({
                type: 'POST',
                url: yith_wcwl_l10n.ajax_url,
                data: data,
                dataType: 'json',
                success: function (response) {
                    $this.addClass('added');
                    $this.removeClass('loading');
                    el_wrap.addClass('exists');
                    $this.attr('data-view', response.wishlist_url);
                    $('body').trigger('added_to_wishlist', [$this, el_wrap]);
                    tfl_Wish_List_Remove();
                }

            });

            return false;
        });


    }

    var tfl_Quickview = function () {

        var qv_modal = $(document).find('#yith-quick-view-modal'),
            qv_overlay = qv_modal.find('.yith-quick-view-overlay'),
            qv_content = qv_modal.find('#yith-quick-view-content'),
            qv_close = qv_modal.find('#yith-quick-view-close'),
            qv_wrapper = qv_modal.find('.yith-wcqv-wrapper'),
            qv_wrapper_w = qv_wrapper.width(),
            qv_wrapper_h = qv_wrapper.height();

        $(document).off('click', '.tfl-call-quickview').on('click', '.tfl-call-quickview', function (e) {
            e.preventDefault();

            var t = $(this),
                product_id = t.data('product_id'),
                is_blocked = false;

            t.addClass('loading');

            if (typeof yith_qv.loader !== 'undefined') {
                is_blocked = true;
                t.block({
                    message: null,
                    overlayCSS: {
                        background: '#fff url(' + yith_qv.loader + ') no-repeat center',
                        opacity: 0.5,
                        cursor: 'none'
                    }
                });

                if (!qv_modal.hasClass('loading')) {
                    qv_modal.addClass('loading');
                }

                // stop loader
                $(document).trigger('qv_loading');
            }
            ajax_call(t, product_id, is_blocked);
        });

        var ajax_call = function (t, product_id, is_blocked) {

            $.ajax({
                url: yith_qv.ajaxurl,
                data: {
                    action: 'yith_load_product_quick_view',
                    product_id: product_id,
                    lang: yith_qv.lang,
                    context: 'frontend',
                },
                dataType: 'json',
                type: 'POST',
                success: function (data) {

                    qv_content.html(data.html);
                    // Variation Form
                    var form_variation = qv_content.find('.variations_form');
                    form_variation.each(function () {
                        $(this).wc_variation_form();
                        // add Color and Label Integration
                        if (typeof $.fn.yith_wccl !== 'undefined') {
                            $(this).yith_wccl();
                        } else if (typeof $.yith_wccl != 'undefined' && data.prod_attr) {

                            $.yith_wccl(data.prod_attr);
                        }
                    });

                    form_variation.trigger('check_variations');
                    form_variation.trigger('reset_image');


                    if (!qv_modal.hasClass('open')) {
                        qv_modal.removeClass('loading');

                    }

                }

            }).done(function() {

                qv_modal.addClass("open");
                t.removeClass('loading');

            });


        };

    }


    // Dom Ready
    $(function () {
        tfl_Wish_List_Adding();
        tfl_Wish_List_Remove();
        tfl_Quickview();
    });
})(jQuery);