import { Injectable } from '@angular/core';
import { CoreGameRMFactories } from '../../games/massive-darkness2/factorys/roamingMonsters/CoreGame';
import { DrawingBag, MobInfo, TreasureItem, TreasureType } from '../../games/massive-darkness2/massive-darkness2.model';
import { MD2StateService } from './md2-state.service';
import { MD2MobInfoService } from '../../games/massive-darkness2/service/massive-darkness2.service';
import { first } from 'rxjs/operators';

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

  constructor(
    private stateService: MD2StateService,
    private mobInfoService: MD2MobInfoService,
  ) { }

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

    this.initCoreGameMobs();
    this.initCoreGameRoamingMonsters();
  }

  private initCoreGameRoamingMonsters() {
    CoreGameRMFactories.forEach(factory => {
      for (let i = 1; i <= 5; i++) {
        this.stateService.roamingMobDeck.AddItem(new MobInfo({ name: factory.mobName, level: i, drawingWeight: 1 }));
        i++;
      }
    });

  }
  private initCoreGameMobs() {
    // 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.stateService.mobInfos = result;
      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];

          this.stateService.mobDeck.AddItem(new MobInfo({ name: mobInfo.name, level: levelInfo.level, drawingWeight: 1 }));
        }
      }

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

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

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