module Dodge.Creature.Statistics 
    ( getCrStrength
    , getCrDexterity
    ) where
import Dodge.Data
import Dodge.Creature.Test
import qualified IntMapHelp as IM
import LensHelp

import Data.Maybe
import Data.IntMap.Merge.Strict

getCrDexterity :: Creature -> Int
getCrDexterity cr = _dexterity (_crStatistics cr)

getCrStrength :: Creature -> Int
getCrStrength cr = strFromHeldItem cr + strFromEquipment cr + _strength (_crStatistics cr)

strFromEquipment :: Creature -> Int
strFromEquipment = sum . fmap equipmentStrValue . crCurrentEquipment

equipmentStrValue :: Item -> Int
equipmentStrValue itm = case _iyBase $ _itType itm of
    FRONTARMOUR -> negate 3
    POWERLEGS   -> 3
    _ -> 0

crCurrentEquipment :: Creature -> IM.IntMap Item
crCurrentEquipment cr =  merge dropMissing dropMissing (zipWithMatched (\_ _ itm -> itm))
    (_crInvEquipped cr)
    (_crInv cr)

strFromHeldItem :: Creature -> Int
strFromHeldItem cr
    | _posture (_crStance cr) == Aiming || crIsReloading cr
        = negate $ fromMaybe 0 $ cr ^? crInv . ix (crSel cr) . itUse . useAim . aimWeight
    | otherwise = 0
