<script lang="ts">
  import { currentServer, channels, currentChannel, textChannels, voiceChannels, currentUser, voiceState, refreshChannels, userProfile, refreshProfile, updateDisplayName, unreadCounts } from '../lib/store';
  import { connectToVoice, disconnectFromVoice, toggleMute, toggleDeafen, voiceRoom } from '../lib/voice';
  import { onMount } from 'svelte';
  import { getJoinedRooms, createRoom, joinRoom, leaveRoom, logout, setPresence, type RoomInfo } from '../lib/api';

  let showCreateRoom = false;
  let showJoinRoom = false;
  let newRoomName = '';
  let newRoomTopic = '';
  let joinRoomInput = '';
  let createError = '';
  let joinError = '';
  let showSettings = false;
  let presenceStatus: 'online' | 'idle' | 'dnd' | 'offline' = 'online';
  let editingName = false;
  let editNameValue = '';

  onMount(async () => {
    try {
      const rooms: RoomInfo[] = await getJoinedRooms();
      channels.set(rooms.map((r: RoomInfo) => ({
        id: r.room_id,
        name: r.name || r.room_id,
        type: 'text' as const,
        topic: r.topic || null,
        parentId: null,
      })));
    } catch (e) {
      console.error('Failed to load rooms', e);
    }
    refreshProfile();
  });

  async function handleCreateRoom() {
    createError = '';
    try {
      await createRoom(newRoomName, newRoomTopic || undefined);
      showCreateRoom = false;
      newRoomName = '';
      newRoomTopic = '';
      await refreshChannels();
    } catch (e: any) {
      createError = e.toString();
    }
  }

  async function handleJoinRoom() {
    joinError = '';
    try {
      await joinRoom(joinRoomInput);
      showJoinRoom = false;
      joinRoomInput = '';
      await refreshChannels();
    } catch (e: any) {
      joinError = e.toString();
    }
  }

  async function handleJoinVoice(channel: any) {
    try {
      await connectToVoice(channel.id);
      voiceState.update(s => ({
        ...s,
        channelId: channel.id,
      }));
    } catch (e) {
      console.error('Failed to join voice', e);
    }
  }

  async function handleLeaveRoom(roomId: string) {
    try {
      await leaveRoom(roomId);
      await refreshChannels();
      if ($currentChannel?.id === roomId) {
        currentChannel.set(null);
      }
    } catch (e) {
      console.error('Failed to leave room', e);
    }
  }

  async function handleLogout() {
    try {
      await logout();
      window.location.reload();
    } catch (e) {
      console.error('Failed to logout', e);
    }
  }

  async function handleSaveName() {
    if (!editNameValue.trim()) return;
    try {
      await updateDisplayName(editNameValue.trim());
      editingName = false;
    } catch (e) {
      console.error('Failed to update name', e);
    }
  }

  function startEditName() {
    editNameValue = $userProfile?.display_name || $currentUser?.username || '';
    editingName = true;
  }

  async function handlePresenceChange(status: string) {
    try {
      await setPresence(status);
      presenceStatus = status as any;
    } catch (e) {
      console.error('Failed to set presence', e);
    }
  }

  function extractUsername(userId: string): string {
    return userId.split(':')[0].replace('@', '');
  }
</script>

