import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ApiService as ApiService } from '../../services/login-api';
import { RaidData } from '../../../../../../backend/Types/Types';
import { getClassColor } from '../../../../../../backend/Types/PlayerSpecs';

@Component({
  selector: 'archive',
  templateUrl: './raid.component.html',
})
export class FrontcraftArchiveComponent implements OnInit{

    canSignup = false
    isSignedup = false
    canManage = false
    mySignup

    raid: RaidData = <any>{
      participants:{
          Druid: [],
          Hunter: [],
          Mage: [],
          Paladin: [],
          Priest: [],
          Rogue: [],
          Shaman: [],
          Warlock: [],
          Warrior: [],
      },
      tokens:{}
    }
    
    constructor(
      private api: ApiService,
      private route: ActivatedRoute,
    ){
    }

    async ngOnInit(){
      this.refresh()
    }

    refresh = async () => {
      const param = this.route.snapshot.paramMap.get('id');

      const raidManager = this.api.get('RaidManager')
      const raiddata = await raidManager.getArchiveRaid(parseInt(param))
      this.raid = raiddata

      Object.values(raiddata.participants).flat().forEach(p => {
        p['color'] = getClassColor(p.class)
      })
      
      const matchingSignup = Object.values(raiddata.participants).flat().find(char => char.userid === this.api.getCurrentUser()!.id!)
      if(matchingSignup){
        this.isSignedup = true
        this.mySignup = matchingSignup
        this.mySignup.status = matchingSignup['benched']?'Bench':matchingSignup['late']?'Late':'Attending'
      }else{
        this.isSignedup = false
        this.mySignup = null
      }
    } 
}