import { Component, Input } from '@angular/core';
import { ModalService } from 'src/services/modal/modal.service';
import { ListService } from 'src/services/list/list.service';
import { Damage, Heal, Spell } from 'src/interfaces/interfaces';
import { Editor } from 'ngx-editor';

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

  // #region Properties
  public name: string = '';
  public german: string = '';
  public english: string = '';
  public image: string = '';
  public cost:
    | 'action'
    | 'bonus'
    | 'reaction'
    | 'rounds'
    | 'minutes'
    | 'hours'
    | 'days' = 'action';
  public duration: number = 1;
  public durationtype:
    | 'instant'
    | 'rounds'
    | 'minutes'
    | 'hours'
    | 'days'
    | 'permanent' = 'instant';
  public timeToCast: number = 1;
  public isRitual: boolean = false;
  public needsConcentration: boolean = false;
  public needsVerbal: boolean = false;
  public needsSomatic: boolean = false;
  public needsMaterial: boolean = false;
  public school: string = 'evocation';
  public description_de: string = '';
  public description_en: string = '';
  public doesDamage: boolean = false;
  public doesHeal: boolean = false;
  public needsAttackRoll: boolean = false;
  public needsSavingThrow: boolean = false;
  public savingThrowAttribute: string = '';
  public heal: Heal = {
    diceNumber: 0,
    diceType: 0,
    additionalHeal: 0,
  };
  public attackBonus: string = '';
  public damage: Damage[] = [{ diceNumber: 0, diceType: 0, damageType: '' }];
  public isRanged: boolean = false;
  public range: number = 5;
  public hasAreaOfEffect: boolean = false;
  public length: number = 5;
  public areaOfEffectType: string = 'circle';

  active = 'description';
  editor: Editor = new Editor();
  html = '';
  toolbar: any = [
    // default value
    ['bold', 'italic'],
    ['bullet_list'],
    [{ heading: ['h3', 'h4', 'h5', 'h6'] }],
  ];
  // #endregion

  public costs: string[] = [
    'action',
    'bonus',
    'reaction',
    'rounds',
    'minutes',
    'hours',
    'days',
  ];

  // #endregion
  public constructor(
    private modalAccessor: ModalService,
    public list: ListService,
  ) {}

  public ngOnInit(): void {
    if (this.isModification || this.copyOfficalSpell) {
      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.english = this.spell.english;
      // TODO: Implement internationalization
      this.name = this.spell.german;
      this.classes = this.spell.classes;
    } else {
      this.german = this.spell.german + ' (Kopie)';
      this.english = this.spell.english + ' (copy)';
      this.name = this.spell.german + ' (Kopie)';
    }
    this.image = this.spell.image;
    this.classes = this.spell.classes;
    this.level = this.spell.level;
    this.cost = this.spell.cost;
    this.isRitual = this.spell.isRitual;
    this.duration = this.spell.duration;
    this.durationtype = this.spell.durationType;
    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.description_en = this.spell.description_en;
    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.length = this.spell.length;
    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.name,
      german: this.name,
      image: this.image,
      classes: this.classes,
      duration: this.duration,
      durationType: this.durationtype,
      timeToCast: this.timeToCast,
      damage: this.damage,
      heal: this.heal,
      level: parseInt(this.level.toString()),
      cost: this.cost,
      isRitual: this.isRitual,
      school: this.school,
      description_de: this.description_de,
      description_en: this.description_de,
      needsConcentration: this.needsConcentration,
      needsVerbal: this.needsVerbal,
      needsSomatic: this.needsSomatic,
      needsMaterial: this.needsMaterial,
      needsAttackRoll: this.needsSavingThrow ? false : this.needsAttackRoll,
      needsSavingThrow: this.needsSavingThrow,
      savingThrowAttribute: this.savingThrowAttribute,
      isRanged: this.isRanged,
      range: this.range,
      length: this.length,
      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.name = '';
    this.image = '';
    this.classes = [];
    this.level = 0;
    this.cost = 'action';
    this.isRitual = false;
    this.duration = 0;
    this.durationtype = 'rounds';
    this.timeToCast = 0;
    this.needsConcentration = false;
    this.needsVerbal = false;
    this.needsSomatic = false;
    this.needsMaterial = false;
    this.school = 'evocation';
    this.description_de = '';
    this.description_en = '';
    this.doesDamage = true;
    this.needsSavingThrow = false;
    this.savingThrowAttribute = '';
    this.damage = [{ diceNumber: 0, diceType: 0, damageType: '' }];
    this.isRanged = false;
    this.range = 5;
    this.hasAreaOfEffect = false;
    this.length = 5;
    this.areaOfEffectType = 'circle';
    this.needsAttackRoll = false;
    this.attackBonus = '';
    this.doesHeal = false;
    this.heal = { diceNumber: 0, diceType: 0, additionalHeal: 0 };
  }

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

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

  public checkWhichCostWasSelected(): void {
    if (
      this.cost === 'action' ||
      this.cost === 'bonus' ||
      this.cost === 'reaction'
    ) {
      this.timeToCast = 1;
    }
  }

  public unselectAttackRoll(): void {
    this.needsAttackRoll = false;
  }

  public checkIfInstant(): void {
    if (this.durationtype === 'instant' || this.durationtype === 'permanent') {
      this.duration = 1;
    }
  }
  // #endregion
}
