import { Component, Input } from '@angular/core';
import { ModalService } from 'src/services/modal/modal.service';
import { Damage } from 'src/interfaces/damage';
import { Heal } from 'src/interfaces/heal';
import { Spell } from 'src/interfaces/spell';

@Component({
  selector: 'spell-modal',
  templateUrl: './spell-modal.component.html',
  styleUrls: ['./spell-modal.component.scss'],
})
export class SpellModalComponent {
  public constructor(private modalAccessor: ModalService) {}
  @Input() public spell: any;
  @Input() public level: number = 0;
  @Input() classes: string[] = [];
  @Input() public id: number = 0;
  @Input() public isModification: boolean = false;
  @Input() public isBasedOnOfficialSpell: boolean = false;

  // #region Properties
  public german: string = '';
  public english: string = '';
  public cost: string = 'action';
  public duration: number = 0;
  public timeToCast: number = 0;
  public canRitual: 'true' | 'false' | 'only' = 'false';
  public needsConcentration: boolean = false;
  public needsVerbal: boolean = false;
  public needsSomatic: boolean = false;
  public needsMaterial: boolean = false;
  public school: string = '';
  public description_de: string = '';
  public doesDamage: boolean = false;
  public doesHeal: boolean = false;
  public needsAttackRoll: boolean = false;
  public needsSavingThrow: boolean = false;
  public savingThrowAttribute: string = '';
  public heal: Heal = {
    diceNumber: '',
    diceType: '',
    additionalHeal: 0,
  };
  public attackBonus: string = '';
  public damage: Damage[] = [{ diceNumber: '', diceType: '', damageType: '' }];
  public isRanged: boolean = false;
  public range: number = 5;
  public hasAreaOfEffect: boolean = false;
  public radius: number = 0;
  public areaOfEffectType: string = '';

  // #endregion

  // #region OPTIONS
  public areaTypes: any[] = [
    { display: 'Kegel', value: 'cone' },
    { display: 'Kugel', value: 'sphere' },
    { display: 'Kreis', value: 'circle' },
    { display: 'Linie', value: 'line' },
    { display: 'Quadrat', value: 'square' },
    { display: 'Würfel', value: 'cube' },
  ];

  public schools: any[] = [
    { display: 'Verwandlung', value: 'transmutation' },
    { display: 'Verzauberung', value: 'enchantment' },
    { display: 'Illusion', value: 'illusion' },
    { display: 'Nekromantie', value: 'necromancy' },
    { display: 'Beschwörung', value: 'conjuration' },
    { display: 'Hervorrufung', value: 'evocation' },
    { display: 'Bannmagie', value: 'abjuration' },
    { display: 'Wahrsagerei', value: 'divination' },
  ];

  public savingThrowAttributes: any[] = [
    { display: 'Stärke', value: 'strength' },
    { display: 'Geschicklichkeit', value: 'dexterity' },
    { display: 'Konstitution', value: 'constitution' },
    { display: 'Intelligenz', value: 'intelligence' },
    { display: 'Weisheit', value: 'wisdom' },
    { display: 'Charisma', value: 'charisma' },
  ];

  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 costs: any[] = [
    { display: 'Aktion', value: 'action' },
    { display: 'Bonusaktion', value: 'bonus action' },
    { display: 'Reaktion', value: 'reaction' },
  ];

  // #endregion

  public ngOnInit(): void {
    if (this.isModification || this.isBasedOnOfficialSpell) {
      this.loadspell();
    }
  }

  // #region RESPONSES

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

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

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

  // #endregion

  // #region FUNCTIONS

  private loadspell(): void {
    if (this.isModification) {
      this.id = this.spell.id;
      this.german = this.spell.german;
      this.classes = this.spell.classes;
    } else {
      this.german = this.spell.german + ' (Kopie)';
    }

    this.english = this.spell.english;
    this.level = this.spell.level;
    this.cost = this.spell.cost;
    this.canRitual = this.spell.canRitual;
    this.duration = this.spell.duration;
    this.timeToCast = this.spell.timeToCast;
    this.needsConcentration = this.spell.needsConcentration;
    this.needsVerbal = this.spell.needsVerbal;
    this.needsSomatic = this.spell.needsSomatic;
    this.needsMaterial = this.spell.needsMaterial;
    this.school = this.spell.school;
    this.description_de = this.spell.description_de;
    this.doesDamage = this.spell.doesDamage;
    this.needsSavingThrow = this.spell.needsSavingThrow;
    this.savingThrowAttribute = this.spell.savingThrowAttribute;
    this.damage = this.spell.damage;
    this.isRanged = this.spell.isRanged;
    this.range = this.spell.range;
    this.hasAreaOfEffect = this.spell.hasAreaOfEffect;
    this.radius = this.spell.radius;
    this.areaOfEffectType = this.spell.areaOfEffectType;
    this.needsAttackRoll = this.spell.needsAttackRoll;
    this.attackBonus = this.spell.attackBonus;
    this.doesHeal = this.spell.doesHeal;
    this.heal = this.spell.heal;
  }

  private createSpell(): Spell {
    const spell: Spell = {
      id: this.id,
      isCustom: true,
      english: this.english,
      german: this.german,
      classes: this.classes,
      duration: 0, // FIXME: only mocked
      timeToCast: 0, // FIXME: only mocked
      damage: this.damage,
      heal: this.heal,
      level: parseInt(this.level.toString()),
      cost: this.cost,
      canRitual: this.canRitual,
      school: this.school,
      description_de: this.description_de,
      needsConcentration: this.needsConcentration,
      needsVerbal: this.needsVerbal,
      needsSomatic: this.needsSomatic,
      needsMaterial: this.needsMaterial,
      needsAttackRoll: this.needsAttackRoll,
      needsSavingThrow: this.needsSavingThrow,
      savingThrowAttribute: this.savingThrowAttribute,
      isRanged: this.isRanged,
      range: this.range,
      radius: this.radius,
      hasAreaOfEffect: this.hasAreaOfEffect,
      areaOfEffectType: this.areaOfEffectType,
      doesDamage: this.doesDamage,
      doesHeal: this.doesHeal,
    };
    return spell;
  }

  private resetSpell(): void {
    this.id = 0;
    this.german = '';
    this.english = '';
    this.classes = [];
    this.level = 0;
    this.cost = 'action';
    this.canRitual = 'false';
    this.duration = 0;
    this.timeToCast = 0;
    this.needsConcentration = false;
    this.needsVerbal = false;
    this.needsSomatic = false;
    this.needsMaterial = false;
    this.school = '';
    this.description_de = '';
    this.doesDamage = true;
    this.needsSavingThrow = false;
    this.savingThrowAttribute = '';
    this.damage = [{ diceNumber: '', diceType: '', damageType: '' }];
    this.isRanged = false;
    this.range = 5;
    this.hasAreaOfEffect = false;
    this.radius = 0;
    this.areaOfEffectType = '';
    this.needsAttackRoll = false;
    this.attackBonus = '';
    this.doesHeal = false;
    this.heal = { diceNumber: '', diceType: '', additionalHeal: 0 };
  }

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

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

  // #endregion
}
