import {Component, OnInit} from '@angular/core';
import { HttpClient } from "@angular/common/http";
import {environment} from "../../environments/environment";
import {DatePipe, DecimalPipe, JsonPipe, NgForOf, NgIf} from "@angular/common";
import {calculateTimeAgo, formatDuration} from "../format";
import {RouterLink} from "@angular/router";
import {UserService} from "../../corelib/service/user.service";
import {Title} from "@angular/platform-browser";
import {DownloadFilesService} from "../../corelib/service/download-files.service";

interface FollowsBanStatusResponse {
  follows: FollowsBanStatusEntry[];
}

interface FollowsBanStatusEntry {
  userId: number;
  username: string;
  secondsPlayed?: number;
  pp?: number;
  rank?: number;
  isBanned?: boolean;
  approximateBanTime?: string;
  lastUpdate?: string;
}


@Component({
  selector: 'app-profile',
  standalone: true,
  imports: [
    JsonPipe,
    NgForOf,
    NgIf,
    RouterLink,
    DatePipe,
    DecimalPipe
  ],
  templateUrl: './profile.component.html',
  styleUrl: './profile.component.css'
})
export class ProfileComponent implements OnInit {

  follows: FollowsBanStatusResponse | null = null;

  constructor(
    private httpClient: HttpClient,
    private title: Title,
    public userService: UserService,
    public downloadFilesService: DownloadFilesService
  ) { }

  ngOnInit(): void {
    this.title.setTitle(`hi, ${this.userService.currentUser!.username}`);
    this.getFollows();
  }

  getFollows(): void {
    this.httpClient.get<FollowsBanStatusResponse>(`${environment.apiUrl}/follows`).subscribe(response => {
      this.follows = response;
    });
  }

  protected readonly calculateTimeAgo = calculateTimeAgo;
  protected readonly formatDuration = formatDuration;
}
