// Listen for status updates from background script
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if (message.type === 'status' && message.stats) {
    var s = message.stats;
    document.getElementById('status').textContent = message.active ? 'Running' : 'Inactive';
    document.getElementById('indicator').className = 'indicator ' + (message.active ? 'active' : 'inactive');
    document.getElementById('activeConnections').textContent = s.activeConnections;
    document.getElementById('totalConnections').textContent = s.totalConnections;
    document.getElementById('bytesReceived').textContent = s.bytesReceived;
    document.getElementById('bytesSent').textContent = s.bytesSent;
    document.getElementById('uptime').textContent = s.uptime;

    // Show connection durations
    var list = document.getElementById('connectionsList');
    if (s.connectionDurations && s.connectionDurations.length > 0) {
      list.style.display = 'block';
      list.innerHTML = '<div style="font-size: 11px; opacity: 0.7; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.5px;">Active Connections</div>';
      s.connectionDurations.forEach(function(duration) {
        var div = document.createElement('div');
        div.className = 'connection-item';
        div.textContent = 'Connection ' + (s.connectionDurations.indexOf(duration) + 1) + ': ' + duration;
        list.appendChild(div);
      });
    } else {
      list.style.display = 'none';
    }
  } else if (message.type === 'heartbeat') {
    var now = new Date();
    document.getElementById('heartbeat').textContent = 'Last heartbeat: ' + now.toLocaleTimeString();
  }
});

// Request initial status
chrome.runtime.sendMessage({ type: 'getStatus' });
