// Task Status Helper - Utility functions for task status pages
class TaskStatusHelper {
    constructor() {
        this.taskLogManager = null;
        this.diagnosticManager = null;
    }

    // Initialize the task status page with SignalR and diagnostics
    initializeTaskStatus(serverUrl, taskId, diagnosticUrl, diagnosticsSent) {
        console.log("Initializing task status page");

        // Initialize SignalR connection
        this.taskLogManager = new TaskLogConnectionManager(serverUrl, taskId);
        this.taskLogManager.connect();

        // Initialize diagnostic checking if needed
        if (diagnosticsSent) {
            this.diagnosticManager = new DiagnosticManager(diagnosticUrl, taskId);
            this.diagnosticManager.start();
        }

        // Save cookie state
        this.saveCookieState();
    }

    // Initialize simple SignalR connection (for TaskStatus.cshtml compatibility)
    initializeSimpleSignalR(serverUrl, taskId) {
        console.log("connecting to hub");
        Connect(serverUrl, taskId);
    }

    // Cleanup function
    cleanup() {
        console.log("Cleaning up page resources");

        if (this.taskLogManager) {
            this.taskLogManager.disconnect();
            this.taskLogManager = null;
        }

        if (this.diagnosticManager) {
            this.diagnosticManager.stop();
            this.diagnosticManager = null;
        }

        // Clean up global connection
        disconnectSignalR();
    }

    // Send diagnostic function
    async sendDiagnostic(url, taskId) {
        try {
            const response = await $.ajax({
                url: url,
                type: "GET",
                data: { id: taskId },
                cache: false,
                timeout: 30000 // 30 second timeout
            });

            $("#modal-overlay").html(response);
            if (typeof showModelDialog === 'function') {
                showModelDialog();
            }
        } catch (error) {
            console.error("Error sending diagnostic:", error);
            alert("Failed to send diagnostic. Please try again.");
        }
    }

    // Cookie management
    saveCookieState() {
        const cookie = this.getCookieValue("ClarityChildTasksView");
        if (typeof (cookie) !== 'undefined') {
            const date = new Date();
            date.setTime(date.getTime() + (10 * 60 * 1000));
            if (typeof ASPxClientUtils !== 'undefined') {
                ASPxClientUtils.SetCookie("ClarityChildTasksView", cookie, date);
            }
        }
    }

    // Helper function to get cookie value
    getCookieValue(name) {
        const value = "; " + document.cookie;
        const parts = value.split("; " + name + "=");
        if (parts.length === 2) return parts.pop().split(";").shift();
    }
}

// Global instance for easy access
const taskStatusHelper = new TaskStatusHelper();

// Global functions for backward compatibility
function sendDiagnostic(url, taskId) {
    return taskStatusHelper.sendDiagnostic(url, taskId);
}

function getCookieValue(name) {
    return taskStatusHelper.getCookieValue(name);
}

// Setup page cleanup handlers
function setupCleanupHandlers() {
    $(window).on('beforeunload', function() {
        taskStatusHelper.cleanup();
    });

    window.addEventListener('pagehide', function() {
        taskStatusHelper.cleanup();
    });
}