// Diagnostic management for task status monitoring
class DiagnosticManager {
    constructor(diagnosticUrl, taskId) {
        this.diagnosticUrl = diagnosticUrl;
        this.taskId = taskId;
        this.intervalId = null;
        this.timeWaiting = 0;
        this.interval = 5000; // 5 seconds
        this.timeout = 60000; // 60 seconds
    }

    start() {
        if (this.intervalId) {
            return; // Already running
        }

        this.timeWaiting = 0;
        this.intervalId = setInterval(() => {
            this.checkDiagnosticStatus();
            this.timeWaiting += this.interval;
        }, this.interval);
    }

    stop() {
        if (this.intervalId) {
            clearInterval(this.intervalId);
            this.intervalId = null;
            this.timeWaiting = 0;
        }
    }

    async checkDiagnosticStatus() {
        try {
            const response = await $.ajax({
                url: this.diagnosticUrl,
                type: "GET",
                data: { id: this.taskId },
                cache: false,
                timeout: 10000 // 10 second timeout
            });

            if (response.Processed) {
                this.showSuccess();
                this.stop();
            } else if (this.timeWaiting >= this.timeout) {
                this.showTimeout();
                this.stop();
            }
        } catch (error) {
            console.error("Error checking diagnostic status:", error);
            this.showError();
            this.stop();
        }
    }

    showSuccess() {
        $("#pending-diagnostic-confirmation").fadeOut("fast", () => {
            $("#success-diagnostic-confirmation").fadeIn("fast");
        });
    }

    showTimeout() {
        $("#pending-diagnostic-confirmation").fadeOut("fast", () => {
            $("#failed-diagnostic-confirmation").fadeIn("fast");
        });
    }

    showError() {
        $("#pending-diagnostic-confirmation").fadeOut("fast", () => {
            $("#failed-diagnostic-confirmation").fadeIn("fast");
        });
    }
}

// Legacy function for backward compatibility with existing TaskStatus.cshtml
function isDiagnosticProcessed(diagnosticUrl, taskId) {
    $.ajax({
        url: diagnosticUrl,
        type: "GET",
        data: { id: taskId },
        cache: false,
        success: function (result) {
            if (result.Processed) {
                $("#pending-diagnostic-confirmation").fadeOut("fast", function () {
                    $("#success-diagnostic-confirmation").fadeIn("fast");
                    if (typeof refreshIntervalId !== 'undefined') {
                        clearInterval(refreshIntervalId);
                    }
                });
            } else if (typeof timeWaitingForResponse !== 'undefined' && timeWaitingForResponse > 60000) {
                $("#pending-diagnostic-confirmation").fadeOut("fast", function () {
                    $("#failed-diagnostic-confirmation").fadeIn("fast");
                    if (typeof refreshIntervalId !== 'undefined') {
                        clearInterval(refreshIntervalId);
                    }
                });
            }
        },
        error: function (response) {
            $("#pending-diagnostic-confirmation").fadeOut("fast", function () {
                $("#failed-diagnostic-confirmation").fadeIn("fast");
                if (typeof refreshIntervalId !== 'undefined') {
                    clearInterval(refreshIntervalId);
                }
            });
        }
    });
}