import { Component, ViewChild } from '@angular/core';
import { DataService } from 'src/services/data/data.service';
import { Weapon } from 'src/interfaces/weapon';
import { Spell } from 'src/interfaces/spell';
import { Damage } from 'src/interfaces/damage';
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
import { NgxSmartModalService, NgxSmartModalComponent } from 'ngx-smart-modal';

@Component({
  selector: 'app-weapons-container',
  templateUrl: './weapons-container.component.html',
  styleUrls: ['./weapons-container.component.scss'],
})
export class WeaponsContainerComponent {
  public constructor(
    public dataAccessor: DataService,
    public ngxSmartModalService: NgxSmartModalService
  ) {}

  public active: number = 1;

  public newWeaponName: string = '';
  public newWeaponDamageType: string = '';
  public newWeaponRange: string = '';
  public newWeaponAttackBonus: string = '';

  public newWeaponDamage: Damage[] = [{ damage: '', damageType: '' }];

  public newWeaponProficient: boolean = false;
  public newWeaponAttribute: string = '';
  public newWeaponIsVersatile: boolean = false;
  public newWeaponIsTwoHanded: boolean = false;
  public newWeaponIsFinesse: boolean = false;
  public newWeaponIsRanged: boolean = false;
  public newWeaponVersatileDamage: Damage[] = [{ damage: '', damageType: '' }];

  // public weaponAttributes: string[] = [
  //   'Strength',
  //   'Dexterity',
  //   'Constitution',
  //   'Intelligence',
  //   'Wisdom',
  //   'Charisma',
  // ];

  public damageTypes: string[] = [
    'Wucht',
    'Stich',
    'Hieb',
    'Feuer',
    'Kälte',
    'Blitz',
    'Gift',
    'Säure',
    'Nekrotisch',
    'Psychisch',
    'Heilig',
    'Göttlich',
    'Kraft',
  ];

  public weapons: Weapon[] = [
    {
      name: 'Dagger',
      damage: [{ damage: '1d4', damageType: 'Stich' }],
      attackBonus: '+5',
      range: '20/60',
      isFinesse: true,
      proficient: true,
      isTwoHanded: false,
      isVersatile: false,
      isRanged: false,
    },
  ];

  public newSpellName: string = '';
  public newSpellAttackBonus: string = '';
  public newSpellDamage: string = '';
  public newSpellType: string = '';
  public newSpellRange: string = '';
  public newSpellProficient: boolean = false;
  public newSpellAttribute: string = '';

  public spells: Spell[] = [
    {
      name: 'Fireball',
      damage: '8d6',
      attackBonus: '0',
      range: '150',
      type: 'Fire',
      attribute: 'Dexterity',
      level: 3,
    },
  ];

  public ngOnInit(): void {
    this.weapons = this.dataAccessor.getWeapons();
    this.spells = this.dataAccessor.getSpells();
  }

  public dropWeapons(event: CdkDragDrop<string[]>): void {
    moveItemInArray(this.weapons, event.previousIndex, event.currentIndex);
    this.updateWeapons();
  }

  public dropSpells(event: CdkDragDrop<string[]>): void {
    moveItemInArray(this.spells, event.previousIndex, event.currentIndex);
    this.updateSpells();
  }

  public updateWeapons(): void {
    this.dataAccessor.setWeapons(this.weapons);
  }

  public updateSpells(): void {
    this.dataAccessor.setSpells(this.spells);
  }

  public openWeaponModal(): void {
    this.ngxSmartModalService.getModal('weaponModal').open();
  }

  public addWeapon(): void {
    const newWeapon: Weapon = {
      name: this.newWeaponName,
      damage: this.newWeaponDamage,
      attackBonus: this.newWeaponAttackBonus,
      range: this.newWeaponRange,
      proficient: this.newWeaponProficient,
      isTwoHanded: this.newWeaponIsTwoHanded,
      isVersatile: this.newWeaponIsVersatile,
      isFinesse: this.newWeaponIsFinesse,
      isRanged: this.newWeaponIsRanged,
      versatileDamage: this.newWeaponIsVersatile
        ? this.newWeaponVersatileDamage
        : undefined,
    };

    console.log('test if versatileDamage is present?', newWeapon);

    this.weapons.push(newWeapon);
    this.ngxSmartModalService.getModal('weaponModal').close();
  }

  public openSpellModal(): void {
    this.ngxSmartModalService.getModal('spellModal').open();
  }

  public addSpell(): void {
    this.spells.push({
      name: this.newSpellName,
      damage: this.newSpellDamage,
      attackBonus: this.newSpellAttackBonus,
      range: this.newSpellRange,
      type: this.newSpellType,
      level: 0,
      attribute: this.newSpellAttribute,
    });
    this.ngxSmartModalService.getModal('spellModal').close();
  }
}
