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

@Component({
  selector: 'death-save',
  templateUrl: './death-save.component.html',
  styleUrls: ['./death-save.component.scss'],
})
export class DeathSaveComponent {
  public constructor(private dataAccessor: DataService) {}

  public deathSave: number[] = [0, 0];

  public deathSaveSucc1: boolean = false;
  public deathSaveSucc2: boolean = false;
  public deathSaveSucc3: boolean = false;
  public deathSaveFail1: boolean = false;
  public deathSaveFail2: boolean = false;
  public deathSaveFail3: boolean = false;

  ngOnInit(): void {
    this.deathSave = this.dataAccessor.deathSaves;
    this.displayDeathSaves();
  }

  public updateValue(): void {
    let successCount = 0;
    let failCount = 0;

    if (this.deathSaveSucc1) {
      successCount++;
    }
    if (this.deathSaveSucc2) {
      successCount++;
    }
    if (this.deathSaveSucc3) {
      successCount++;
    }
    if (this.deathSaveFail1) {
      failCount++;
    }
    if (this.deathSaveFail2) {
      failCount++;
    }
    if (this.deathSaveFail3) {
      failCount++;
    }

    this.deathSave = [successCount, failCount];
    this.dataAccessor.deathSaves = this.deathSave;
    this.displayDeathSaves();
  }

  public displayDeathSaves(): void {
    setTimeout(() => {
      if (this.deathSave[0] == 0) {
        this.deathSaveSucc1 = false;
        this.deathSaveSucc2 = false;
        this.deathSaveSucc3 = false;
      } else if (this.deathSave[0] == 1) {
        this.deathSaveSucc1 = true;
        this.deathSaveSucc2 = false;
        this.deathSaveSucc3 = false;
      } else if (this.deathSave[0] == 2) {
        this.deathSaveSucc1 = true;
        this.deathSaveSucc2 = true;
        this.deathSaveSucc3 = false;
      } else if (this.deathSave[0] == 3) {
        this.deathSaveSucc1 = true;
        this.deathSaveSucc2 = true;
        this.deathSaveSucc3 = true;
      }
      if (this.deathSave[1] == 0) {
        this.deathSaveFail1 = false;
        this.deathSaveFail2 = false;
        this.deathSaveFail3 = false;
      } else if (this.deathSave[1] == 1) {
        this.deathSaveFail1 = true;
        this.deathSaveFail2 = false;
        this.deathSaveFail3 = false;
      } else if (this.deathSave[1] == 2) {
        this.deathSaveFail1 = true;
        this.deathSaveFail2 = true;
        this.deathSaveFail3 = false;
      } else if (this.deathSave[1] == 3) {
        this.deathSaveFail1 = true;
        this.deathSaveFail2 = true;
        this.deathSaveFail3 = true;
      }
    });
  }
}
