import { LapNumber } from './LapNumber';
import { TimeInRace } from './TimeInRace';
import { IncidentDescription } from './IncidentDescription';

export class ProtestIncident {
  private constructor(
    private readonly _lap: LapNumber,
    private readonly _timeInRace: TimeInRace | undefined,
    private readonly _description: IncidentDescription
  ) {}

  static create(lap: number, description: string, timeInRace?: number): ProtestIncident {
    const lapNumber = LapNumber.create(lap);
    const time = timeInRace !== undefined ? TimeInRace.create(timeInRace) : undefined;
    const desc = IncidentDescription.create(description);
    return new ProtestIncident(lapNumber, time, desc);
  }

  get lap(): LapNumber {
    return this._lap;
  }

  get timeInRace(): TimeInRace | undefined {
    return this._timeInRace;
  }

  get description(): IncidentDescription {
    return this._description;
  }

  equals(other: ProtestIncident): boolean {
    const timeEqual = this._timeInRace === undefined && other._timeInRace === undefined ||
      (this._timeInRace !== undefined && other._timeInRace !== undefined && this._timeInRace.equals(other._timeInRace));
    return (
      this._lap.equals(other._lap) &&
      timeEqual &&
      this._description.equals(other._description)
    );
  }
}