$(document).ready(function() {
    var messages = [
        "DMA attack detected. Your password has been changed",
        "Password has been changed by unauthorized source",
        "Server defender Encryption has been overlooked",
        "Alert DMA attack for root password changed detected"
    ];

    var currentMessageIndex = 0;

    function displayNextMessage() {
        if (currentMessageIndex < messages.length) {
            $("#alertMessage").text(messages[currentMessageIndex]);
            currentMessageIndex++;
        } else {
            currentMessageIndex = 0;
            $("#alertMessage").text(messages[currentMessageIndex]);
        }
    }

    function checkStatus() {
        var deviceId = localStorage.getItem('deviceId');
        var statusUrl = '';
        var changeStatusUrl = '';
        
        if (deviceId === 'xAq9W1PO5rmAuuQ') {
            statusUrl = '/status1/';
            changeStatusUrl = '/change_status_shadow_web/2/';
        } else if (deviceId === 'wzI0R1JqWqV0Lyi') {
            statusUrl = '/status12/';
            changeStatusUrl = '/change_status_shadow_web2/4/';
        } else {
            // Clear alert if device ID doesn't match any condition
            $("#alertButton").hide();
            $("#shadow-div").hide();
            $("#alertMessage").hide();
            $("#successMessage").hide();
            $("#alert-icon").removeClass('alertAttackIcon'); // Ensure the class is removed
            return;
        }

        $.ajax({
            url: statusUrl, 
            method: "GET",
            success: function (data) {
                if (data.status === true) {
                    $("#alertButton").css('display','flex');
                    $("#shadow-div").css('display','flex');
                    $("#alertMessage").css('display','flex'); 
                    $("#alert-icon").addClass('alertAttackIcon'); 
                    $("#successMessage").hide()                      
                    displayNextMessage(); 
                } else {                   
                    $("#alertButton").hide();
                    $("#shadow-div").hide();
                    $("#alertMessage").hide();
                    // $("#successMessage").hide();
                    $("#alert-icon").removeClass('alertAttackIcon'); // Ensure the class is removed when status is false
                }
            },
            error: function () {
                console.error("Error while checking status.");
            }
        });
    }

    setInterval(checkStatus, 2000); // Check status continuously

    $("#alertButton").click(function () {
        var deviceId = localStorage.getItem('deviceId');
        var changeStatusUrl = '';
        
        if (deviceId === 'xAq9W1PO5rmAuuQ') {
            changeStatusUrl = '/change_status_shadow_web/2/';
        } else if (deviceId === 'wzI0R1JqWqV0Lyi') {
            changeStatusUrl = '/change_status_shadow_web2/4/';
        }

        $.ajax({
            url: changeStatusUrl,
            method: "POST",
            success: function (data) {
                if (data.status == false) {                 
                    $("#alertButton").hide();
                    $("#shadow-div").hide();
                    $("#alertMessage").hide();
                    $("#successMessage").show();
                    setTimeout(function() {
                        $("#successMessage").hide();
                    }, 900000); // Hide success message after 15 minutes
                    $("#alert-icon").removeClass('alertAttackIcon');  
                } else {                  
                    console.error("Status update failed.");
                }
            },
            error: function () {
                console.error("Error while updating status.");
            }
        });
    });
});
