/* eslint-disable @typescript-eslint/member-ordering */
import {
  Component,
  computed,
  inject,
  signal
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { Store } from '@ngrx/store';
import { NgIcon, provideIcons } from '@ng-icons/core';
import {
  lucideHash,
  lucideMonitor,
  lucideSettings,
  lucideUsers,
  lucideMenu,
  lucideX,
  lucideChevronLeft
} from '@ng-icons/lucide';

import { ChatMessagesComponent } from '../../../domains/chat/feature/chat-messages/chat-messages.component';
import { RoomsSidePanelComponent } from '../rooms-side-panel/rooms-side-panel.component';
import { VoiceWorkspaceComponent } from '../voice-workspace/voice-workspace.component';

import { selectCurrentRoom, selectTextChannels } from '../../../store/rooms/rooms.selectors';
import { SettingsModalService } from '../../../core/services/settings-modal.service';
import { selectIsCurrentUserAdmin } from '../../../store/users/users.selectors';
import { VoiceWorkspaceService } from '../../../domains/voice-session';
import { ThemeNodeDirective, ThemeService } from '../../../domains/theme';

@Component({
  selector: 'app-chat-room',
  standalone: true,
  imports: [
    CommonModule,
    NgIcon,
    ChatMessagesComponent,
    VoiceWorkspaceComponent,
    RoomsSidePanelComponent,
    ThemeNodeDirective
  ],
  viewProviders: [
    provideIcons({
      lucideHash,
      lucideMonitor,
      lucideSettings,
      lucideUsers,
      lucideMenu,
      lucideX,
      lucideChevronLeft
    })
  ],
  templateUrl: './chat-room.component.html'
})
/**
 * Main chat room view combining the messages panel, side panels, and admin controls.
 */
export class ChatRoomComponent {
  private readonly store = inject(Store);
  private readonly settingsModal = inject(SettingsModalService);
  private readonly theme = inject(ThemeService);
  private voiceWorkspace = inject(VoiceWorkspaceService);
  showMenu = signal(false);
  showAdminPanel = signal(false);

  currentRoom = this.store.selectSignal(selectCurrentRoom);
  isAdmin = this.store.selectSignal(selectIsCurrentUserAdmin);
  textChannels = this.store.selectSignal(selectTextChannels);
  isVoiceWorkspaceExpanded = this.voiceWorkspace.isExpanded;
  hasTextChannels = computed(() => this.textChannels().length > 0);
  roomLayoutStyles = computed(() => this.theme.getLayoutContainerStyles('roomLayout'));
  channelsPanelLayoutStyles = computed(() => this.theme.getLayoutItemStyles('chatRoomChannelsPanel'));
  mainPanelLayoutStyles = computed(() => this.theme.getLayoutItemStyles('chatRoomMainPanel'));
  membersPanelLayoutStyles = computed(() => this.theme.getLayoutItemStyles('chatRoomMembersPanel'));

  /** Open the settings modal to the Server admin page for the current room. */
  toggleAdminPanel() {
    const room = this.currentRoom();

    if (room) {
      this.settingsModal.open('server', room.id);
    }
  }
}
