"use strict";

(function ($) {
    $(document).on('click', '#wpil_url_changer_table .delete', wpil_url_changer_delete);
    $(document).on('click', '#wpil_url_changer_reset_button', wpil_url_changer_reset);

    if (is_wpil_url_changer_reset) {
        wpil_url_changer_reset_process(2, 1);
    }

    function wpil_url_changer_delete() {
        if (confirm("Are you sure you want to delete this URL?")) {
            var el = $(this);
            var id = el.data('id');

            $.post(ajaxurl, {
                action: 'wpil_url_changer_delete',
                id: id
            }, function(){
                el.closest('tr').fadeOut(300);
                wpil_swal('Success!', 'URLs have been replaced with the old ones.', 'success');
            });
        }
    }

    function wpil_url_changer_reset() {
        $('#wpil_url_changer_table .table').hide();
        $('#wpil_url_changer_table .progress').show();
        wpil_url_changer_reset_process(1, 1);
    }

    function wpil_url_changer_reset_process(count, total) {
        $.ajax({
            type: "POST",
            url: ajaxurl,
            data: {
                action: 'wpil_url_changer_reset',
                nonce: wpil_url_changer_nonce,
                count: count,
                total: total,
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var wrapper = document.createElement('div');
                $(wrapper).append('<strong>' + textStatus + '</strong><br>');
                $(wrapper).append(jqXHR.responseText);
                wpil_swal({"title": "Error", "content": wrapper, "icon": "error"}).then(function(){
                    wpil_url_changer_reset_process(1, 1)
                });
            },
            success: function(response){
                if(!isJSON(response)){
                    response = extractAndValidateJSON(response, ['error', 'ready', 'total', 'finish']);
                }

                if (response.error) {
                    wpil_swal(response.error.title, response.error.text, 'error');
                    return;
                }

                var progress = Math.floor((response.ready / response.total) * 100);
                $('#wpil_url_changer_table .progress .progress_count').text(progress + '%' + ' ' + response.ready + '/' + response.total);
                if (response.finish) {
                    location.reload();
                } else {
                    wpil_url_changer_reset_process(response.count, response.total)
                }
            }
        });
    }

    if(created_rule_id && created_rule_id > 0){
        ajax_change_urls_by_id(1); // pass a 1 to get the counter started
    }

    function ajax_change_urls_by_id(remaining = 0, lastCount = 0){
        if(!created_rule_id || !remaining || parseInt(remaining) === parseInt(lastCount)){
            return;
        }

        $.ajax({
            type: "POST",
            url: ajaxurl,
            data: {
                action: 'wpil_url_changer_by_id',
                nonce: wpil_url_changer_nonce,
                id: created_rule_id
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var wrapper = document.createElement('div');
                $(wrapper).append('<strong>' + textStatus + '</strong><br>');
                $(wrapper).append(jqXHR.responseText);
                wpil_swal({"title": "Error", "content": wrapper, "icon": "error"}).then(function(){
                    wpil_url_changer_reset_process(1, 1)
                });
            },
            success: function(response){
                if(!isJSON(response)){
                    response = extractAndValidateJSON(response, ['error', 'ready', 'total', 'finish']);
                }

                if (response.error) {
                    wpil_swal(response.error.title, response.error.text, 'error');
                    return;
                }

                var progress = Math.floor((response.ready / response.total) * 100);
                var text = (response.finish) ? 'Changing Complete!': progress + '%' + ' ' + response.ready + '/' + response.total;
                $('#wpil_url_changer_table .progress .progress_count').text(text);
                if (response.finish || parseInt(response.remaining) === parseInt(response.lastCount)) {
                    location.reload();
                } else {
                    ajax_change_urls_by_id(response.remaining, response.lastCount)
                }
            }
        });
    }
})(jQuery);