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

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

  contacts: ChatUser[] = [];
  conversations: ChatConversation[];
  @Input() showWithFooter: boolean
  constructor(
    private chatService: ChatService,
    private chatUiService: ChatUiService,
    private userService: UserService
  ) {
    this.showWithFooter = false;
   }

  ngOnInit() {
    this.initialize();
  }

  initialize() {
    if (this.userService.userInfo()) {
      let userId = this.userService.userInfo().userInfoMstrId;
      this.chatService.connect();

      this.chatService.getContacts(userId).subscribe(response => {
        this.contacts = response as ChatUser[];
      });

      // TODO: make conversations into a subject behivor
      this.chatUiService.conversations.subscribe(resp => {
        this.conversations = resp;
      });
    } else {
      setTimeout(() => { this.initialize() }, 100);
    }

  }

}
