/* eslint-disable @typescript-eslint/member-ordering */
import { CommonModule } from '@angular/common';
import {
  Component,
  ElementRef,
  effect,
  HostListener,
  inject,
  input,
  OnDestroy,
  output,
  signal,
  viewChild
} from '@angular/core';
import { NgIcon, provideIcons } from '@ng-icons/core';
import {
  lucideMaximize,
  lucideMinimize,
  lucideMonitor,
  lucideRotateCw,
  lucideVideo,
  lucideVolume2,
  lucideVolumeX
} from '@ng-icons/lucide';

import { UserAvatarComponent } from '../../../../shared';
import { ViewportService } from '../../../../core/platform';
import { VoiceWorkspacePlaybackService } from '../voice-workspace-playback.service';
import { VoiceWorkspaceStreamItem } from '../voice-workspace.models';

@Component({
  selector: 'app-voice-workspace-stream-tile',
  standalone: true,
  imports: [
    CommonModule,
    NgIcon,
    UserAvatarComponent
  ],
  viewProviders: [
    provideIcons({
      lucideMaximize,
      lucideMinimize,
      lucideMonitor,
      lucideRotateCw,
      lucideVideo,
      lucideVolume2,
      lucideVolumeX
    })
  ],
  templateUrl: './voice-workspace-stream-tile.component.html',
  host: {
    class: 'block h-full'
  }
})
export class VoiceWorkspaceStreamTileComponent implements OnDestroy {
  private readonly workspacePlayback = inject(VoiceWorkspacePlaybackService);
  private readonly viewport = inject(ViewportService);
  private fullscreenHeaderHideTimeoutId: ReturnType<typeof setTimeout> | null = null;

  readonly item = input.required<VoiceWorkspaceStreamItem>();
  readonly focused = input(false);
  readonly featured = input(false);
  readonly compact = input(false);
  readonly mini = input(false);
  readonly immersive = input(false);
  readonly focusRequested = output<string>();
  readonly tileRef = viewChild<ElementRef<HTMLElement>>('tileRoot');
  readonly videoRef = viewChild<ElementRef<HTMLVideoElement>>('streamVideo');

  readonly isFullscreen = signal(false);
  readonly isMobile = this.viewport.isMobile;
  readonly showFullscreenHeader = signal(true);
  readonly volume = signal(100);
  readonly muted = signal(false);

  constructor() {
    effect(() => {
      const ref = this.videoRef();
      const item = this.item();

      if (!ref) {
        return;
      }

      const video = ref.nativeElement;

      if (video.srcObject !== item.stream) {
        video.srcObject = item.stream;
      }

      void video.play().catch(() => {});
    });

    effect(() => {
      this.workspacePlayback.settings();

      const item = this.item();

      if (item.isLocal || !item.hasAudio) {
        this.volume.set(0);
        this.muted.set(false);
        return;
      }

      this.volume.set(this.workspacePlayback.getUserVolume(item.peerKey));
      this.muted.set(this.workspacePlayback.isUserMuted(item.peerKey));
    });

    effect(() => {
      const ref = this.videoRef();
      const item = this.item();
      const muted = this.muted();
      const volume = this.volume();

      if (!ref) {
        return;
      }

      const video = ref.nativeElement;

      if (item.isLocal || !item.hasAudio) {
        video.muted = true;
        video.volume = 0;
        return;
      }

      video.muted = muted;
      video.volume = Math.max(0, Math.min(1, volume / 100));
      void video.play().catch(() => {});
    });

  }

  @HostListener('document:fullscreenchange')
  onFullscreenChange(): void {
    const tile = this.tileRef()?.nativeElement;
    const isFullscreen = !!tile && document.fullscreenElement === tile;

    this.isFullscreen.set(isFullscreen);

    if (isFullscreen) {
      this.revealFullscreenHeader();
      return;
    }

    this.unlockOrientation();
    this.clearFullscreenHeaderHideTimeout();
    this.showFullscreenHeader.set(true);
  }

  ngOnDestroy(): void {
    this.clearFullscreenHeaderHideTimeout();

    const tile = this.tileRef()?.nativeElement;

    if (tile && document.fullscreenElement === tile) {
      void document.exitFullscreen().catch(() => {});
    }

    this.unlockOrientation();
  }

  canToggleFullscreen(): boolean {
    return !this.mini() && !this.compact();
  }

  onTilePointerMove(): void {
    if (!this.isFullscreen()) {
      return;
    }

    this.revealFullscreenHeader();
  }

  async onTileDoubleClick(event: MouseEvent): Promise<void> {
    event.preventDefault();
    event.stopPropagation();

    await this.toggleFullscreen();
  }

