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

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

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

  profiles: any;
  tabNavEl: any;

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

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

  doTellStory() {
    this.navCtrl.push(TellYourStoryPage);
  }

  getBackgroundThumbnail(pics) {
    return this._sanitizer.bypassSecurityTrustStyle('url(https://appsby.fitz.guru/urge/' + pics.thumb + ')');
  }

  profilePressed(event, profile) {
    if (profile.messages && profile.messages.length) {
      this.navCtrl.push(ChatPage, {
        profile: profile
      });
    }
  }

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