import { Component } from '@angular/core';
import { DataService } from 'src/services/data/data.service';
import { DetailsService } from 'src/services/details/details.service';
import { ConditionsDetailsComponent } from './conditions-details/conditions-details.component';
import { ExhaustionDetailsComponent } from './exhaustion-details/exhaustion-details.component';

@Component({
  selector: 'conditions',
  templateUrl: './conditions.component.html',
  styleUrl: './conditions.component.scss',
})
export class ConditionsComponent {
  public constructor(
    private dataAccessor: DataService,
    public detailsAccessor: DetailsService,
  ) {}

  public exhaustion: number = 0;
  public conditions: string[] = [];

  public availableConditions: string[] = [
    'Betäubt',
    'Bewusstlos',
    'Bezaubert',
    'Blind',
    'Festgesetzt',
    'Gelähmt',
    'Gepackt',
    'Kampfunfähig',
    'Liegend',
    'Taub',
    'Unsichtbar',
    'Verängstigt',
    'Vergiftet',
    'Versteinert',
  ];

  public ngOnInit(): void {
    this.conditions = this.dataAccessor.conditions;
    this.exhaustion = this.dataAccessor.exhaustion;
  }

  public updateExhaustion(): void {
    this.dataAccessor.exhaustion = this.exhaustion;
  }

  public updateCondition(): void {
    this.dataAccessor.conditions = this.conditions;
  }

  public openConditionDetails(): void {
    this.detailsAccessor.openPanel(ConditionsDetailsComponent, {
      conditions: JSON.parse(JSON.stringify(this.conditions)),
    });
    const resultSubscription = this.detailsAccessor.result$.subscribe(
      (result) => {
        if (result.state === 'update') {
          this.conditions = result.data;
          this.dataAccessor.conditions = this.conditions;
        }
        resultSubscription.unsubscribe();
      },
    );
  }

  public openExhaustionDetails(): void {
    this.detailsAccessor.openPanel(ExhaustionDetailsComponent, {
      exhaustion: this.exhaustion,
    });
  }
}