  async toggleFullscreen(event?: Event): Promise<void> {
    event?.preventDefault();
    event?.stopPropagation();

    if (!this.canToggleFullscreen()) {
      return;
    }

    if (this.isFullscreen()) {
      await document.exitFullscreen().catch(() => {});
      return;
    }

    await this.enterFullscreen();
  }

  async enterLandscapeFullscreen(event?: Event): Promise<void> {
    event?.preventDefault();
    event?.stopPropagation();

    if (!this.canToggleFullscreen()) {
      return;
    }

    if (!this.isFullscreen()) {
      await this.enterFullscreen();
    }

    await this.lockLandscape();
  }

  async exitFullscreen(event?: Event): Promise<void> {
    event?.preventDefault();
    event?.stopPropagation();

    if (!this.isFullscreen()) {
      return;
    }

    await document.exitFullscreen().catch(() => {});
  }

  requestFocus(): void {
    this.focusRequested.emit(this.item().id);
  }

  toggleMuted(): void {
    const item = this.item();

    if (item.isLocal || !item.hasAudio) {
      return;
    }

    const nextMuted = !this.muted();

    this.muted.set(nextMuted);
    this.workspacePlayback.setUserMuted(item.peerKey, nextMuted);
  }

  updateVolume(event: Event): void {
    const item = this.item();

    if (item.isLocal || !item.hasAudio) {
      return;
    }

    const input = event.target as HTMLInputElement;
    const nextVolume = Math.max(0, Math.min(100, parseInt(input.value, 10) || 0));

    this.volume.set(nextVolume);
    this.workspacePlayback.setUserVolume(item.peerKey, nextVolume);

    if (nextVolume > 0 && this.muted()) {
      this.muted.set(false);
      this.workspacePlayback.setUserMuted(item.peerKey, false);
    }
  }

  displayName(): string {
    return this.item().isLocal ? 'You' : this.item().user.displayName;
  }

  streamIconName(): string {
    return this.item().kind === 'camera' ? 'lucideVideo' : 'lucideMonitor';
  }

  streamBadgeLabel(): string {
    return this.item().kind === 'camera' ? 'Camera live' : 'Screen share live';
  }

  fullscreenDescription(): string {
    if (this.item().isLocal) {
      return this.item().kind === 'camera'
        ? 'Local camera preview in fullscreen'
        : 'Local preview in fullscreen';
    }

    return this.item().kind === 'camera'
      ? 'Fullscreen camera view'
      : 'Fullscreen stream view';
  }

  localPreviewDescription(): string {
    return this.item().kind === 'camera'
      ? 'Your camera preview never captures audio.'
      : 'Your preview stays muted locally to avoid audio feedback.';
  }

  canControlStreamAudio(): boolean {
    const item = this.item();

    return !item.isLocal && item.hasAudio;
  }

  private async enterFullscreen(): Promise<void> {
    const tile = this.tileRef()?.nativeElement;

    if (tile?.requestFullscreen) {
      await tile.requestFullscreen().catch(() => {});
      return;
    }

    const video = this.videoRef()?.nativeElement as WebKitFullscreenVideoElement | undefined;

    if (video?.webkitSupportsFullscreen && video.webkitEnterFullscreen) {
      video.webkitEnterFullscreen();
    }
  }

  private async lockLandscape(): Promise<void> {
    if (!this.isMobile()) {
      return;
    }

    const orientation = screen.orientation as LockableScreenOrientation | undefined;

    await orientation?.lock?.('landscape').catch(() => {});
  }

  private unlockOrientation(): void {
    screen.orientation?.unlock?.();
  }

  private scheduleFullscreenHeaderHide(): void {
    this.clearFullscreenHeaderHideTimeout();

    this.fullscreenHeaderHideTimeoutId = setTimeout(() => {
      this.showFullscreenHeader.set(false);
      this.fullscreenHeaderHideTimeoutId = null;
    }, 2200);
  }

  private revealFullscreenHeader(): void {
    this.showFullscreenHeader.set(true);
    this.scheduleFullscreenHeaderHide();
  }

  private clearFullscreenHeaderHideTimeout(): void {
    if (this.fullscreenHeaderHideTimeoutId === null) {
      return;
    }

    clearTimeout(this.fullscreenHeaderHideTimeoutId);
    this.fullscreenHeaderHideTimeoutId = null;
  }
}

interface WebKitFullscreenVideoElement extends HTMLVideoElement {
  webkitEnterFullscreen?: () => void;
  webkitSupportsFullscreen?: boolean;
}

interface LockableScreenOrientation extends ScreenOrientation {
  lock?: (orientation: 'landscape') => Promise<void>;
}
