import { Component } from '@angular/core';
import { DataService } from 'src/services/data/data.service';
import { Router } from '@angular/router';
import { ListService } from 'src/services/list/list.service';
import { TranslateService } from '@ngx-translate/core';

interface characterData {
  view: string;
  value: string;
}

@Component({
  selector: 'app-character-creator',
  templateUrl: './character-creator.component.html',
  styleUrls: ['./character-creator.component.scss'],
})
export class CharacterCreatorComponent {
  selectedValue: string = '';

  // Predefined Data

  private hitDice: any = {
    barbarian: 12,
    bard: 8,
    cleric: 8,
    druid: 8,
    fighter: 10,
    monk: 8,
    paladin: 10,
    ranger: 10,
    rogue: 8,
    sorcerer: 6,
    warlock: 8,
    wizard: 6,
  };

  public spellcastingAttributes: any = {
    barbarian: null,
    bard: 'charisma',
    cleric: 'wisdom',
    druid: 'wisdom',
    fighter: null,
    monk: 'wisdom',
    paladin: 'charisma',
    ranger: 'wisdom',
    rogue: 'intelligence',
    sorcerer: 'charisma',
    warlock: 'charisma',
    wizard: 'intelligence',
  };

  public characterName: string = '';
  public characterClass: string = '';
  public characterSpecies: string = '';
  public characterBackground: string = '';
  public characterGender: string = '';

  public constructor(
    public dataAccessor: DataService,
    private Router: Router,
    public list: ListService,
    public translate: TranslateService,
  ) {}

  public async createCharacter(): Promise<void> {
    // Creates a new entry in the character collection
    this.dataAccessor.addData('characters', { name: this.characterName });
    // Creates a new collection with the character name
    this.createNewCharacterInDatabase().then(() => {
      // Die Funktion muss ertstmal durchlaufen, bevor der Character ausgewählt werden kann
      sessionStorage.setItem('characterName', this.characterName);
      this.dataAccessor.dataLoaded = false;
      this.Router.navigate(['journal']);
    });
  }

