import { Component, OnInit, Input } from '@angular/core';
import { ChatConversation } from 'app/chat/models/chat-conversation';
import { ChatService } from 'app/chat/services/chat.service';
import { ChatMessage } from 'app/chat/models/chat-message';
import { ChatMessageType } from 'app/chat/models/chat-message-type.enum';
import { ChatUser } from 'app/chat/models/chat-user';
import { ChatUiService } from 'app/chat/services/chat-ui.service';

@Component({
  selector: 'app-chat-conversation',
  templateUrl: './chat-conversation.component.html',
  styleUrls: ['./chat-conversation.component.scss']
})
export class ChatConversationComponent implements OnInit {

  collapsed: boolean = false;

  @Input()
  conversation: ChatConversation;

  constructor(
    private chatService: ChatService,
    private chatUiService: ChatUiService
  ) { }

  private get userId(): number {
    return this.currentUser.id;
  }

  private get currentUser(): ChatUser {
    return this.chatService.getCurrentUser();
  }

  ngOnInit() { }

  toggleCollapsed() {
    this.collapsed = !this.collapsed;
  }

  sendMessage(message: ChatMessage) {
    this.chatService.sendMessage(this.userId, this.conversation.user.id, message);
  }

  closeWindow() {
    this.chatUiService.closeConversation(this.conversation);
  }
}
