import { Component, Input, OnInit } from '@angular/core';

import { UsersService } from './users.service';

@Component({
  moduleId: module.id,
  selector: 'app-user',
  templateUrl: 'user.component.html',
  styleUrls: ['user.component.scss']
})
export class UserComponent implements OnInit {
  @Input() user: any;

  private selectedIndex: number = 0;
  @Input() set selectedTab(tab: string) {
    switch (tab) {
      case 'details': this.selectedIndex = 0; break;
      case 'activities': this.selectedIndex = 1; break;
      default: this.selectedIndex = 0;
    }
  }

  loanRequests: any[];
  loans: any[];
  showLoanRequests: boolean = false;
  showLoans: boolean = false;

  loading: boolean = true;

  constructor(private usersService: UsersService) { }

  ngOnInit() {
    const userPromise = typeof this.user === 'number' ? this.usersService.getUser(this.user) : Promise.resolve(this.user);

    userPromise
      .then(user => this.user = user)
      .then(user =>
        Promise.all([])
      )
      .then(_ => this.loading = false);
  }
}
