import { RoomInfo } from '@ucap-webmessenger/protocol-room';
import { Component, OnInit, Inject } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';

export interface EditChatRoomDialogData {
  title: string;
  roomInfo: RoomInfo;
}

export interface EditChatRoomDialogResult {
  choice: boolean;
  roomName?: string;
  roomNameChangeTarget?: string;
  timeRoomInterval?: number;
  roomInfo: RoomInfo;
}

@Component({
  selector: 'app-layout-messenger-edit-chat-room',
  templateUrl: './edit-chat-room.dialog.component.html',
  styleUrls: ['./edit-chat-room.dialog.component.scss']
})
export class EditChatRoomDialogComponent implements OnInit {
  roomName: string;
  inputForm: FormGroup;

  timerArray: { value: number; text: string }[] = [
    { value: 5, text: '5 초' },
    { value: 10, text: '10 초' },
    { value: 30, text: '30 초' },
    { value: 60, text: '1 분' },
    { value: 300, text: '5 분' },
    { value: 600, text: '10 분' },
    { value: 1800, text: '30 분' },
    { value: 3600, text: '1 시간' },
    { value: 21600, text: '6 시간' },
    { value: 43200, text: '12 시간' },
    { value: 86400, text: '24 시간' }
  ];

  constructor(
    public dialogRef: MatDialogRef<
      EditChatRoomDialogData,
      EditChatRoomDialogResult
    >,
    @Inject(MAT_DIALOG_DATA) public data: EditChatRoomDialogData,
    private formBuilder: FormBuilder
  ) {}

  ngOnInit(): void {
    this.inputForm = this.formBuilder.group({
      roomName: [this.data.roomInfo.roomName],
      changeTarget: ['me'],
      timerInterval: [this.data.roomInfo.timeRoomInterval]
    });
  }

  onClickChoice(choice: boolean): void {
    this.dialogRef.close({
      choice,
      roomName: this.inputForm.get('roomName').value,
      roomNameChangeTarget: this.inputForm.get('changeTarget').value,
      timeRoomInterval: this.inputForm.get('timerInterval').value,
      roomInfo: this.data.roomInfo
    });
  }
}
