{-# LANGUAGE FlexibleContexts  #-}

module Hrpg.Framework.Combat.Encounter
      ( EncounterData (..)
      , runEncounter
      ) where

import Control.Monad.Random
import Control.Monad.State
import Data.Text

import Hrpg.Framework.Combat.CombatResult
import Hrpg.Framework.Combat.EncounterData
import Hrpg.Framework.Combat.IO
import Hrpg.Framework.Player
import Hrpg.Framework.Mobs.AI
import Hrpg.Framework.Mobs.Mob
import Hrpg.Framework.Xp

doPlayerTurn :: (MonadRandom m, MonadIO m, MonadState EncounterData m) => m ()
doPlayerTurn = do
  action <- getPlayerAction
  result <- doPlayerAction action
  updateMob result 
  printPlayerResult result

doMobTurn :: (MonadRandom m, MonadIO m, MonadState EncounterData m) => m ()
doMobTurn = do
  action <- getMobAction
  result <- doMobAction action
  updatePlayer result 
  printMobResult result

runEncounter :: (MonadRandom m, MonadIO m, MonadState EncounterData m) => m Bool
runEncounter = do
  printEncounter
  doPlayerTurn
  mob <- getMob
  case isMobDead mob of
    True -> do
      printMobDeath
      printMobLoot
      return True
    False -> do
      doMobTurn
      player <- getPlayer
      case isPlayerDead player of
        True  -> do
          printPlayerDeath
          return True
        False -> runEncounter
