import { Injectable } from '@angular/core';
import { first } from 'rxjs/operators';
import { MobInfo, MobType } from '../../games/massive-darkness2/massive-darkness2.model';
import { ADIcon } from '../../ui/alert-dlg/alert-dlg.model';
import { MsgBoxService } from '../msg-box.service';
import { MD2Service } from './md2.service';

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

  attackingAllExp: number = 0;
  attackingAttackerExp: number = 0;
  attackingAttackerReward: string = '';
  constructor(
    private md2Service: MD2Service,
    private msgBoxService: MsgBoxService
  ) { }
  public get roamingMonsters() {
    return this.md2Service.info.roamingMonsters;
  }
  public get mobs() {
    return this.md2Service.info.mobs;
  }

  addOneUnit(mob: MobInfo) {
    if (mob.type == MobType.Mob) {
      mob.mobAmount++;
      mob.unitRemainHp = 1;
      this.md2Service.refreshUI$.next();
    }
  }

  private killOneUnit(mob: MobInfo) {
    let attacker = this.md2Service.currentActivateHero;
    if (mob.mobAmount > 1) {
      mob.mobAmount--;
      mob.unitRemainHp = mob.hp;
      this.attackingAttackerExp += 1;


    } else {
      if (mob.type == MobType.Mob) {
        this.mobs.splice(this.mobs.indexOf(mob), 1);
      } else {
        this.roamingMonsters.splice(this.roamingMonsters.indexOf(mob), 1);

      }
      mob.mobAmount = 0;

      this.attackingAllExp += mob.leaderExp;
      this.attackingAttackerExp += 1;
      this.attackingAttackerReward = `<br> and gains ${mob.carriedTreasureHtml}`;

      this.md2Service.mobBeenKilledSubject.next(mob);

    }
    this.md2Service.refreshUI$.next();
  }

  healMob(mob: MobInfo, healAmount: number) {
    switch (mob.type) {
      case MobType.Mob:
        mob.unitRemainHp += healAmount;
        if (mob.unitRemainHp > mob.hp) {
          mob.unitRemainHp -= mob.hp;
          mob.mobAmount += 1;
        }

        break;
      case MobType.RoamingMonster:
      case MobType.Boss:
        mob.unitRemainHp += healAmount;
        if (mob.unitRemainHp > mob.hp) {
          mob.unitRemainHp = mob.hp;
        }
        break;
      default: break;
    }

    this.md2Service.refreshUI$.next();
  }
  attackMob(mob: MobInfo, attackDamage: number) {

    this.attackingAttackerReward = '';
    if (attackDamage) {
      do {
        if (attackDamage >= mob.unitRemainHp) {
          attackDamage -= mob.unitRemainHp;
          this.killOneUnit(mob);
        } else {
          let originAttackDamage = attackDamage;
          attackDamage -= mob.unitRemainHp;
          mob.unitRemainHp -= originAttackDamage;
        }
      } while (attackDamage > 0 && mob.mobAmount > 0);

      let attacker = this.md2Service.currentActivateHero;
      let attackerTitle = '';
      if (attacker) {
        attackerTitle = attacker.heroFullName;
      }
      if (this.attackingAllExp > 0) {
        let attackerRewardText = '';
        if (attacker) {
          attackerRewardText = `<b>${attackerTitle}</b> Gains ${this.attackingAttackerExp} Extra Exp.<br> and gains ${mob.carriedTreasureHtml}`;
        }
        this.msgBoxService.show(`All Hero Gains ${this.attackingAllExp} Exp`,
          { text: attackerRewardText, icon: ADIcon.INFO })
          .pipe(first()).subscribe(result => {
            for (let i = 0; i < this.md2Service.heros.length; i++) {
              const hero = this.md2Service.heros[i];
              hero.exp += this.attackingAllExp;
            }
            if (attacker) {
              attacker.exp += this.attackingAttackerExp;
            }
            this.attackingAllExp = 0;
            this.attackingAttackerExp = 0;
            this.md2Service.broadcastAllHeroInfoToAll();
            this.md2Service.refreshUI$.next();
          });
      } else if (this.attackingAttackerExp > 0 && attacker) {
        this.msgBoxService.show(`<b>${attackerTitle}</b> Gains ${this.attackingAttackerExp} Exp`, { icon: ADIcon.INFO }).pipe(first()).subscribe(result => {
          attacker.exp += this.attackingAttackerExp;
          this.attackingAttackerExp = 0;
          this.md2Service.broadcastHeroInfoToOwner(attacker);
          this.md2Service.refreshUI$.next();
        });


      }

    }

  }
}