<div class="channel-sidebar">
  <div class="server-header">
    <span class="server-name">{$currentServer?.name || 'EifelDC'}</span>
    <button class="btn-icon" on:click={() => showSettings = !showSettings}>
      <svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M7 10l5 5 5-5z"/></svg>
    </button>
  </div>

  {#if showSettings}
    <div class="settings-popup">
      <div class="settings-section">
        <div class="settings-label">Status</div>
        <button class="status-option" on:click={() => handlePresenceChange('online')}>
          <span class="status-indicator" style="background: var(--online)"></span> Online
        </button>
        <button class="status-option" on:click={() => handlePresenceChange('away')}>
          <span class="status-indicator" style="background: var(--idle)"></span> Abwesend
        </button>
        <button class="status-option" on:click={() => handlePresenceChange('unavailable')}>
          <span class="status-indicator" style="background: var(--dnd)"></span> Nicht stören
        </button>
      </div>
      <hr />
      <button class="settings-option" on:click={handleLogout}>Abmelden</button>
    </div>
  {/if}

  <div class="channel-list">
    <div class="channel-category">
      <div class="category-header" on:click={() => showCreateRoom = !showCreateRoom}>
        <svg viewBox="0 0 24 24" width="12" height="12" fill="currentColor"><path d="M7 10l5 5 5-5z"/></svg>
        <span>Textkanäle</span>
        <span class="category-add" on:click|stopPropagation={() => { showCreateRoom = true; showJoinRoom = false; }}>+</span>
      </div>
      {#each $textChannels as channel}
        <button
          class="channel-item"
          class:active={$currentChannel?.id === channel.id}
          on:click={() => { currentChannel.set(channel); unreadCounts.update(c => { delete c[channel.id]; return c; }); }}
          on:contextmenu|preventDefault={() => handleLeaveRoom(channel.id)}
        >
          <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor" class="channel-icon">
            <path d="M5.88657 21C5.57547 21 5.3399 20.7189 5.39427 20.4126L6.00001 17H2.59511C2.28449 17 2.04905 16.7198 2.10259 16.4138L2.27759 15.4138C2.31946 15.1746 2.52722 15 2.77011 15H6.35001L7.41001 9H4.00511C3.69449 9 3.45905 8.71977 3.51259 8.41381L3.68759 7.41381C3.72946 7.17456 3.93722 7 4.18011 7H7.76001L8.39677 3.41262C8.43914 3.17391 8.64664 3 8.88907 3H9.89044C10.2015 3 10.4371 3.28107 10.3827 3.58738L9.76001 7H15.76L16.3968 3.41262C16.4391 3.17391 16.6466 3 16.8891 3H17.8904C18.2015 3 18.4371 3.28107 18.3827 3.58738L17.76 7H21.1649C21.4755 7 21.711 7.28023 21.6574 7.58619L21.4824 8.58619C21.4406 8.82544 21.2328 9 20.9899 9H17.41L16.35 15H19.755C20.0656 15 20.301 15.2802 20.2474 15.5862L20.0724 16.5862C20.0306 16.8254 19.8228 17 19.5799 17H16L15.3632 20.5874C15.3209 20.8261 15.1134 21 14.8709 21H13.8696C13.5585 21 13.3229 20.7189 13.3773 20.4126L14 17H8.00001L7.36325 20.5874C7.32088 20.8261 7.11337 21 6.87094 21H5.88657ZM9.41045 9L8.35045 15H14.3504L15.4104 9H9.41045Z"/>
          </svg>
          <span>{channel.name}</span>
          {#if $unreadCounts[channel.id]}
            <span class="unread-badge">{$unreadCounts[channel.id]}</span>
          {/if}
        </button>
      {/each}
    </div>

    <div class="channel-category">
      <div class="category-header">
        <svg viewBox="0 0 24 24" width="12" height="12" fill="currentColor"><path d="M7 10l5 5 5-5z"/></svg>
        <span>Sprachkanäle</span>
      </div>
      {#each $voiceChannels as channel}
        <button
          class="channel-item voice"
          class:active={$voiceRoom.roomId === channel.id}
          on:click={() => {
            if ($voiceRoom.roomId === channel.id) {
              disconnectFromVoice();
            } else {
              handleJoinVoice(channel);
            }
          }}
        >
          <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor" class="channel-icon">
            <path d="M12 3c-4.97 0-9 4.03-9 9v7c0 1.66 1.34 3 3 3h3v-8H5v-2c0-3.87 3.13-7 7-7s7 3.13 7 7v2h-4v8h3c1.66 0 3-1.34 3-3v-7c0-4.97-4.03-9-9-9z"/>
          </svg>
          <span>{channel.name}</span>
        </button>
      {/each}
    </div>

    <button class="add-channel-btn" on:click={() => { showJoinRoom = true; showCreateRoom = false; }}>
      <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm5 11h-4v4h-2v-4H7v-2h4V7h2v4h4v2z"/></svg>
      Raum beitreten
    </button>
  </div>

  {#if showCreateRoom}
    <div class="modal-overlay" on:click={() => showCreateRoom = false}>
      <div class="modal" on:click|stopPropagation>
        <h3>Raum erstellen</h3>
        {#if createError}
          <div class="modal-error">{createError}</div>
        {/if}
        <div class="modal-field">
          <label>Name</label>
          <input type="text" bind:value={newRoomName} placeholder="Raumname" />
        </div>
        <div class="modal-field">
          <label>Thema (optional)</label>
          <input type="text" bind:value={newRoomTopic} placeholder="Thema" />
        </div>
        <div class="modal-actions">
          <button class="btn-secondary" on:click={() => showCreateRoom = false}>Abbrechen</button>
          <button class="btn-primary" on:click={handleCreateRoom} disabled={!newRoomName}>Erstellen</button>
        </div>
      </div>
    </div>
  {/if}

  {#if showJoinRoom}
    <div class="modal-overlay" on:click={() => showJoinRoom = false}>
      <div class="modal" on:click|stopPropagation>
        <h3>Raum beitreten</h3>
        {#if joinError}
          <div class="modal-error">{joinError}</div>
        {/if}
        <div class="modal-field">
          <label>Raum-ID oder Alias</label>
          <input type="text" bind:value={joinRoomInput} placeholder="#raum:matrix.org" />
        </div>
        <div class="modal-actions">
          <button class="btn-secondary" on:click={() => showJoinRoom = false}>Abbrechen</button>
          <button class="btn-primary" on:click={handleJoinRoom} disabled={!joinRoomInput}>Beitreten</button>
        </div>
      </div>
    </div>
  {/if}

  <div class="user-panel">
    <div class="user-info">
      <div class="avatar-small">{$userProfile?.display_name?.charAt(0).toUpperCase() || $currentUser?.username?.charAt(0).toUpperCase() || '?'}</div>
      <div class="user-details">
        {#if editingName}
          <input
            type="text"
            bind:value={editNameValue}
            placeholder="Anzeigename"
            class="edit-name-input"
            on:keydown={(e) => { if (e.key === 'Enter') handleSaveName(); if (e.key === 'Escape') editingName = false; }}
          />
          <div class="edit-name-actions">
            <button class="btn-icon-small" on:click={handleSaveName}>✓</button>
            <button class="btn-icon-small" on:click={() => editingName = false}>✕</button>
          </div>
        {:else}
          <span class="username" on:click={startEditName} title="Klicken zum Bearbeiten">{$userProfile?.display_name || $currentUser?.username || 'Benutzer'}</span>
          <span class="status-text">{presenceStatus === 'online' ? 'Online' : presenceStatus === 'idle' ? 'Abwesend' : presenceStatus === 'dnd' ? 'Nicht stören' : 'Offline'}</span>
        {/if}
      </div>
    </div>
    <div class="user-actions">
      <button class="btn-icon" title="Mikrofon" on:click={() => { if ($voiceRoom.connected) toggleMute(); }}>
        <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M12 14c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3zm-1-9c0-.55.45-1 1-1s1 .45 1 1v6c0 .55-.45 1-1 1s-1-.45-1-1V5z"/><path d="M17 11c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08c3.39-.49 6-3.39 6-6.92h-2z"/></svg>
      </button>
      <button class="btn-icon" title="Kopfhörer" on:click={() => { if ($voiceRoom.connected) toggleDeafen(); }}>
        <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M12 3c-4.97 0-9 4.03-9 9v7c0 1.66 1.34 3 3 3h3v-8H5v-2c0-3.87 3.13-7 7-7s7 3.13 7 7v2h-4v8h3c1.66 0 3-1.34 3-3v-7c0-4.97-4.03-9-9-9z"/></svg>
      </button>
      <button class="btn-icon" title="Einstellungen" on:click={() => showSettings = !showSettings}>⚙</button>
    </div>
  </div>
</div>

<style>
  .channel-sidebar {
    width: 240px;
    min-width: 240px;
    background: var(--bg-secondary);
    display: flex;
    flex-direction: column;
    position: relative;
  }

  .server-header {
    height: 48px;
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--bg-primary);
    cursor: pointer;
  }

  .server-name {
    font-weight: 600;
    font-size: 0.95rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .channel-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
  }

  .channel-category {
    margin-bottom: 8px;
  }

  .category-header {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 16px;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text-secondary);
    cursor: pointer;
  }

  .category-header:hover {
    color: var(--text-primary);
  }

  .category-add {
    margin-left: auto;
    font-size: 1rem;
    line-height: 1;
  }

  .category-add:hover {
    color: var(--text-primary);
  }

  .channel-item {
    display: flex;
    align-items: center;
    gap: 6px;
    width: 100%;
    padding: 6px 16px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.9rem;
  }

  .channel-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-radius: var(--radius-sm);
  }

  .channel-item.active {
    background-color: var(--bg-active);
    color: var(--text-primary);
  }

  .unread-badge {
    background: var(--danger);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
    border-radius: 8px;
    padding: 1px 6px;
    margin-left: auto;
    min-width: 16px;
    text-align: center;
  }

  .channel-icon {
    flex-shrink: 0;
  }

  .add-channel-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    width: 100%;
    padding: 8px 16px;
    border: none;
    background: transparent;
    color: var(--success);
    cursor: pointer;
    font-size: 0.85rem;
  }

  .add-channel-btn:hover {
    background: var(--bg-hover);
  }

  .user-panel {
    height: 52px;
    padding: 0 8px;
    background: var(--bg-primary);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
  }

  .avatar-small {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--bg-tertiary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    flex-shrink: 0;
  }

  .user-details {
    display: flex;
    flex-direction: column;
    min-width: 0;
  }

  .username {
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
  }

  .username:hover {
    text-decoration: underline;
  }

  .edit-name-input {
    width: 100%;
    padding: 2px 6px;
    background: var(--bg-input);
    border: 1px solid var(--accent);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.9rem;
    outline: none;
  }

  .edit-name-actions {
    display: flex;
    gap: 4px;
    margin-top: 2px;
  }

  .btn-icon-small {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-secondary);
    cursor: pointer;
    padding: 1px 4px;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
  }

  .btn-icon-small:hover {
    color: var(--text-primary);
    border-color: var(--text-secondary);
  }

  .status-text {
    font-size: 0.7rem;
    color: var(--text-secondary);
  }

  .user-actions {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
  }

  .btn-icon {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .btn-icon:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
  }

  .settings-popup {
    position: absolute;
    bottom: 52px;
    left: 8px;
    right: 8px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-md);
    padding: 8px;
    z-index: 10;
    box-shadow: 0 4px 12px rgba(0,0,0,0.5);
  }

  .settings-section {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .settings-label {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text-secondary);
    padding: 4px 8px;
  }

  .status-option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border: none;
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
  }

  .status-option:hover {
    background: var(--bg-hover);
  }

  .status-indicator {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    display: inline-block;
  }

  .settings-option {
    width: 100%;
    padding: 6px 8px;
    border: none;
    background: transparent;
    color: var(--danger);
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    text-align: left;
  }

  .settings-option:hover {
    background: var(--bg-hover);
  }

  .modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
  }

  .modal {
    background: var(--bg-tertiary);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 400px;
    max-width: 90vw;
  }

  .modal h3 {
    margin-bottom: 16px;
  }

  .modal-field {
    margin-bottom: 12px;
  }

  .modal-field label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    margin-bottom: 4px;
  }

  .modal-field input {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.95rem;
    outline: none;
  }

  .modal-field input:focus {
    border-color: var(--accent);
  }

  .modal-error {
    background: rgba(237, 66, 69, 0.15);
    color: var(--danger);
    padding: 8px;
    border-radius: var(--radius-sm);
    margin-bottom: 12px;
    font-size: 0.85rem;
  }

  .modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 16px;
  }

  .btn-primary {
    padding: 8px 16px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 600;
  }

  .btn-primary:hover {
    background: var(--accent-hover);
  }

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

  .btn-secondary {
    padding: 8px 16px;
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
  }

  .btn-secondary:hover {
    color: var(--text-primary);
    border-color: var(--text-secondary);
  }

  hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 8px 0;
  }
</style>