  public async createNewCharacterInDatabase(): Promise<void> {
    // TODO: Für alle Daten einen eigenen Key/Value Eintrag anlegen addData(collection: string, data: any, key?: string): void
    return Promise.all([
      // Character Data
      this.dataAccessor.addData(
        this.characterName,
        {
          class: this.characterClass,
          subclass: '',
          race: this.characterSpecies,
          background: this.characterBackground,
          backgroundStory: '',
          level: 1,
          experience: 0,
          image: '',
          gender: this.characterGender,
          age: '',
          height: '',
          weight: '',
          eyes: '',
          skin: '',
          hair: '',
        },
        'characterData',
      ),
      // Character Image
      this.dataAccessor.addData(
        this.characterName,
        {
          value: undefined,
        },
        'image',
      ),
      // Character Attributes
      this.dataAccessor.addData(
        this.characterName,
        {
          strength: { name: 'strength', value: 10, proficiency: false },
          dexterity: { name: 'dexterity', value: 10, proficiency: false },
          constitution: { name: 'constitution', value: 10, proficiency: false },
          intelligence: { name: 'intelligence', value: 10, proficiency: false },
          wisdom: { name: 'wisdom', value: 10, proficiency: false },
          charisma: { name: 'charisma', value: 10, proficiency: false },
        },
        'attributes',
      ),
      // Character Skills
      this.dataAccessor.addData(
        this.characterName,
        {
          acrobatics: { name: 'acrobatics', proficiency: false },
          animalHandling: { name: 'animalHandling', proficiency: false },
          arcana: { name: 'arcana', proficiency: false },
          athletics: { name: 'athletics', proficiency: false },
          deception: { name: 'deception', proficiency: false },
          history: { name: 'history', proficiency: false },
          insight: { name: 'insight', proficiency: false },
          intimidation: { name: 'intimidation', proficiency: false },
          investigation: { name: 'investigation', proficiency: false },
          medicine: { name: 'medicine', proficiency: false },
          nature: { name: 'nature', proficiency: false },
          perception: { name: 'perception', proficiency: false },
          performance: { name: 'performance', proficiency: false },
          persuasion: { name: 'persuasion', proficiency: false },
          religion: { name: 'religion', proficiency: false },
          sleightOfHand: { name: 'sleightOfHand', proficiency: false },
          stealth: { name: 'stealth', proficiency: false },
          survival: { name: 'survival', proficiency: false },
        },
        'skills',
      ),
      // Character Combat Stats
      this.dataAccessor.addData(
        this.characterName,
        {
          armorClass: 10,
          initiative: 0,
          movement: 30,
          deathSaves: [0, 0],
          proficiencyBonus: 2,
          conditions: [],
          exhaustion: 0,
          inspiration: false,
        },
        'combatStats',
      ),
      //  Character Hit Points
      this.dataAccessor.addData(
        this.characterName,
        {
          hitPoints: {
            maxHitPoints: 10,
            currentHitPoints: 10,
            temporaryHitPoints: 0,
          },
          hitDice: {
            diceNumber: 1,
            diceType: this.hitDice[this.characterClass],
            diceUsed: 0,
          },
        },
        'hitPoints',
      ),

      // Character Abilities
      this.dataAccessor.addData(
        this.characterName,
        {
          data: [],
        },
        'abilities',
      ),

      // Character Traits
      this.dataAccessor.addData(
        this.characterName,
        {
          data: [],
        },
        'traits',
      ),

      // Character Proficiencies
      this.dataAccessor.addData(
        this.characterName,
        {
          light: false,
          medium: false,
          heavy: false,
          shields: false,
          simple: false,
          martial: false,
          other: [],
          tools: [],
          languages: ['common'],
        },
        'proficiencies',
      ),

      // Character Spellslots
      this.dataAccessor.addData(
        this.characterName,
        {
          spellslots: [],
          showSpellslots: false,
          spellcastingAttribute:
            this.spellcastingAttributes[this.characterClass],
        },
        'spellslots',
      ),
      // Ki Points
      this.dataAccessor.addData(
        this.characterName,
        {
          totalPoints: 0,
          usedPoints: 0,
          showKiPoints: false,
        },
        'kiPoints',
      ),
      // Weapons
      this.dataAccessor.addData(
        this.characterName,
        {
          data: [],
        },
        'favoriteWeapons',
      ),
      // WEAPONS AND ARMOR
      this.dataAccessor.addData(
        this.characterName,
        {
          data: [],
        },
        'weaponsAndArmor',
      ),
      // MISCELLANEOUS
      this.dataAccessor.addData(
        this.characterName,
        {
          data: [],
        },
        'miscellaneous',
      ),
      // CONSUMABLES
      this.dataAccessor.addData(
        this.characterName,
        {
          data: [],
        },
        'consumables',
      ),
      // MONEY
      this.dataAccessor.addData(
        this.characterName,
        {
          platinum: 0,
          gold: 0,
          silver: 0,
          copper: 0,
        },
        'money',
      ),
      // FOOD
      this.dataAccessor.addData(
        this.characterName,
        {
          data: [],
        },
        'food',
      ),
      // Favorite Spells
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'favoriteSpells',
      ),
      // Spells

      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
          id: 10000,
        },
        'customSpells',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel0',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel1',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel2',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel3',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel4',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel5',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel6',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel7',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel8',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          spells: [],
        },
        'spellLevel9',
      ),

      this.dataAccessor.addData(
        this.characterName,
        {
          data: {},
        },
        'companions',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          data: {},
        },
        'notes',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          data: {},
        },
        'quests',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          data: {},
        },
        'npcs',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          data: {},
        },
        'locations',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          data: {},
        },
        'maps',
      ),
      this.dataAccessor.addData(
        this.characterName,
        {
          data: {
            resistances: [],
            immunities: [],
            vulnerabilities: [],
          },
        },
        'resistances',
      ),

      // Notes

      // Maps

      // NPCs

      // Quests
    ]).then(() => {});
  }
}
