;(function($) {
    'use strict';

    const App = {
        data: {
            deactivationUrl: null,
            $feedback: null
        },
        fn: {
            show: (deactivationUrl) => {
                App.globals = gutendraw_feedback_globals;
                App.data.deactivationUrl = deactivationUrl;
                App.data.$feedback = $('#gutendraw-feedback');

                App.data.$feedback.css("display","");
                App.data.$feedback.find(".gutendraw-fields").off().on("click", "input[type=radio]", App.fn.select);
                App.data.$feedback.find(".gutendraw-close").off().on("click", App.fn.close);
                App.data.$feedback.find(".gutendraw-btn.gutendraw-submit").off().on("click", App.fn.submit);
                App.data.$feedback.find(".gutendraw-btn.gutendraw-skip").off().on("click", App.fn.skip);

                setTimeout(() => {
                    App.data.$feedback.addClass("gutendraw-active");
                });
            },
            select: (e) => {
                const $radio = $(e.target);
                App.data.$feedback.find(".gutendraw-fields input[type=text]").removeClass("gutendraw-active");
                $radio.closest('.gutendraw-field').find("input[type=text]").addClass("gutendraw-active");
            },
            close: () => {
                App.data.$feedback.removeClass("gutendraw-active");
            },
            deactivate: () => {
                App.fn.close();
                window.location.href = App.data.deactivationUrl;
                $(document).off("click", "tr[data-slug='gutendraw']");
            },
            skip: () => {
                App.fn.deactivate();
            },
            submit: () => {
                const $radio = App.data.$feedback.find(".gutendraw-fields input[type=radio]:checked");
                if($radio.length) {
                    const $description = $radio.closest(".gutendraw-field").find("input[type=text]");
                    const description = $description.length ? $description.val() : "";
                    const reason = $radio.val();
                    const data = JSON.stringify({
                        token: App.globals.token,
                        reason: reason,
                        description: description
                    });

                    $.ajax({
                        url: App.globals.api.feedback_url,
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json",
                        data: data
                    }).always(() => {
                        App.fn.deactivate();
                    });
                } else {
                    App.fn.deactivate();
                }
            }
        }
    }

    $(document).on("click", "tr[data-slug='gutendraw']", (e) => {
        if($(e.target).parent().hasClass("deactivate")) {
            e.preventDefault();
            App.fn.show(e.target.href);

            $(window).on("keydown", (e) => {
                if (e.key === "Escape" || e.keyCode === 27) {
                    App.fn.close();
                }
            });
        }
    });
})(jQuery);