import {
  Component,
  HostListener,
  OnInit,
  input,
  output,
  signal
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { ScreenShareQuality, SCREEN_SHARE_QUALITY_OPTIONS } from '../../../domains/screen-share';

@Component({
  selector: 'app-screen-share-quality-dialog',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './screen-share-quality-dialog.component.html'
})
export class ScreenShareQualityDialogComponent implements OnInit {
  selectedQuality = input.required<ScreenShareQuality>();
  includeSystemAudio = input(false);

  confirmed = output<ScreenShareQuality>();
  cancelled = output<undefined>();

  readonly qualityOptions = SCREEN_SHARE_QUALITY_OPTIONS;
  readonly activeQuality = signal<ScreenShareQuality>('balanced');

  @HostListener('document:keydown.escape')
  onEscape(): void {
    this.cancelled.emit(undefined);
  }

  ngOnInit(): void {
    this.activeQuality.set(this.selectedQuality());
  }

  chooseQuality(quality: ScreenShareQuality): void {
    this.activeQuality.set(quality);
  }

  confirm(): void {
    this.confirmed.emit(this.activeQuality());
  }
}
