import { RaceDetailUserResultDTO } from '../types/generated/RaceDetailUserResultDTO';

export class RaceDetailUserResultViewModel {
  position!: number;
  startPosition!: number;
  incidents!: number;
  fastestLap!: number;
  positionChange!: number;
  isPodium!: boolean;
  isClean!: boolean;
  ratingChange!: number;

  constructor(dto: RaceDetailUserResultDTO) {
    this.position = dto.position;
    this.startPosition = dto.startPosition;
    this.incidents = dto.incidents;
    this.fastestLap = dto.fastestLap;
    this.positionChange = dto.positionChange;
    this.isPodium = dto.isPodium;
    this.isClean = dto.isClean;
    this.ratingChange = dto.ratingChange ?? 0;
  }

  /** UI-specific: Display for position change */
  get positionChangeDisplay(): string {
    if (this.positionChange > 0) return `+${this.positionChange}`;
    if (this.positionChange < 0) return `${this.positionChange}`;
    return '0';
  }

  /** UI-specific: Color for position change */
  get positionChangeColor(): string {
    if (this.positionChange > 0) return 'green';
    if (this.positionChange < 0) return 'red';
    return 'gray';
  }

  /** UI-specific: Whether this is the winner */
  get isWinner(): boolean {
    return this.position === 1;
  }

  /** UI-specific: Rating change display */
  get ratingChangeDisplay(): string {
    if (this.ratingChange > 0) return `+${this.ratingChange}`;
    return `${this.ratingChange}`;
  }

  /** UI-specific: Rating change color */
  get ratingChangeColor(): string {
    if (this.ratingChange > 0) return 'green';
    if (this.ratingChange < 0) return 'red';
    return 'gray';
  }

  /** UI-specific: Formatted lap time */
  get lapTimeFormatted(): string {
    if (this.fastestLap <= 0) return '--:--.---';
    const minutes = Math.floor(this.fastestLap / 60);
    const seconds = Math.floor(this.fastestLap % 60);
    const milliseconds = Math.floor((this.fastestLap % 1) * 1000);
    return `${minutes}:${seconds.toString().padStart(2, '0')}.${milliseconds.toString().padStart(3, '0')}`;
  }
}