module Dodge.Inventory.CheckSlots where
import Dodge.Data
import Dodge.Base.You
import Dodge.Inventory.ItemSpace
import qualified IntMapHelp as IM

import Data.Maybe
--import Control.Lens

-- | checks whether or not an item will fit in your inventory
-- if so return Just the next slot to be used
checkInvSlotsYou :: Item -> World -> Maybe Int
checkInvSlotsYou it w
    | crNumFreeSlots ycr >= ceiling (_itInvSize it)
                = Just $ findItemSlot it inv
    | otherwise = Nothing
  where 
    ycr = you w
    inv = _crInv ycr

-- Assumes that the item is singular.
-- Do not want to stack floor items for now
findItemSlot :: Item -> IM.IntMap Item -> Int
findItemSlot it inv = case _itConsumption it of
    ItemItselfConsumable _ 
        -> fromMaybe newslot $ IM.findIndex (\it' -> _itType it == _itType it') inv
    _   -> newslot
  where
    newslot = maybe 0 ((+1) . fst) $ IM.lookupMax inv

crNumFreeSlots :: Creature -> Int
crNumFreeSlots cr = _crInvCapacity cr - invSize (_crInv cr)

crInvSize :: Creature -> Int
crInvSize = invSize . _crInv

invSize :: IM.IntMap Item -> Int
invSize = sum . fmap itSlotsTaken
