import { OnInit, Component } from '@angular/core';
import { NbWindowRef, NbToastrService, NbDialogRef } from '@nebular/theme';
import { RaidData, Character } from '../../../../../../backend/Types/Types';
import { ApiService } from '../../services/login-api';
import { getClassColor } from '../../../../../../backend/Types/PlayerSpecs';

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

    raid : RaidData
    characters: Character[]

    constructor(
        protected dialogRef: NbDialogRef<FrontcraftCharacerpickerComponent>,
        private api : ApiService,
        private toast : NbToastrService
    ){}

    signup = async (character: Character, late: boolean) => {
        const auth = this.api.getAuth()
        const signup = this.api.get('signup')
        if(!signup) return

        await signup.sign(auth.token.value, character, this.raid, late)
        this.toast.show('Signup', 'Success', { status: 'success' })
        this.dialogRef.close()
    }

    ngOnInit(): void {
        const usr = this.api.getCurrentUser()
        this.api.get('CharacterManager')
        .getCharactersOfUser(usr.username)
        .then(chars => {
            chars.forEach(char => {
                char['color'] = getClassColor(char.class)
            })
            this.characters = chars
        })
    }

}