(function ($) {
    'use strict';

    function showMessage(text, type) {
        var $el = $('#rank-ms-chat-message');
        $el.removeClass('rank-ms-chat-message--success rank-ms-chat-message--error');
        if (type) {
            $el.addClass('rank-ms-chat-message--' + type);
        }
        $el.text(text || '');
    }

    function setLoading($btn, loading) {
        if (loading) {
            $btn.prop('disabled', true).data('original-text', $btn.text()).text('Please wait…');
        } else {
            $btn.prop('disabled', false).text($btn.data('original-text') || $btn.text());
        }
    }

    $('#rank-ms-chat-connect-btn').on('click', function () {
        var $btn = $(this);
        var payload = {
            action: 'rank_ms_chat_connect',
            nonce: rank_ms_chat_obj.nonce
        };

        var state = $('#rank-ms-chat-state').val();
        if (state) {
            payload.ms_chat_state = state;
        }

        setLoading($btn, true);
        showMessage('');

        $.post(rank_ms_chat_obj.ajax_url, payload)
            .done(function (response) {
                if (!response || !response.success) {
                    var err = (response && response.data && response.data.message) ? response.data.message : 'Connection failed.';
                    showMessage(err, 'error');
                    setLoading($btn, false);
                    return;
                }

                if (response.data && response.data.redirect_url) {
                    window.location.href = response.data.redirect_url;
                    return;
                }

                var successMsg = response.data.message || 'ms-chat connected successfully.';
                if (response.data && response.data.warning) {
                    successMsg += ' ' + response.data.warning;
                }

                showMessage(successMsg, 'success');
                window.setTimeout(function () {
                    window.location.reload();
                }, 800);
            })
            .fail(function (xhr) {
                var message = 'Connection failed.';
                if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
                    message = xhr.responseJSON.data.message;
                }
                showMessage(message, 'error');
                setLoading($btn, false);
            });
    });

    $('#rank-ms-chat-disconnect').on('click', function () {
        if (!window.confirm('Disconnect ms-chat from this site?')) {
            return;
        }

        var $btn = $(this);
        setLoading($btn, true);
        showMessage('');

        $.post(rank_ms_chat_obj.ajax_url, {
            action: 'rank_ms_chat_disconnect',
            nonce: rank_ms_chat_obj.nonce
        })
            .done(function (response) {
                if (!response || !response.success) {
                    var err = (response && response.data && response.data.message) ? response.data.message : 'Disconnect failed.';
                    showMessage(err, 'error');
                    setLoading($btn, false);
                    return;
                }

                showMessage(response.data.message || 'Disconnected.', 'success');
                window.setTimeout(function () {
                    window.location.reload();
                }, 600);
            })
            .fail(function () {
                showMessage('Disconnect failed.', 'error');
                setLoading($btn, false);
            });
    });
}(jQuery));
