import { Component, Input } from '@angular/core';
import { ModalService } from 'src/services/modal/modal.service';
import { Ability } from 'src/interfaces/ability';

@Component({
  selector: 'ability-modal',
  templateUrl: './ability-modal.component.html',
  styleUrls: ['./ability-modal.component.scss'],
})
export class AbilityModalComponent {
  @Input() public isUpdate: boolean = false;
  @Input() public ability: Ability | undefined;

  public name: string = '';
  public charges: number = 0;
  public currentlyUsedCharges: number = 0;
  public cost: string = 'none';
  public shortDescription: string = '';
  public longDescription: string = '';
  public costs: any[] = [
    { display: 'keine', value: 'none' },
    { display: 'Aktion', value: 'action' },
    { display: 'Bonusaktion', value: 'bonus' },
    { display: 'Reaktion', value: 'reaction' },
  ];

  public chargesTranslator: any[] = [
    { display: 'unbegrenzt', value: 0 },
    { display: '1', value: 1 },
    { display: '2', value: 2 },
    { display: '3', value: 3 },
    { display: '4', value: 4 },
    { display: '5', value: 5 },
    { display: '6', value: 6 },
    { display: '7', value: 7 },
    { display: '8', value: 8 },
    { display: '9', value: 9 },
    { display: '10', value: 10 },
  ];

  public constructor(private modalAccessor: ModalService) {}

  public ngOnInit(): void {
    if (this.isUpdate) {
      this.loadItem();
    }
  }

  //   FUNCTIONS

  public loadItem(): void {
    this.name = this.ability!.name;
    this.charges = this.ability!.charges;
    this.currentlyUsedCharges = this.ability!.currentlyUsedCharges;
    this.cost = this.ability!.cost;
    this.shortDescription = this.ability!.shortDescription;
    this.longDescription = this.ability!.longDescription;
  }

  public createAbility(): Ability {
    return {
      name: this.name,
      shortDescription: this.shortDescription,
      longDescription: this.longDescription,
      cost: this.cost,
      charges: parseInt(this.charges.toString()),
      currentlyUsedCharges: Math.min(this.currentlyUsedCharges, this.charges),
    };
  }

  // RESPONSES

  public cancel(): void {
    this.modalAccessor.handleModalClosing('cancel', undefined);
  }

  public add(): void {
    this.modalAccessor.handleModalClosing('add', this.createAbility());
  }

  public update(): void {
    this.modalAccessor.handleModalClosing('update', this.createAbility());
  }
}
