import { Component, OnInit } from '@angular/core';
import { LoginApiService, hash } from '../../services/login-api';
import { Router } from '@angular/router';
import { _Rank, _Class, Class, User  } from '../../../../../../backend/Types/Types'
import { specs  } from '../../../../../../backend/Types/PlayerSpecs'


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

  user = {
    rank: "Guest"
  } as User

  character: any = {
    class : "Warrior",
    spec : "Arms"
  }


  ranks = _Rank
  classes = _Class
  selectedClass : Class = "Warrior"
  selectedSpec = specs[this.selectedClass][0]
  specs = specs[this.selectedClass]
  showApplication = false

  constructor(
    private router : Router,  
    private loginApi : LoginApiService
  ){}

  ngOnInit(){
    this.loginApi.checkLogin().then(loggedin => {
      if(loggedin){
        this.router.navigateByUrl("/")
      }
    })
  }

  onSelectClass(){
    this.specs = specs[this.character.class]
    setTimeout(() => {
      this.character.spec = this.specs[0]
    }, 25)
  }

  onSelectSpec(){
  }

  async onSubmit(){
    this.user.pwhash = await hash(this.user.pwhash)
    const user = this.user
    this.user = {} as User
    const char = this.character
    this.character = {}

    try{
      const usr = await this.loginApi.getUnprivilegedSocket().Authenticator.createUser(user)
      
    }catch(e){
      alert("Error creating user"+e)
    }
  }

}