import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Spec, User, Character, Item, _Class, _Race, Class } from '../../../../../../backend/Types/Types';
import { getClassColor, specs } from '../../../../../../backend/Types/PlayerSpecs';
import { _Tiers } from '../../../../../../backend/Types/Items';
import { IApiService } from '../../services/ApiService/ApiService';
import { NbToastrService } from '@nebular/theme';

@Component({
  selector: 'character',
  templateUrl: './character.component.html',
})
export class FrontcraftCharacterComponent implements OnInit {

  @Input() name?: string
  @Input() link?: "owner" | "character" = 'owner'
  char: (Character & User & Spec) = {
    charactername: "Loading",
    race: 'Human',
    class: 'Warrior',
    spec: 'Arms'
  } as any

  classes = _Class
  races = _Race
  specs = specs[this.char.class]

  color: string
  tokens
  dataLink = ""
  boss1 = {}
  gear1: string[] = []

  boss2 = { name: undefined, date: undefined }
  gear2: Item[] = []

  canManage: boolean = false

  constructor(
    private api: IApiService,
    private route: ActivatedRoute,
    private toast: NbToastrService,
  ) { }

  async ngOnInit() {
    const param = this.name || this.route.snapshot.paramMap.get('name');
    if (!param) return

    const manageChar = this.api.get('manageCharacter')

    if (manageChar) this.canManage = true

    const char = await this.api.get('CharacterManager').getCharacterByName(param)
    if (char) {
      this.color = getClassColor(char.class)
      this.char = char
      this.specs = specs[this.char.class]
      this.api.get('ItemManager').getTokens(this.char, _Tiers, true).then(tokens => {
        this.tokens = tokens
      })

      /*
      this.dataLink = "https://classic.warcraftlogs.com:443/v1/parses/character/" + this.char.charactername.replace(/^\w/, c => c.toUpperCase()) + "/Gandling/EU?api_key=c698515ab4f592cdb848d80b3abe616c&metric=dps"

      const json: any[] = await fetch(this.dataLink).then(raw => raw.json())
      if (json.length < 1) return
      let data
      let min = -1

      for (let i = json.length - 1; i > 0; i--) {
        const start = Number.parseInt(json[i].startTime)
        if (start > min && json[i].gear[0].name != "Unknown Item") {
          min = start
          data = json[i]
        }
      }
      if (!data) return

      this.boss2 = {
        name: data.encounterName,
        date: data.startTime
      }

      const maybeItems: (Item | undefined)[] = await Promise.all(data.gear.filter(item => item.name != "Unknown Item").map(async item => await this.api.get('ItemManager').getItem(item.name)))
      const geardata = maybeItems.filter(maybeItem => maybeItem != null)
      this.gear2 = geardata
      */
    }
  }

  updateCharacter = async () => {
    const manager = this.api.get('manageCharacter')
    if (manager) {
      const specid = await this.api.get('CharacterManager').getSpecId(this.char['class'], this.char['specname'] as any)
      this.char.specid = specid
      manager.updateCharacter(this.char).then(() => {
        this.toast.show('Update', 'Success', { status: 'success' })
      })
    }
  }
  onSelectClass() {
    this.specs = specs[this.char.class]
    setTimeout(() => {
      this.char.specname = this.specs[0]
    }, 25)
  }
}