import {Component} from '@angular/core';
import {RandomGameService, RandomGameStatus} from './random-game.service';
import {map} from 'rxjs/operators';

@Component({
  selector: 'app-random-game',
  templateUrl: './random-game.component.html',
  styleUrls: ['./random-game.component.scss']
})
export class RandomGameComponent {
  guess = 42;
  status$ = this.contractService.state$.pipe(
    map(s => this.getStatusText(s.status))
  );
  gameId$ = this.contractService.state$.pipe(
    map(s => s.gameId)
  );

  constructor(private contractService: RandomGameService) {
  }

  get chanStartGame() {
    return this.contractService.canStartGame;
  }

  async startGame() {
    await this.contractService.startGame(this.guess);
  }

  private getStatusText(status: RandomGameStatus) {
    switch (status) {
      case RandomGameStatus.Initial:
        return 'Initial - Ready to Start a Game';
      case RandomGameStatus.WaitingForPartnerRegistration:
        return 'Waiting for partner... he has not yet started the game';
      case RandomGameStatus.WaitBeforeSendNum:
        return 'Waiting before submitting our guess in plain text';
      case RandomGameStatus.ReadyToSendNum:
        return 'Submitting our guess in plain text';
      case RandomGameStatus.WaitForPartnerToSendNum:
        return 'Waiting until for partner... he has not yet submitted his guess...';
      case RandomGameStatus.GettingWinner:
        return 'And the winner is....';
      case RandomGameStatus.FinishedAsLooser:
        return 'Game finished - Oh no you had no luck!';
      case RandomGameStatus.ReadyToTakePrice:
        return 'Yeahhhhhh!!! You won!!! - getting the price...';
      case RandomGameStatus.FinishedAsWinner:
        return 'Yeahhhhhh!!! You won!!! 🥳 - price submitted';
    }
  }

  retry() {
    this.contractService.retry();
  }

  get canResetGame() {
    return this.contractService.canReset;
  }

  resetGame() {
    this.contractService.resetGame();
  }
}
