import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { AuthService } from 'src/app/modules/auth.service';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class UserService {

  players: any[] = [];

  constructor(private http: HttpClient, private authService: AuthService, private snackBar: MatSnackBar) { }


  loadTeamsOfUser() {
    this.http.get(`${environment.apiUrl}users/${this.authService.user.id}/teams`).subscribe({
      next: data => { this.players = data as any[]; },
      error: () => { this.onUserLoadFailure(); }
    })
  }

  onUserLoadFailure() {
    this.snackBar.open('Teams konnten nicht geladen werden', undefined, {
      duration: 5000,
      panelClass: 'snackbar_error'
    })
  }
}
