import { Component, Input, Output, EventEmitter } from '@angular/core';
import { DataService } from 'src/services/data/data.service';
import { Spell } from 'src/interfaces/spell';
import { SpellsService } from 'src/services/spells/spells.service';

@Component({
  selector: 'add-card',
  templateUrl: './add-card.component.html',
  styleUrl: './add-card.component.scss',
})
export class AddCardComponent {
  @Input() level!: number;
  @Input() alreadyUsedSpells!: any[];

  @Output() createNewSpell = new EventEmitter<any>();
  @Output() createNewSpellFromOfficial = new EventEmitter<any>();
  @Output() onSpellSelected = new EventEmitter<any>();

  public newSpellName: string = '';
  private allAvailableSpells: any[] = [];
  public availableSpells: any[] = [];
  public isModification: boolean | undefined;
  public state: number = 1;

  public constructor(
    private spellsAccessor: SpellsService,
    private dataAccessor: DataService
  ) {}

  public ngOnInit(): void {
    this.allAvailableSpells = this.spellsAccessor.getAvailableSpells(
      this.level,
      this.dataAccessor.characterData.class
    );
    this.filterSpellArray();
    this.spellsAccessor.closeSubject$.subscribe((level) => {
      if (level !== this.level) {
        this.resetThis();
      }
    });
  }

  public emitNewSpell(): void {
    console.warn('Emitting new spell to create: ', this.level);
    this.createNewSpell.emit(this.level);
    this.resetThis();
  }

  public emitNewSpellFromOfficial(spell: Spell): void {
    console.warn('Emitting new spell to modify from official: ', spell);

    this.createNewSpellFromOfficial.emit(spell);
    this.resetThis();
  }

  public continueToSpellSelection(modify: boolean): void {
    this.allAvailableSpells = this.spellsAccessor.getAvailableSpells(
      this.level,
      this.dataAccessor.characterData.class
    );
    this.filterSpellArray();
    this.isModification = modify;
    if (modify) {
      this.filterSpellArrayForModification();
    } else {
      this.filterSpellArray();
    }
    this.spellsAccessor.closeAllOthers(this.level);
  }

  public spellSelected(spell: any): void {
    this.onSpellSelected.emit(spell);
    this.resetThis();
  }

  public resetThis(): void {
    this.newSpellName = '';
    this.state = 1;
    this.isModification = undefined;
  }

  public filterSpellArray(): void {
    if (this.newSpellName.length >= 1) {
      this.availableSpells = this.allAvailableSpells.filter(
        (spell) =>
          (spell.german
            .toLowerCase()
            .includes(this.newSpellName.toLowerCase()) ||
            spell.english
              .toLowerCase()
              .includes(this.newSpellName.toLowerCase())) &&
          !this.alreadyUsedSpells.includes(spell.id)
      );
    } else {
      this.availableSpells = this.allAvailableSpells.filter(
        (spell) => !this.alreadyUsedSpells.includes(spell.id)
      );
    }
  }

  public filterSpellArrayForModification(): void {
    if (this.newSpellName.length >= 1) {
      this.availableSpells = this.allAvailableSpells.filter(
        (spell) =>
          spell.german
            .toLowerCase()
            .includes(this.newSpellName.toLowerCase()) ||
          spell.english.toLowerCase().includes(this.newSpellName.toLowerCase())
      );
    } else {
      this.availableSpells = this.allAvailableSpells;
    }
  }
}
