import { Component } from '@angular/core';
import { DataService } from 'src/services/data/data.service';
import { ClassService } from 'src/services/class/class.service';

@Component({
  selector: 'app-journal-character',
  templateUrl: './journal-character.component.html',
  styleUrls: ['./journal-character.component.scss'],
})
export class JournalCharacterComponent {
  public activeTab: string = 'general';
  public showSubclass: boolean = false;
  public data: any = {};
  public name = sessionStorage.getItem('characterName');
  public subclassLabel: string = '';

  public constructor(
    public dataAccessor: DataService,
    private classAccessor: ClassService,
  ) {}

  ngOnInit(): void {
    this.data = this.dataAccessor.characterData;
    this.isLevelSufficientForSubclass();
  }

  /**
   * Calculates if the level is sufficient for showing the subclass tab.
   * @returns boolean Returns true if the level is sufficient for the subclass.
   */
  private isLevelSufficientForSubclass(): void {
    var className = this.data.class;
    var level = this.data.level;
    var necessaryLevelForSubclass =
      this.classAccessor.getClassDetails(className).subclassLevel;
    this.showSubclass = level >= necessaryLevelForSubclass;
  }

  public updateData(): void {
    this.isLevelSufficientForSubclass();
    this.dataAccessor.characterData = this.data;
  }
}
