import { Component, OnInit, OnDestroy, Injector } from '@angular/core';
import { NbDialogService, NbToastrService } from '@nebular/theme';
import { FrontcraftCreateRaidsComponent } from './createraid.compontent';
import { Router, NavigationStart } from '@angular/router';
import { IApiService } from '../../services/ApiService/ApiService';
import { UpdatingComponent } from '../../UpdatingComponent';
import { LoggerService } from '../../services/LoggerService/logger.service';
import { FlashService } from '../../services/flash-service';
import { Tiers, _Tiers } from '../../../../../../backend/Types/Items';

@Component({
  selector: 'raids',
  templateUrl: 'raids.component.html',
  styleUrls: ['raids.component.scss'],
})
export class FrontcraftRaidsComponent
  extends UpdatingComponent
  implements OnInit {

  archiveCount = 50
  tiers:string[] = ["any", ..._Tiers]
  tier = "any"

  manageRaid
  raids = []
  oldraids = []
  pageSize = 25;

  constructor(
    injector: Injector,
    private api: IApiService,
    private dialogService: NbDialogService,
    private flash: FlashService,
  ) {
    super(injector)
  }

  ngOnDestroy() {
    super.ngOnDestroy()
  }

  async ngOnInit() {
    this.subscribe("raids", () => this.refresh().then(_ =>
      this.flash.show()
    ))

    this.manageRaid = this.api.get('manageRaid')
    this.refresh()
  }

  async refresh() {
    this.raids = await this.api.get('RaidManager').getRaids()
    const raidManager = this.api.get('RaidManager')
    this.oldraids = await raidManager.getPastRaids(this.archiveCount)

    if(this.tier !== "any"){
      this.oldraids = this.oldraids.filter(raid => raid.tier === this.tier)
    }
  }

  create() {
    this.dialogService.open(FrontcraftCreateRaidsComponent, {
      closeOnBackdropClick: true,
      closeOnEsc: true,
      context: {
        templates: this.oldraids
      }
    })
  }
}
