import { Injectable } from '@angular/core';
import { DrawingBag, MobInfo, MobType, TreasureItem, TreasureType } from '../../games/massive-darkness2/massive-darkness2.model';
import { MD2MobInfoService } from '../../games/massive-darkness2/service/massive-darkness2.service';
import { first } from 'rxjs/operators';
import { MD2Service } from './md2.service';

@Injectable({
  providedIn: 'root'
})
export class MD2InitService {

  constructor(
    private md2Service: MD2Service,
    private mobInfoService: MD2MobInfoService,
  ) { }

  public initMobDecks() {
    this.md2Service.mobDeck = new DrawingBag();
    this.md2Service.roamingMobDeck = new DrawingBag();

    this.initCoreGameMobAndRoamingMonsters();
  }

  private initCoreGameMobAndRoamingMonsters() {
    // CoreGameMobFactories.forEach(factory => {
    //   for (let i = 1; i <= 5; i++) {
    //     this.stateService.mobDeck.AddItem(new MobInfo({ name: factory.mobName, level: i, drawingWeight: 1 }));
    //     i++;
    //   }
    // });
    this.mobInfoService.getAll().pipe(first()).subscribe(result => {
      this.md2Service.mobInfos = result.filter(m => m.type == MobType.Mob);
      this.md2Service.roamingMobInfos = result.filter(m => m.type == MobType.RoamingMonster);
      for (let i = 0; i < result.length; i++) {
        const mobInfo = result[i];
        for (let j = 0; j < mobInfo.mobLevelInfos.length; j++) {
          const levelInfo = mobInfo.mobLevelInfos[j];
          switch (mobInfo.type) {
            case MobType.Mob:
              this.md2Service.mobDeck.AddItem(new MobInfo({ name: mobInfo.name, level: levelInfo.level, drawingWeight: 1 }));
              break;
            case MobType.RoamingMonster:
              this.md2Service.roamingMobDeck.AddItem(new MobInfo({ name: mobInfo.name, level: levelInfo.level, drawingWeight: 1 }));
              break;
          }
        }
      }

      // result.forEach(mobInfo => {
      //   this.stateService.mobDeck.AddItem(new MobInfo({ name: mobInfo.name, level: mobInfo.level, drawingWeight: 1 }));
      // });
    });
  }

  public initTreasureBag() {
    this.md2Service.treasureBag.ClearAllItems();
    this.addTreasure(TreasureType.Common, 15);
    this.addTreasure(TreasureType.Rare, 5);
  }

  public addTreasure(type: TreasureType, amount: number = 1) {
    this.md2Service.treasureBag.AddItem(new TreasureItem(type, amount));
  }
}
