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

@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, proficiency: false };
  public skillModifier: string = '0';
  public skillProficiency: boolean = false;
  private proficiencyBonus: number = 0;

  public constructor(
    public dataAccessor: DataService,
    public detailsAccessor: DetailsService,
  ) {}

  public skillNames: any = {
    acrobatics: { skill: 'Akrobatik', attribute: 'GES', long: 'dexterity' },
    animalHandling: { skill: 'Tierkunde', attribute: 'WIS', long: 'wisdom' },
    arcana: { skill: 'Arkana', attribute: 'INT', long: 'intelligence' },
    athletics: { skill: 'Athletik', attribute: 'STR', long: 'strength' },
    deception: { skill: 'Täuschen', attribute: 'CHA', long: 'charisma' },
    history: { skill: 'Geschichte', attribute: 'INT', long: 'intelligence' },
    insight: { skill: 'Motiv erkennen', attribute: 'WIS', long: 'wisdom' },
    intimidation: {
      skill: 'Einschüchtern',
      attribute: 'CHA',
      long: 'charisma',
    },
    investigation: {
      skill: 'Nachforschung',
      attribute: 'INT',
      long: 'intelligence',
    },
    medicine: { skill: 'Heilkunde', attribute: 'WIS', long: 'wisdom' },
    nature: { skill: 'Naturkunde', attribute: 'INT', long: 'intelligence' },
    perception: { skill: 'Wahrnehmung', attribute: 'WIS', long: 'wisdom' },
    performance: { skill: 'Auftreten', attribute: 'CHA', long: 'charisma' },
    persuasion: { skill: 'Überzeugen', 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: 'Überlebenskunst', 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;
      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();
    }
  }

  public openDetails(): void {
    this.detailsAccessor.openPanel(SkillDetailsComponent, {
      skillName: this.skillName,
      skillModifier: this.skillModifier,
    });
  }
}