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

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

    raid : RaidData
    characters: Character[]
    memo: string

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

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

        await signup.sign(auth.token.value, character, this.raid, late, attending, this.memo)
        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
        })
    }

}