import { Component } from '@angular/core';
import { SubclassService } from 'src/services/subclass/subclass.service';
import { DataService } from 'src/services/data/data.service';
import { ModalService } from 'src/services/modal/modal.service';
import { Ability, Trait } from 'src/interfaces/interfaces';
import { AbilityModalComponent } from '../../journal-stats/ability-panel/ability-table/ability-modal/ability-modal.component';
import { TraitModalComponent } from '../../journal-stats/ability-panel/trait-table/trait-modal/trait-modal.component';

@Component({
  selector: 'subclass',
  templateUrl: './subclass.component.html',
  styleUrl: './subclass.component.scss',
})
export class SubclassComponent {
  public subclassName: string = 'echoKnight';
  public subclass: any;
  public className: string = '';
  public availableSubclasses: string[] = [];
  public data: any;

  public constructor(
    private SubclassService: SubclassService,
    private dataAccessor: DataService,
    private modalAccessor: ModalService,
  ) {
    this.data = this.dataAccessor.characterData;
    this.className = this.data.class;
    this.loadSubclassData();
  }

  private loadSubclassData(): void {
    this.subclassName = this.data.subclass;
    if (this.subclassName) {
      this.subclass = this.SubclassService.getSubclassDetails(
        this.subclassName,
      );
    } else {
      this.availableSubclasses = this.SubclassService.getAvailableSubclassNames(
        this.className,
      );
    }
  }

  /**
   * Updates the character data in the data service and implicitly in the database.
   */
  updateData(): void {
    this.dataAccessor.characterData = this.data;
    this.loadSubclassData();
  }

  public addFeature(feature: any) {
    if ('ability' in feature) {
      this.addAbility(feature);
    } else {
      this.addTrait(feature);
    }
  }

  private addAbility(feature: any) {
    let index = feature.name.indexOf('Optional');
    if (index > -1) {
      feature.name = feature.name.slice(index, 8);
    }
    let ability: Ability = {
      name: feature.name,
      shortDescription: '',
      longDescription: feature.description,
      cost: '',
      charges: 0,
      currentlyUsedCharges: 0,
    };
    this.modalAccessor.openModal(AbilityModalComponent, {
      ability: ability,
      isUpdate: true,
      isAddedFromCharacter: true,
    });
    const resultSubscription = this.modalAccessor.result$.subscribe(
      (result) => {
        if (result.state === 'update' || result.state === 'add') {
          let ability = this.dataAccessor.abilities;
          ability.push(result.data);
          this.dataAccessor.abilities = ability;
        }
        resultSubscription.unsubscribe();
      },
    );
  }

  private addTrait(feature: any) {
    let index = feature.name.indexOf('Optional');
    if (index > -1) {
      feature.name = feature.name.slice(index, 8);
    }
    let trait: Trait = {
      name: feature.name,
      shortDescription: '',
      longDescription: feature.description,
      origin: 'Class',
    };
    this.modalAccessor.openModal(TraitModalComponent, {
      trait: trait,
      isUpdate: true,
      isAddedFromCharacter: true,
    });
    const resultSubscription = this.modalAccessor.result$.subscribe(
      (result) => {
        if (result.state === 'update' || result.state === 'add') {
          let traits = this.dataAccessor.traits;
          traits.push(result.data);
          this.dataAccessor.traits = traits;
        }
        resultSubscription.unsubscribe();
      },
    );
  }
}
