import { Component, Input } from '@angular/core';
import { Skill } from 'src/interfaces/skill';
import { DataService } from 'src/services/data/data.service';
import { Observable } from 'rxjs';
import { Attribute } from 'src/interfaces/attribute';

@Component({
  selector: 'app-skill-field',
  templateUrl: './skill-field.component.html',
  styleUrls: ['./skill-field.component.scss'],
})
export class SkillFieldComponent {
  @Input() skillName: string = '';
  public skill: Skill = { name: '', proficiency: false };
  private attribute: Attribute = { name: '', value: 0 };
  public skillModifier: string = '0';
  public skillProficiency: boolean = false;
  private proficiencyBonus: number = 0;

  public constructor(public dataAccessor: DataService) {}

  public skillNames: any = {
    acrobatics: { skill: 'Akrobatik', attribute: 'GES', long: 'dexterity' },
    animalHandling: { skill: 'Tierkunde', attribute: 'WIS', long: 'wisdom' },
    arcana: { skill: 'Arkanes Wissen', attribute: 'INT', long: 'intelligence' },
    athletics: { skill: 'Athletik', attribute: 'STR', long: 'strength' },
    deception: { skill: 'Täuschung', attribute: 'CHA', long: 'charisma' },
    history: { skill: 'Geschichte', attribute: 'INT', long: 'intelligence' },
    insight: { skill: 'Einsicht', attribute: 'WIS', long: 'wisdom' },
    intimidation: {
      skill: 'Einschüchterung',
      attribute: 'CHA',
      long: 'charisma',
    },
    investigation: {
      skill: 'Ermittlung',
      attribute: 'INT',
      long: 'intelligence',
    },
    medicine: { skill: 'Medizin', attribute: 'WIS', long: 'wisdom' },
    nature: { skill: 'Natur', attribute: 'INT', long: 'intelligence' },
    perception: { skill: 'Wahrnehmung', attribute: 'WIS', long: 'wisdom' },
    performance: { skill: 'Performance', attribute: 'CHA', long: 'charisma' },
    persuasion: { skill: 'Überredung', attribute: 'CHA', long: 'charisma' },
    religion: { skill: 'Religion', attribute: 'INT', long: 'intelligence' },
    sleightOfHand: {
      skill: 'Fingerfertigkeit',
      attribute: 'GES',
      long: 'dexterity',
    },
    stealth: { skill: 'Heimlichkeit', attribute: 'GES', long: 'dexterity' },
    survival: { skill: 'Überleben', attribute: 'WIS', long: 'wisdom' },
  };

  ngOnInit(): void {
    this.initProficiencySubscription();
    this.initAttributeSubscription();
    this.initSkillSubscription();
  }

  private initAttributeSubscription(): void {
    const observable: Observable<Attribute> = eval(
      `this.dataAccessor.${this.skillNames[this.skillName].long}$`
    );
    observable.subscribe((newValue: Attribute) => {
      this.attribute = newValue;
      this.skillModifier = this.calculateModifier();
    });
  }

  private initSkillSubscription(): void {
    const observable: Observable<Skill> = eval(
      `this.dataAccessor.${this.skillName}$`
    );
    observable.subscribe((newValue: Skill) => {
      this.skill = newValue;
      this.skillProficiency = this.skill.proficiency;
      this.skillModifier = this.calculateModifier();
    });
  }

  private initProficiencySubscription(): void {
    this.dataAccessor.proficiency$.subscribe((newValue: any) => {
      this.proficiencyBonus = newValue.value;
      this.skillModifier = this.calculateModifier();
    });
  }

  public updateValue(): void {
    this.dataAccessor.updateSkill({
      name: this.skillName,
      proficiency: this.skillProficiency,
    });
  }

  public calculateModifier(): string {
    let mod: number = Math.floor((this.attribute.value - 10) / 2);
    if (this.skillProficiency) {
      mod += this.proficiencyBonus;
    }
    if (mod > 0) {
      return '+' + mod;
    } else {
      return mod.toString();
    }
  }
}
