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 {CuteProgressbarComponent} from "../../corelib/components/cute-progressbar/cute-progressbar.component";
import {CuteLoadingComponent} from "../../corelib/components/cute-loading/cute-loading.component";
import {DownloadFilesService} from "../../corelib/service/download-files.service";
import {CalculatePageRangePipe} from "../../corelib/calculate-page-range.pipe";

interface BanlistResponse {
  pagination: BanlistPagination;
  users: BanlistEntry[];
}

interface BanlistPagination {
  currentPage: number;
  pageSize: number;
  totalResults: number;
  totalPages: number;
}

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

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

  isLoading = true;
  banlist: BanlistResponse | null = null;

  constructor(
    private httpClient: HttpClient,
    public downloadFilesService: DownloadFilesService
  ) { }

  ngOnInit(): void {
    this.getBanlist();
  }

  getBanlist(page: number = 1): void {
    const body = {
      page: page
    }
    this.httpClient.post<BanlistResponse>(`${environment.apiUrl}/banlist`, body).subscribe(response => {
      this.banlist = response;
      this.isLoading = false;
    });
  }

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