module Dodge.Damage where
import Dodge.Data
import Dodge.Wall.Damage
import FoldableHelp
import Geometry.Vector
import LensHelp

import qualified Data.Map.Strict as M

damageCrWall :: Damage -> Either Creature Wall -> World -> World
damageCrWall dt (Left cr) = creatures . ix (_crID cr) . crState . csDamage .:~ dt
damageCrWall dt (Right wl) = damageWall dt wl

damageDirection :: [Damage] -> Maybe Float
damageDirection ds = do
    dm <- safeMinimumOn (negate . _dmAmount) ds
    safeArgV (_dmTo dm -.- _dmFrom dm)

collectDamageTypes :: [Damage] -> M.Map DamageType Int
collectDamageTypes = foldr f M.empty
  where
    f dm = M.insertWith (+) (_dmType dm) (_dmAmount dm)

maxDamageType :: [Damage] -> Maybe (DamageType,Int)
maxDamageType = safeMinimumOn (negate . snd) . M.assocs . collectDamageTypes
