import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

import { ProfileService } from '../../services/profiles';
import { ProfilePage } from '../profile/profile';
import { ChatPage } from '../chat/chat';

import moment from 'moment';

@Component({
  selector: 'page-messages',
  templateUrl: 'messages.html',
  providers: [ ProfileService ]
})
export class MessagesPage {

  profiles: any;
  tabNavEl: any;

  constructor(public navCtrl: NavController, public profileService: ProfileService) {
    profileService.load().then((data) => {
      this.profiles = data;
    });
    this.tabNavEl = document.querySelector('#tab-nav .tabbar');
  }

  ionViewWillEnter() {
    this.tabNavEl.style.display = 'flex';
  }

  getLatestMessage(messages) {
    var latest = messages[(messages.length - 1)];
    var isUser = latest.isUser;
    return latest.text ? latest.text : '<em>' + (!isUser ? 'Sent ' : '') + 'Photo' + (isUser ? ' Recieved' : '') + '</em>';
  }

  getLatestMessageTimestamp(messages) {
    return moment(messages[(messages.length - 1)].timestamp).fromNow();
  }

  interviewTapped(event, profile) {
    this.navCtrl.push(ChatPage, {
      profile: profile
    });
  }

  profilePictureTapped(event, profile) {
    this.navCtrl.push(ProfilePage, {
      profile: profile
    });
  }
}
