{% extends "base.html" %}

{% block title %}Terminal - {{ client_id }}{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h3 class="card-title mb-0">
                    <i class="fas fa-terminal"></i> SSH Terminal - {{ client_id }}
                </h3>
                <div>
                    <input type="text" id="sshUsername" class="form-control form-control-sm d-inline-block w-auto me-2" placeholder="Username" value="root">
                    <button id="connectBtn" class="btn btn-success btn-sm">
                        <i class="fas fa-play"></i> Connect
                    </button>
                    <button id="disconnectBtn" class="btn btn-danger btn-sm" disabled>
                        <i class="fas fa-stop"></i> Disconnect
                    </button>
                </div>
            </div>
            <div class="card-body p-0">
                <div id="terminal" class="terminal-container">
                    <div id="output"></div>
                    <div class="input-group">
                        <span class="text-success me-2">$</span>
                        <input type="text" id="commandInput" class="terminal-input" placeholder="Type your command..." disabled>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
let ws = null;
let connected = false;

document.getElementById('connectBtn').addEventListener('click', connect);
document.getElementById('disconnectBtn').addEventListener('click', disconnect);
document.getElementById('commandInput').addEventListener('keypress', function(e) {
    if (e.key === 'Enter') {
        sendCommand();
    }
});

function connect() {
    const username = document.getElementById('sshUsername').value;
    if (!username) {
        alert('Please enter a username');
        return;
    }

    // For demo purposes, we'll simulate the connection
    // In a real implementation, this would use WebSocket to communicate with wsssh
    connected = true;
    document.getElementById('connectBtn').disabled = true;
    document.getElementById('disconnectBtn').disabled = false;
    document.getElementById('commandInput').disabled = false;
    document.getElementById('sshUsername').disabled = true;

    appendOutput(`Connecting to ${username}@{{ client_id }}...`);
    setTimeout(() => {
        appendOutput(`Connected successfully!`);
        appendOutput(`Welcome to {{ client_id }}`);
        appendOutput(`$ `);
    }, 1000);
}

function disconnect() {
    connected = false;
    document.getElementById('connectBtn').disabled = false;
    document.getElementById('disconnectBtn').disabled = true;
    document.getElementById('commandInput').disabled = true;
    document.getElementById('sshUsername').disabled = false;

    appendOutput('Disconnected.');
}

function sendCommand() {
    if (!connected) return;

    const input = document.getElementById('commandInput');
    const command = input.value.trim();
    if (!command) return;

    appendOutput(`$ ${command}`);

    // Simulate command execution
    setTimeout(() => {
        if (command === 'ls') {
            appendOutput(`Desktop  Documents  Downloads  Music  Pictures  Videos`);
        } else if (command === 'pwd') {
            appendOutput(`/home/${document.getElementById('sshUsername').value}`);
        } else if (command === 'whoami') {
            appendOutput(document.getElementById('sshUsername').value);
        } else if (command === 'exit' || command === 'logout') {
            disconnect();
            return;
        } else {
            appendOutput(`Command not found: ${command}`);
        }
        appendOutput(`$ `);
    }, 500);

    input.value = '';
}

function appendOutput(text) {
    const output = document.getElementById('output');
    const line = document.createElement('div');
    line.textContent = text;
    output.appendChild(line);
    output.scrollTop = output.scrollHeight;
}

// Focus on command input when connected
document.addEventListener('keydown', function(e) {
    if (connected && e.target.tagName !== 'INPUT') {
        document.getElementById('commandInput').focus();
    }
});
</script>
{% endblock %}