import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { AuthService } from '@services/auth/auth.service';

import { MatchesService } from '@services/matches/matches.service';
import { MatchState, Matches } from '@interfaces/matches';

import { PlayerService } from '@services/player/player.service';
import { Player } from '@interfaces/player';

import { RefereeService } from '@services/referee/referee.service';
import { Referee, RefereeLevel } from '@interfaces/referee';

import { getMatchStateLabel, getMatchStateColor } from '@utils/getState';

@Component({
  selector: 'app-matches-id',
  standalone: false,
  templateUrl: './matches-id.component.html',
  styleUrl: './matches-id.component.css',
})
export class MatchesIdComponent implements OnInit {
  match: Matches | undefined;
  player1: Player | undefined;
  player2: Player | undefined;
  referee: Referee | undefined;

  constructor(
    private route: ActivatedRoute,
    private matchService: MatchesService,
    private playerService: PlayerService,
    private refereeService: RefereeService,
    public authService: AuthService,
    private router: Router
  ) {}

  ngOnInit(): void {
    const id = Number(this.route.snapshot.paramMap.get('id'));
    this.matchService.getMatchById(id).subscribe((match) => {
      if (match) {
        this.match = match;

        this.playerService
          .getPlayerById(match.player1ID)
          .subscribe((p1) => (this.player1 = p1!));
        this.playerService
          .getPlayerById(match.player2ID)
          .subscribe((p2) => (this.player2 = p2!));

        this.refereeService
          .getRefereeById(match.refereeID)
          .subscribe((ref) => (this.referee = ref!));
      }
    });
  }

  saveScores(): void {
    if (this.match) {
      this.matchService.updateMatch(this.match.id, {
        score1: this.match.score1,
        score2: this.match.score2,
      });
    }
  }

  getMatchStateLabel(state: MatchState): string {
    switch (state) {
      case MatchState.NOT_STARTED:
        return 'À venir';
      case MatchState.ONGOING:
        return 'En cours';
      case MatchState.OVER:
        return 'Terminé';
      default:
        return 'Inconnu';
    }
  }

  getMatchStateColor(state: MatchState): string {
    switch (state) {
      case MatchState.NOT_STARTED:
        return 'secondary';
      case MatchState.ONGOING:
        return 'warning';
      case MatchState.OVER:
        return 'success';
      default:
        return 'light';
    }
  }

  getRefereeLevelLabel(level: RefereeLevel): string {
    switch (level) {
      case RefereeLevel.NATIONAL:
        return 'National';
      case RefereeLevel.REGIONAL:
        return 'Régional';
      case RefereeLevel.DEPARTMENTAL:
        return 'Départemental';
      default:
        return 'Inconnu';
    }
  }

  updateScores(): void {
    if (this.match) {
      this.matchService.updateMatch(this.match.id, {
        score1: this.match.score1,
        score2: this.match.score2,
      });
    }
  }

  incrementScore(player: 1 | 2): void {
    if (!this.match) return;

    // Incrémente le score du joueur
    if (player === 1) {
      this.match.score1 += 1;
    } else {
      this.match.score2 += 1;
    }

    // Change l'état en fonction des scores
    if (
      this.match.score1 === 1 &&
      this.match.score2 === 0 &&
      this.match.state === MatchState.NOT_STARTED
    ) {
      this.match.state = MatchState.ONGOING; // passage à l'état 1 (en cours)
    } else if (
      this.match.score2 === 1 &&
      this.match.score1 === 0 &&
      this.match.state === MatchState.NOT_STARTED
    ) {
      this.match.state = MatchState.ONGOING;
    }

    // Si l'un des scores arrive à 15, on considère le match terminé
    if (this.match.score1 >= 15 || this.match.score2 >= 15) {
      this.match.state = MatchState.OVER; // passage à l'état 2 (terminé)
    }

    this.updateScores();
  }

  goToMatchDel(): void {
    this.router.navigate([
      '/matches/del/',
      this.route.snapshot.paramMap.get('id'),
    ]);
  }
}
