import { Component, Input } from '@angular/core';
import { Weapon } from 'src/interfaces/weapon';
import { Damage } from 'src/interfaces/damage';
import { ModalService } from 'src/services/modal/modal.service';

@Component({
  selector: 'app-weapon-modal',
  templateUrl: './weapon-modal.component.html',
  styleUrl: './weapon-modal.component.scss',
})
export class WeaponModalComponent {
  public constructor(private modalAccessor: ModalService) {}
  @Input() public item: any;
  @Input() public isUpdate: boolean = false;

  public name: string = '';
  public range: number[] = [5, 5];
  public throwRange: number[] = [5, 5];
  public attackBonus: string = '+0';
  public damage: Damage[] = [{ diceNumber: '', diceType: '', damageType: '' }];
  public proficient: boolean = false;
  public isVersatile: boolean = false;
  public isTwoHanded: boolean = false;
  public isFinesse: boolean = false;
  public isRanged: boolean = false;
  public versatileDamage: string = '';
  public canBeThrown: boolean = false;
  public weight: string = 'normal';
  public isMagical: boolean = false;
  public magicBonus: number = 0;

  // Options for the select boxes
  public weights: string[] = ['leicht', 'normal', 'schwer'];

  public damageTypes: any[] = [
    { display: 'Wucht', value: 'bludgeoning' },
    { display: 'Stich', value: 'piercing' },
    { display: 'Hieb', value: 'slashing' },
    { display: 'Feuer', value: 'fire' },
    { display: 'Kälte', value: 'cold' },
    { display: 'Blitz', value: 'lightning' },
    { display: 'Gift', value: 'poison' },
    { display: 'Säure', value: 'acid' },
    { display: 'Nekrotisch', value: 'necrotic' },
    { display: 'Psychisch', value: 'psychic' },
    { display: 'Heilig', value: 'holy' },
    { display: 'Göttlich', value: 'divine' },
    { display: 'Kraft', value: 'force' },
  ];

  public dice: string[] = ['d4', 'd6', 'd8', 'd10', 'd12', 'd20', 'd100'];

  public numbers: string[] = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
  ];

  public attackBonuses: string[] = [
    '-5',
    '-4',
    '-3',
    '-2',
    '-1',
    '+0',
    '+1',
    '+2',
    '+3',
    '+4',
    '+5',
    '+6',
    '+7',
    '+8',
    '+9',
    '+10',
    '+11',
    '+12',
    '+13',
    '+14',
    '+15',
    '+16',
    '+17',
    '+18',
    '+19',
    '+20',
  ];

  public magicBonuses: any[] = [
    { display: '+1', value: 1 },
    { display: '+2', value: 2 },
    { display: '+3', value: 3 },
    { display: '+4', value: 4 },
    { display: '+5', value: 5 },
  ];

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

  public loadItem(): void {
    this.name = this.item.name;
    this.range = this.item.range;
    this.throwRange = this.item.throwRange;
    this.attackBonus = this.item.attackBonus;
    this.damage = this.item.damage;
    this.proficient = this.item.proficient;
    this.isVersatile = this.item.isVersatile;
    this.isTwoHanded = this.item.isTwoHanded;
    this.isFinesse = this.item.isFinesse;
    this.isRanged = this.item.isRanged;
    this.versatileDamage = this.item.versatileDamage;
    this.canBeThrown = this.item.canBeThrown;
    this.weight = this.item.weight;
    this.isMagical = this.item.isMagical;
    this.magicBonus = this.item.magicBonus;
  }

  // RESPONSES

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

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

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

  public createItem(): Weapon {
    return {
      name: this.name,
      range: this.range,
      throwRange: this.throwRange,
      attackBonus: this.attackBonus,
      damage: this.damage,
      proficient: this.proficient,
      isVersatile: this.isVersatile,
      isTwoHanded: this.isTwoHanded,
      isFinesse: this.isFinesse,
      isRanged: this.isRanged,
      versatileDamage: this.versatileDamage,
      canBeThrown: this.canBeThrown,
      weight: this.weight,
      isMagical: this.isMagical,
      magicBonus: this.magicBonus,
    };
  }

  /**
   * Resets all values to their default values.
   */
  public resetItem(): void {
    this.name = '';
    this.range = [5, 5];
    this.throwRange = [5, 5];
    this.attackBonus = '+0';
    this.damage = [{ diceNumber: '', diceType: '', damageType: '' }];
    this.proficient = false;
    this.isVersatile = false;
    this.isTwoHanded = false;
    this.isFinesse = false;
    this.isRanged = false;
    this.versatileDamage = '';
    this.canBeThrown = false;
    this.weight = 'normal';
    this.isMagical = false;
    this.magicBonus = 0;
  }

  // COMPONENT LOGIC

  public addDamage(): void {
    this.damage.push({ diceNumber: '', diceType: '', damageType: '' });
  }

  public removeDamage(index: number): void {
    this.damage.splice(index, 1);
  }
}
