(function ($) {
    'use strict';
    $("document").ready(function () {
        $('.options-wrap').on('click', "button", function () {
            var id = $(this).attr("data-target");
            $(".content-wrap .tab").removeClass("active");
            $('.content-wrap #' + id).addClass("active");
            $(".msg").html("");
        });

        $("#regenerate-sitemap").on("click", function () {
            var loader = $("#tab-1 .loader");
            $("#tab-1 .msg").html("");
            loader.addClass("active");
            $.ajax({
                url: ada_plugin.ajaxurl,
                method: "POST",
                data: { action: "oada__regenerate_sitemap" },
                success: function (response) {
                    loader.removeClass("active"); $("#tab-1 .msg").html("finished");
                    generate_sitemap_list(response);
                },
                error: function (response) { loader.removeClass("active"); $("#tab-1 .msg").html("There was a problem creating your sitemap"); },
                complete: function (response) { loader.removeClass("active"); },
            });
        });

        $(".sitemap-list").on("click", "li button.remove", function () {
            $(this).closest("li").remove();
        });

        $("#tab-3").on("click", "#button-add", function () {
            var value = $("#input-add").val();
            $(".sitemap-list").append("<li><div><span>" + ada_plugin.home_url + "/" + value + "</span><button class='remove'>remove</button></div></li>");
            $("#input-add").val("");
        });

        $("#tab-3").on("click", "#save-sitemap", function () {
            var sitemap = [];
            $.each($(".sitemap-list li:not('.query-type')"), function () {
                if ($(this).find(".e_m").length > 0) return;
                var page = $(this).find("span:first-of-type").text();
                sitemap.push(page);
            });

            var loader = $("#tab-3 .loader");
            var msg = $("#tab-3 .msg");
            loader.addClass("active");
            msg.html("");

            $.ajax({
                url: ada_plugin.ajaxurl,
                method: "POST",
                data: {
                    action: "oada__save_sitemap",
                    sitemap: sitemap,
                },
                success: function (response) {
                    msg.html("Finished");
                    if (response == "empty") { $(".sitemap-list").html("<span>The sitemap is empty. Click 'Re-Create sitemap' above to regenerate a list of pages from the database OR click 'Upload a sitemap' to upload a sitemap file.</span>"); }
                    else { generate_sitemap_list(response); }
                },
                error: function (response) { msg.html("There was a problem saving your sitemap"); },
                complete: function (response) { loader.removeClass("active"); },
            });
        });

        $(document).on("change", '#fileUpload', function () {
            var fileName = '';
            if ($(this).prop("files").length > 0) { fileName = $(this).prop("files")[0].name; }
            $(this).nextAll('.output-wrapper').find(".file").text(fileName);
        });

        $(document).on("click", ".save-upload", function () {
            var file = $('#fileUpload').prop("files")[0];
            var form_data = new FormData();
            form_data.append('file', file);
            form_data.append('action', "oada__parse_upload");
            $('#tab-2 .loader').addClass("active");
            $("#tab-2 .msg").html("");

            $.ajax({
                url: ada_plugin.ajaxurl,
                method: "POST",
                data: form_data,
                contentType: false,
                processData: false,
                success: function (response) {
                    $('#tab-2 .loader').removeClass("active");
                    $("#tab-2 .msg").html("finished");
                    generate_sitemap_list(response);
                },
            });
        });

        function generate_sitemap_list(response) {
            var sitemap = JSON.parse(response);
            var sitemap_list = $("#tab-3 .sitemap-list");
            sitemap_list.html("");
            var html = "";
            var type_label = "";
            $.each(sitemap, function (index, value_arr) {
                if (index == "query_links") { type_label = "Query Links"; }
                else { type_label = "Non Query Links"; }
                html += "<li class='query-type " + type_label + "' ><span>" + type_label + "</span></li>";
                $.each(value_arr, function (index, value) {
                    html += validate_sitemap_value(value);
                });
            });
            sitemap_list.append(html);
        }

        function validate_sitemap_value(value) {
            var e_i = "<i class='fa fa-exclamation e_i'></i>";
            var html = "<li><div>";
            var error_msg = "";

            if ((!value.includes("http://") || value.indexOf("http://") !== 0) &&
                (!value.includes("https://") || value.indexOf("https://") !== 0)) {
                html += e_i;
                error_msg = "Domain does not start with a protocol of http or https. This link will not be used.";
            }
            else if (!value.includes(ada_plugin.home_url)) {
                html += e_i;
                error_msg = "Domain does not match this site's domain (" + ada_plugin.home_url + "). This link will not be used";
            }

            html += "<span>" + value + "</span>";
            html += "<button class='remove'>remove</button>";
            if (error_msg !== "") { html += "<span class='e_m'>" + error_msg + "</span>"; }
            html += "</div></li>";
            return html;
        }
    });
})(jQuery);
