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

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

  public active: number = 1;

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

  public ngOnInit(): void {
    this.dataAccessor.hitPoints$.subscribe((hitPoints: any) => {
      this.maxHitPoints = hitPoints.maxHitPoints;
      this.currentHitPoints = hitPoints.currentHitPoints;
      this.temporaryHitPoints = hitPoints.temporaryHitPoints;
    });
  }

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

  public updateHitPointsDatabas(): void {
    this.dataAccessor.updateHitPoints({
      maxHitPoints: this.maxHitPoints,
      currentHitPoints: this.currentHitPoints,
      temporaryHitPoints: this.temporaryHitPoints,
    });
  }
}
