import {CommandBus, CommandHandler, EventBus, ICommandHandler} from '@nestjs/cqrs';
import { NextQuestionCommand } from '../../game/commands/next-question.command';
import { QuizService } from '../quiz.service';
import { GiveCardsCommand } from '../../game/commands/give-cards.command';
import {NextQuestionEvent} from "../../game/events/next-question.event";

@CommandHandler(NextQuestionCommand)
export class GameNextQuestionCommandHandler
  implements ICommandHandler<NextQuestionCommand> {
  constructor(private quizService: QuizService, private cmdBus: CommandBus, private eventBus: EventBus) {
  }
  async execute(command: NextQuestionCommand): Promise<any> {
    this.eventBus.publish(new NextQuestionEvent());
    await this.cmdBus.execute(new GiveCardsCommand());
    await this.quizService.playNextQuestion();
  }
}
