import { Component, Input } from '@angular/core';
import { DetailsService } from 'src/services/details/details.service';

@Component({
  selector: 'app-life-details',
  templateUrl: './life-details.component.html',
  styleUrls: ['./life-details.component.scss'],
})
export class LifeDetailsComponent {
  public constructor(public detailsAccessor: DetailsService) {}

  @Input() lifeData: any;
  private hitDice: any;

  public maxHitPoints: number = 0;
  public currentHitPoints: number = 0;
  public temporaryHitPoints: number = 0;

  public ngOnInit(): void {
    this.maxHitPoints = this.lifeData.maxHitPoints;
    this.currentHitPoints = this.lifeData.currentHitPoints;
    this.temporaryHitPoints = this.lifeData.temporaryHitPoints;
  }

  public checkValidity(): void {
    if (this.currentHitPoints > this.maxHitPoints) {
      this.currentHitPoints = this.maxHitPoints;
    }
  }

  public close(result: string): void {
    if (result === 'update') {
      this.detailsAccessor.closePanel(result, {
        maxHitPoints: this.maxHitPoints,
        currentHitPoints: this.currentHitPoints,
        temporaryHitPoints: this.temporaryHitPoints,
        hitDice: this.hitDice,
      });
    } else if (result === 'cancel') {
      this.detailsAccessor.closePanel(result);
    }
  }

  public setHitDice(hitDiceData: any): void {
    console.log('HITDICE IN DETAILS: ', hitDiceData);

    this.hitDice = hitDiceData;
  }
}
