import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
import { AppDragDropService } from 'src/app/shared/drag/app-drag-drop.service';
import { SidebarResource } from 'src/app/shared/server-services/query-records/sidebar-records';
import { RoomService } from 'src/app/shared/server-services/room.service';

@Component({
  selector: 'app-sidebar-room-drag-preview',
  templateUrl: './sidebar-room-drag-preview.component.html',
  styleUrls: ['./sidebar-room-drag-preview.component.scss'],
})
export class SidebarRoomDragPreviewComponent implements OnInit {
  public data: SidebarResource;
  public name: string;
  public avatar: ArrayBuffer;
  public loaded: boolean = false;

  constructor(
    private appDragDropService: AppDragDropService,
    private roomService: RoomService,
    private changeDetectionRef: ChangeDetectorRef
  ) {}

  ngOnInit(): void {
    this.data = this.appDragDropService.getDraggedComponent().previewData;
    this.roomService.getRoom(this.data.id).then((room) => {
      if (room.data.decryptionError) {
        this.name = marker('Room');
      } else {
        this.name = room.data.name;
        this.avatar = room.data.avatar;
      }
      this.loaded = true;
      this.changeDetectionRef.detectChanges();
    });
  }
}
