import { Component } from '@angular/core';
import { Ability, Trait } from 'src/interfaces/interfaces';
import { DataService } from 'src/services/data/data.service';
import { ModalService } from 'src/services/modal/modal.service';
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';
import { ClassService } from 'src/services/class/class.service';
import { SubclassService } from 'src/services/subclass/subclass.service';

@Component({
  selector: 'combined',
  templateUrl: './combined.component.html',
  styleUrl: './combined.component.scss',
})
export class CombinedComponent {
  public combined: any[] = [];

  public constructor(
    private dataAccessor: DataService,
    private modalAccessor: ModalService,
    private classAccessor: ClassService,
    private subclassAccessor: SubclassService,
  ) {
    this.getCombinedData();
  }

  /**
   * Retrieves the class and subclass features and combines them into a single array
   */
  private getCombinedData() {
    let className = this.dataAccessor.characterData.class;
    let subclassName = this.dataAccessor.characterData.subclass;
    let classFeatures = JSON.parse(
      JSON.stringify(this.classAccessor.getClassDetails(className).features),
    );
    let subclassFeatures = JSON.parse(
      JSON.stringify(
        this.subclassAccessor.getSubclassDetails(subclassName).features,
      ),
    );

    // Builds the array from deep copies of the class and subclass features.
    while (classFeatures.length > 0 && subclassFeatures.length > 0) {
      if (classFeatures[0].level < subclassFeatures[0].level) {
        this.combined.push(classFeatures.shift());
      } else {
        this.combined.push(subclassFeatures.shift());
      }
    }
    if (classFeatures.length > 0) {
      this.combined.push(...classFeatures);
    } else {
      this.combined.push(...subclassFeatures);
    }
  }

  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();
      },
    );
  }
}
