{% extends "base.html" %}

{% block title %}Simulation Status - 1D Poisson Simulator{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-lg-8">
        <!-- Simulation Info Card -->
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h4 class="mb-0">
                    <i class="bi bi-activity"></i> Simulation in Progress
                </h4>
            </div>
            <div class="card-body">
                <!-- Simulation Details -->
                <div class="mb-4">
                    <div class="row">
                        <div class="col-md-6">
                            <p class="mb-2">
                                <strong>Name:</strong> 
                                <span id="simName" class="text-monospace">-</span>
                            </p>
                            <p class="mb-2">
                                <strong>Mode:</strong> 
                                <span id="simMode">-</span>
                            </p>
                        </div>
                        <div class="col-md-6">
                            <p class="mb-2">
                                <strong>Started:</strong> 
                                <span id="simStartTime">-</span>
                            </p>
                            <p class="mb-2">
                                <strong>Status:</strong> 
                                <span id="simStatus" class="badge bg-info">Initializing</span>
                            </p>
                        </div>
                    </div>
                </div>

                <!-- Progress Bar -->
                <div class="mb-4">
                    <label class="form-label fw-bold">Progress:</label>
                    <div class="progress" style="height: 30px;">
                        <div class="progress-bar progress-bar-striped progress-bar-animated" 
                             id="progressBar" 
                             role="progressbar" 
                             style="width: 0%"
                             aria-valuenow="0" 
                             aria-valuemin="0" 
                             aria-valuemax="100">
                            <span id="progressText">0%</span>
                        </div>
                    </div>
                </div>

                <!-- Status Message -->
                <div class="alert alert-info" id="statusAlert">
                    <div class="d-flex align-items-center">
                        <div class="spinner-border spinner-border-sm me-3" role="status" id="statusSpinner">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                        <div id="statusMessage">
                            <i class="bi bi-hourglass-split"></i> Initializing simulation...
                        </div>
                    </div>
                </div>

                <!-- Activity Log -->
                <div class="card border-secondary">
                    <div class="card-header bg-light">
                        <h6 class="mb-0">
                            <i class="bi bi-clock-history"></i> Activity Log
                        </h6>
                    </div>
                    <div class="card-body p-0">
                        <div id="activityLog" style="max-height: 300px; overflow-y: auto; font-family: monospace; font-size: 0.85rem;">
                            <div class="p-3 text-muted">Waiting for updates...</div>
                        </div>
                    </div>
                </div>

                <!-- Action Buttons -->
                <div class="mt-4 d-flex gap-2">
                    <button class="btn btn-outline-secondary" id="backToBuilderBtn">
                        <i class="bi bi-arrow-left"></i> Back to Builder
                    </button>
                    <button class="btn btn-primary" id="viewResultsBtn" style="display: none;">
                        <i class="bi bi-graph-up"></i> View Results
                    </button>
                    <button class="btn btn-outline-danger" id="cancelSimBtn" style="display: none;">
                        <i class="bi bi-x-circle"></i> Cancel
                    </button>
                </div>
            </div>
        </div>

        <!-- Error Card (hidden by default) -->
        <div class="card border-danger mt-3" id="errorCard" style="display: none;">
            <div class="card-header bg-danger text-white">
                <h5 class="mb-0">
                    <i class="bi bi-exclamation-triangle"></i> Simulation Failed
                </h5>
            </div>
            <div class="card-body">
                <p id="errorMessage" class="mb-0"></p>
                <div class="mt-3">
                    <button class="btn btn-outline-danger" onclick="window.location.href='/builder'">
                        <i class="bi bi-arrow-left"></i> Return to Builder
                    </button>
                </div>
            </div>
        </div>

        <!-- Success Card (hidden by default) -->
        <div class="card border-success mt-3" id="successCard" style="display: none;">
            <div class="card-header bg-success text-white">
                <h5 class="mb-0">
                    <i class="bi bi-check-circle"></i> Simulation Complete!
                </h5>
            </div>
            <div class="card-body">
                <p class="mb-3">
                    Your simulation has completed successfully. 
                    You can now view the results or start a new simulation.
                </p>
                <div class="d-flex gap-2">
                    <button class="btn btn-success" onclick="window.location.href='/results'">
                        <i class="bi bi-graph-up"></i> View Results
                    </button>
                    <button class="btn btn-outline-secondary" onclick="window.location.href='/builder'">
                        <i class="bi bi-tools"></i> New Simulation
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Hidden data -->
<input type="hidden" id="simIdInput" value="">

{% endblock %}

{% block extra_css %}
<style>
    .activity-log-entry {
        padding: 8px 12px;
        border-bottom: 1px solid #e9ecef;
        font-size: 0.85rem;
    }
    
    .activity-log-entry:last-child {
        border-bottom: none;
    }
    
    .activity-log-entry .timestamp {
        color: #6c757d;
        font-size: 0.75rem;
    }
    
    .activity-log-entry.status-starting {
        background-color: #e7f3ff;
    }
    
    .activity-log-entry.status-running {
        background-color: #fff3cd;
    }
    
    .activity-log-entry.status-complete {
        background-color: #d1e7dd;
    }
    
    .activity-log-entry.status-failed {
        background-color: #f8d7da;
    }

    #progressBar {
        transition: width 0.3s ease-in-out;
        font-weight: bold;
        font-size: 1rem;
    }

    .progress-bar-animated {
        animation: progress-bar-stripes 1s linear infinite;
    }

    @keyframes pulse {
        0%, 100% { opacity: 1; }
        50% { opacity: 0.5; }
    }

    .pulse {
        animation: pulse 2s ease-in-out infinite;
    }
</style>
{% endblock %}

{% block extra_js %}
<script src="{{ url_for('static', filename='js/simulation_status.js') }}"></script>
{% endblock %}