import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NbToastrService } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { AvalonService } from '../../../services/avalon.service';
import { SessionService } from '../../../services/session.service';
import { AvalonBase } from '../avalonBase';

@Component({
  selector: 'avalon-join-game',
  templateUrl: './join-game.component.html',
  styleUrls: ['./join-game.component.scss']
})
export class JoinGameComponent extends AvalonBase implements OnInit {

  tempUsername: string;
  tempSeq: number;
  constructor(
    private route: ActivatedRoute,
    protected cdRef: ChangeDetectorRef,
    protected avalonService: AvalonService,
    private ngZone: NgZone,
    private session: SessionService
  ) {
    super(cdRef, avalonService);
  }

  ngOnInit(): void {
    this.route.queryParamMap.pipe(first()).subscribe(result => {
      if (result.has('name')) {
        this.tempUsername = result.get('name');
      } else {
        this.tempUsername = this.session.currentUserName;
      }
    });
  }

  joinGame() {
    this.avalonService.joinGame(this.tempUsername, this.tempSeq);
  }

  public get rngAssignRole(): boolean {
    return this.avalonService.rngAssignRole;
  }


  setRngAssignRole(v) {
    this.avalonService.rngAssignRole = v;
  }
  public get qrCodeWidth(): number {
    return window.innerWidth - 100;
  }
}
