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

@Component({
  selector: 'character',
  templateUrl: './character.component.html',
})
export class FrontcraftCharacterComponent implements OnInit{

    @Input() name?: string
    @Input() link?: "owner" | "character" = 'owner'

    char : (Character & User & Spec) = {} as any
    color : string
    tokens

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

    async ngOnInit(){
      const param = this.name || this.route.snapshot.paramMap.get('name');
      if(!param) return
      
      this.api.get('CharacterManager')      
      .getCharacterByName(param)
      .then((char) => {
        if(char){
          this.color = getClassColor(char.class)
          this.char = char
          this.api.get('ItemManager').getTokens(this.char).then(tokens => {
            this.tokens = tokens
          })
        }
      })
    }
}