import {
  CommandBus,
  EventBus,
  EventsHandler,
  IEventHandler,
} from '@nestjs/cqrs';
import { ValidAnswerReceivedEvent } from '../../game/events/valid-answer.recieved';
import { GuestsService } from '../guests.service';
import { SendToastCommand } from '../../game/commands/send-toast.command';

@EventsHandler(ValidAnswerReceivedEvent)
export class GuestValidAnswerReceivedEventHandler
  implements IEventHandler<ValidAnswerReceivedEvent>
{
  constructor(
    private guestService: GuestsService,
    private eventBus: EventBus,
    private commandBus: CommandBus,
  ) {}

  async handle(event: ValidAnswerReceivedEvent) {
    await this.guestService.notifyAboutValidAnswer(event.tId);
    await this.guestService.sendValidAnswerActions(event.tId);
    // await this.guestService.updatePlayerScore(event.tId, 1);
    if (event.extraDetails) {
      await this.commandBus.execute(
        new SendToastCommand(event.extraDetails, 4000),
      );
    }
    const coef = +(await this.guestService.getCoefficient());
    // await this.guestService.changeWinningChance(event.tId, 29 * coef);
    await this.guestService.resetInvalidAnswersInTheRow(event.tId);
  }
}
