<template>
  <div class="connection-tab">
    <div class="controls">
      <div class="input-group">
        <label for="wsUrl">WebSocket URL</label>
        <input
          id="wsUrl"
          v-model="config.wsUrl"
          type="text"
          placeholder="ws://localhost:8080/api/v1/ws"
        />
      </div>

      <div class="input-group">
        <label for="userId">User ID</label>
        <input
          id="userId"
          v-model="config.userId"
          type="text"
          placeholder="anonymous"
        />
      </div>

      <div class="input-group">
        <label for="room">Room</label>
        <input
          id="room"
          v-model="config.room"
          type="text"
          placeholder="default"
        />
      </div>

      <div class="input-group">
        <label for="staticId">Static ID (Optional)</label>
        <input
          id="staticId"
          v-model="config.staticId"
          type="text"
          placeholder="Leave empty for dynamic ID"
        />
      </div>
    </div>

    <div class="controls">
      <div class="input-group">
        <label class="checkbox-group">
          <input v-model="config.useIPBasedId" type="checkbox" />
          Use IP-based ID
        </label>
      </div>

      <div class="input-group">
        <label class="checkbox-group">
          <input v-model="config.autoReconnect" type="checkbox" />
          Auto Reconnect
        </label>
      </div>

      <div class="input-group">
        <label class="checkbox-group">
          <input v-model="config.heartbeatEnabled" type="checkbox" />
          Enable Heartbeat
        </label>
      </div>
    </div>

    <div class="controls">
      <button
        class="btn btn-primary"
        :class="{ 'btn-disabled': isConnecting }"
        :disabled="isConnecting"
        @click="connect"
      >
        <span v-if="isConnecting" class="loading-indicator"></span>
        {{ isConnected ? "Reconnect" : "Connect" }}
      </button>

      <button
        class="btn btn-danger"
        :disabled="!isConnected"
        @click="disconnect"
      >
        Disconnect
      </button>

      <button
        class="btn btn-info"
        :disabled="!isConnected"
        @click="testConnection"
      >
        Test Connection
      </button>

      <button class="btn btn-secondary" @click="clearMessages">
        Clear Messages
      </button>
    </div>

    <div class="controls">
      <div class="input-group">
        <label for="maxReconnectAttempts">Max Reconnect Attempts</label>
        <input
          id="maxReconnectAttempts"
          v-model.number="config.maxReconnectAttempts"
          type="number"
          min="1"
          max="50"
        />
      </div>

      <div class="input-group">
        <label for="reconnectDelay">Reconnect Delay (ms)</label>
        <input
          id="reconnectDelay"
          v-model.number="config.reconnectDelay"
          type="number"
          min="100"
          max="10000"
        />
      </div>

      <div class="input-group">
        <label for="heartbeatInterval">Heartbeat Interval (ms)</label>
        <input
          id="heartbeatInterval"
          v-model.number="config.heartbeatInterval"
          type="number"
          min="1000"
          max="60000"
        />
      </div>

      <div class="input-group">
        <label for="maxMessages">Max Messages</label>
        <input
          id="maxMessages"
          v-model.number="config.maxMessages"
          type="number"
          min="100"
          max="5000"
        />
      </div>
    </div>

    <div class="connection-info" v-if="isConnected">
      <h3>Connection Information</h3>
      <div class="info-grid">
        <div class="info-item">
          <strong>Client ID:</strong>
          <span>{{ connectionState.clientId }}</span>
        </div>
        <div class="info-item">
          <strong>Static ID:</strong>
          <span>{{ connectionState.staticId || "N/A" }}</span>
        </div>
        <div class="info-item">
          <strong>IP Address:</strong>
          <span>{{ connectionState.ipAddress || "N/A" }}</span>
        </div>
        <div class="info-item">
          <strong>User ID:</strong>
          <span>{{ connectionState.userId }}</span>
        </div>
        <div class="info-item">
          <strong>Room:</strong>
          <span>{{ connectionState.currentRoom }}</span>
        </div>
        <div class="info-item">
          <strong>Connected At:</strong>
          <span>{{
            new Date(connectionState.connectionStartTime || 0).toLocaleString()
          }}</span>
        </div>
        <div class="info-item">
          <strong>Latency:</strong>
          <span>{{ connectionState.connectionLatency }}ms</span>
        </div>
        <div class="info-item">
          <strong>Reconnect Attempts:</strong>
          <span>{{ connectionState.reconnectAttempts }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { useWebSocket } from "../../composables/useWebSocket";

const {
  isConnected,
  isConnecting,
  connectionState,
  config,
  connect,
  disconnect,
  testConnection,
  clearMessages,
} = useWebSocket();
</script>

<style scoped>
.connection-tab {
  padding: 20px 0;
}

.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
  padding: 24px;
  background: #f8f9fa;
  border-radius: 8px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.input-group label {
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.input-group input {
  padding: 12px 16px;
  border: 2px solid #e9ecef;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.3s ease;
}

.input-group input:focus {
  outline: none;
  border-color: #1976d2;
  box-shadow: 0 0 0 3px rgba(25, 118, 210, 0.1);
}

.checkbox-group {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0;
}

.checkbox-group input[type="checkbox"] {
  margin: 0;
  width: 18px;
  height: 18px;
}

.btn {
  padding: 12px 24px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.btn-primary {
  background: #1976d2;
  color: white;
}

.btn-primary:hover:not(.btn-disabled) {
  background: #1565c0;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(25, 118, 210, 0.3);
}

.btn-danger {
  background: #dc3545;
  color: white;
}

.btn-danger:hover:not(:disabled) {
  background: #c82333;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(220, 53, 69, 0.3);
}

.btn-info {
  background: #17a2b8;
  color: white;
}

.btn-info:hover:not(:disabled) {
  background: #138496;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(23, 162, 184, 0.3);
}

.btn-secondary {
  background: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background: #5a6268;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(108, 117, 125, 0.3);
}

.btn-disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.loading-indicator {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid #f3f3f3;
  border-top: 2px solid #1976d2;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-right: 8px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.connection-info {
  margin-top: 32px;
  padding: 24px;
  background: white;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.connection-info h3 {
  margin-top: 0;
  margin-bottom: 20px;
  color: #333;
  font-size: 18px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: #f8f9fa;
  border-radius: 6px;
  border: 1px solid #e9ecef;
}

.info-item strong {
  color: #333;
  font-weight: 600;
}

.info-item span {
  color: #666;
  font-family: monospace;
  font-size: 13px;
}

/* Responsive Design */
@media (max-width: 768px) {
  .controls {
    grid-template-columns: 1fr;
    padding: 16px;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }
}
</style>
