import {Component, OnInit} from '@angular/core';
import {Router, RouterLink, RouterOutlet} from "@angular/router";
import {UserService} from "../corelib/service/user.service";
import {DecimalPipe, NgIf} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {Observable} from "rxjs";
import {environment} from "../environments/environment";
import {LocalCacheService} from "../corelib/service/local-cache.service";

interface Statistics {
  total_beatmaps: number;
  total_users: number;
  total_scores: number;
  total_replay_scores: number;
  total_replay_similarity: number;
  total_bans: number;
}


@Component({
  selector: 'app-root',
  standalone: true,
    imports: [
        RouterLink,
        FormsModule,
        NgIf,
        RouterOutlet,
        DecimalPipe
    ],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  statistics: Statistics | null = null;
  term: string = '';

  constructor(private router: Router,
              private localCacheService: LocalCacheService,
              public userService: UserService
  ) { }

  ngOnInit(): void {
    this.getStatistics().subscribe((response: Statistics) => {
      this.statistics = response;
    });
  }

  getStatistics(): Observable<Statistics> {
    return this.localCacheService.fetchData<Statistics>(
      'statistics',
      `${environment.apiUrl}/stats`,
      60
    );
  }

  onSubmit(): void {
    this.router.navigate(['/u/' + this.term])
      .then();
  }

}
