import { ChangeDetectorRef, Component, Input, NgZone, OnInit } from '@angular/core';
import { NbToastrService } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { GameInfo, Player, Role, RoleInfo } from '../../../entity/Avalon';
import { AvalonService } from '../../../services/avalon.service';
import { MsgBoxService } from '../../../services/msg-box.service';
import { ADButtons, ADIcon } from '../../../ui/alert-dlg/alert-dlg.model';
import { AvalonBase } from '../avalonBase';

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



  constructor(
    private msgBoxService: MsgBoxService,
    protected cdRef: ChangeDetectorRef,
    private toastrService: NbToastrService,
    protected avalonService: AvalonService,
    private ngZone: NgZone
  ) {
    super(cdRef, avalonService);
  }

  ngOnInit(): void {
  }

  public pickRole(roleInfo: RoleInfo) {

    // this.me.role = roleInfo.role;
    // this.hasRole = true;
    // this.broadcastMe('PickRole');
    // this.cdRef.detectChanges();
    // return;
    this.ngZone.run(
      _ => {
        this.msgBoxService.show(roleInfo.name, { text: roleInfo.description + '<br>你確定你是?', icon: ADIcon.QUESTION, buttons: ADButtons.YesNo }).pipe(first()).subscribe(result => {
          if (result) {
            if (this.players.find(p => p.role == roleInfo.role) && roleInfo.role != Role.ArthurKnight) {
              this.toastrService.danger('腳色重複:' + roleInfo.name, '你確定沒有選錯腳色??')
            } else {
              this.me.role = roleInfo.role;
              this.broadcastMe('PickRole');
              this.cdRef.detectChanges();
            }
          }
        });
      }

    );


  }

  // private broadcastMe(action: PlayerAction) {
  //   console.log('broadcastMe', this.me.username);
  //   this.detectMoveToNextStage(action);
  //   this.broadcastPlayer(action, this.me);
  //   this.cdRef.detectChanges();
  // }

  // detectMoveToNextStage(action: PlayerAction) {
  //   if (
  //     ('VoteTeamMate' == action && this.unVoteTeamPlayerList.length == 0) ||
  //     ('VoteQuest' == action && this.unVoteQuestPlayerList.length == 0)
  //   ) {
  //     this.data.stage = this.data.stage + 1;
  //     this.clearVoteStatus();
  //   }
  // }

}
