import {AfterViewInit, Component, inject, OnDestroy, OnInit, signal} from '@angular/core';
import {Subscription} from 'rxjs';
import {ChatService} from '@pages/altitude/chat/service/chat.service';
import {ChatChannel} from '@pages/altitude/chat/objects/chat-channel.object';
import {HeaderComponent} from '@header/header.component';
import {MiniMessageInteractionService} from '@pages/altitude/chat/mini-message/mini-message-interaction.service';
import {FullSizeComponent} from '@shared-components/full-size/full-size.component';
import {ChatBoxComponent} from '@pages/altitude/chat/components/chat/chat-box.component';
import {ChannelListComponent} from '@pages/altitude/chat/components/channel-list/channel-list.component';
import {ChatInfoComponent} from '@pages/altitude/chat/components/chat-info/chat-info.component';
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';

@Component({
  selector: 'app-chat',
  imports: [
    HeaderComponent,
    FullSizeComponent,
    ChatBoxComponent,
    ChannelListComponent,
    ChatInfoComponent
  ],
  templateUrl: './chat.component.html',
  styleUrl: './chat.component.scss',
  providers: [MiniMessageInteractionService]
})
export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
  private sub?: Subscription;
  private readonly chatService: ChatService = inject(ChatService)
  protected readonly messages = this.chatService.messages;
  protected readonly channels = this.chatService.channels;
  protected readonly selectedChannel = this.chatService.selectedChannel;
  protected readonly showFullscreenPrompt = signal(false);
  protected readonly isMobile = signal(false);

  constructor(private interaction: MiniMessageInteractionService, private breakpointObserver: BreakpointObserver) {
    interaction.clicks$.subscribe(({action, value}) => {
      if (action === 'run_command') {
        alert(value);
      }
    });
    this.breakpointObserver
      .observe([Breakpoints.Handset])
      .subscribe(result => {
        this.isMobile.set(result.matches);
        this.showFullscreenPrompt.set(this.isMobile());
      });
  }

  public onSelectedChannelChange(channel: ChatChannel) {
    this.chatService.selectChannel(channel);
  }

  protected enterFullscreen(): void {
    this.showFullscreenPrompt.set(false);

    if (!document.fullscreenElement) {
      document.documentElement.requestFullscreen().catch(() => {
        // Ignore fullscreen request failures.
      });
    }
  }

  protected skipFullscreen(): void {
    this.showFullscreenPrompt.set(false);
  }


  ngOnInit(): void {
    this.chatService.connect();
  }

  ngAfterViewInit(): void {
    document.documentElement.requestFullscreen().catch(() => {
      console.error('Failed to enter fullscreen');
    });
  }

  ngOnDestroy(): void {
    this.sub?.unsubscribe();
    this.chatService.disconnect();

    if (document.fullscreenElement) {
      document.exitFullscreen().catch(() => {
        console.error('Failed to exit fullscreen');
      });
    }
  }
}
