import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { AvalonStage, QuestInfo } from '../../../entity/Avalon';
import { AvalonService } from '../../../services/avalon.service';
import { StringUtils } from '../../../utilities/string-utils';
import { AvalonBase } from '../avalonBase';

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

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

  ngOnInit(): void {
  }


  public get quests(): QuestInfo[] {
    if (this.avalonService.data && this.avalonService.data.questInfos.length > 0) {
      return this.avalonService.data.questInfos;
    }
    return null;
  }

  public teamSize(q: QuestInfo): string {
    let result = q.teamSize.toString();
    if (q.approve > 0) {
      result += ' :' + StringUtils.getHtmlBadge(q.approve.toString(), "light");
    }
    if (q.reject > 0) {
      result += StringUtils.getHtmlBadge(q.reject.toString(), "dark");
    }
    return result;
  }

  public teamMembers(q: QuestInfo): string {
    if (q.teamMembers.length == 0) return "";
    return StringUtils.getHtmlBadges(q.teamMembers.map(t => t.username), 'info');
  }


  public questResult(q: QuestInfo): string {
    let result = "";
    if (q.success > 0) {
      result += StringUtils.getHtmlBadge(q.success.toString(), "info");
    }
    if (q.failed > 0) {
      result += StringUtils.getHtmlBadge(q.failed.toString(), "danger");
    }
    if (q.need2Failed) result += StringUtils.getHtmlBadge("2 Failed", "warning");
    return result;
  }

  newGame() {
    this.avalonService.data.stage = AvalonStage.JoinGame;
    this.avalonService.data.questInfos = [];
    this.broadcastAll();
  }
}
