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

@Component({
  selector: 'user-component',
  templateUrl: './user.component.html',
})
export class FrontcraftUserComponent implements OnInit{

  user:User = {} as any
  characters : (Character & Spec)[] = []

  constructor(
    private route: ActivatedRoute,
    private api : ApiService
  ){}

  async ngOnInit(){
    const param = this.route.snapshot.paramMap.get('name');
    const usr = await this.api.get('UserManager').getUser(param)


    if(!usr) return
    this.user = usr

    const characters = await this.api.get('CharacterManager').getCharactersOfUser(this.user.username)
    characters.forEach(async c => {
      const tokens = await this.api.get('ItemManager').getTokens(c)
      c['tokens'] = tokens
      c['color'] = getClassColor(c.class)
    })
    this.characters = characters
  }
}