import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {PeopleModel} from '../models/people.model';
import {PeopleService} from '../service/people.service';
import {AuthService} from '../service/auth.service';
import {Button} from '@progress/kendo-angular-buttons';
import {Router} from '@angular/router';
import {UserExtraModel} from '../models/user-extra.model';
import {BadgeAlign} from '@progress/kendo-angular-indicators';

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

  @Input() PeopleId: string;
  @ViewChild('enabled', {static: true}) enabled: Button;
  public contact: PeopleModel = new PeopleModel('', '', '', '', '', '', '' , false);
  public UserExtra: UserExtraModel = UserExtraModel.EmptyNew();

  public badgeAlign: BadgeAlign = { vertical: 'bottom', horizontal: 'end' };

  constructor(private ps: PeopleService, private auth: AuthService, private router: Router) { }

  ngOnInit(): void {
    this.ps.getPeopleById(this.PeopleId).subscribe(
      resp => {
        this.contact = new PeopleModel(resp.Id, resp.First, resp.Last, resp.Middle, resp.Title, resp.Display, resp.Nick, resp.Enabled);
      }
    );

    this.ps.getPeopleExtraById(this.PeopleId).subscribe(
      resp => {
        this.UserExtra.Copy(resp);
      }
    );


  }

  photoURL(): string {
    if (this.contact === undefined ) { return ''; }
    const url = 'url(' + this.auth.getUrl('avatar/') + this.contact.Id + ')';
    return url;
  }

  public bgPhoto(): string {
    if ( !this.contact.Enabled ){
      return 'url("../../assets/img/bg-disabled-card.jpg")';
    }
    switch (this.UserExtra.Role) {
      case 'broker':
        return 'url("../../assets/img/bg-client-card.jpg")';
      case 'user':
        return 'url("../../assets/img/bg-user-card.jpg")';
      case 'people':
        return 'url("../../assets/img/bg-broker-card.jpg")';
      case 'admin':
        return 'url("../../assets/img/bg-admin-card.jpg")';
    }

  }

  public onButtonClick(): void {
    this.router.navigate(['/profile/' + this.contact.Id]);
  }

  public onSelectedChange(e: boolean): void {
    this.ps.enableUser(e, this.PeopleId).subscribe(
      resp => {
        this.contact.Enabled = resp;
      });
  }
}
