module Dodge.Item.Attachment
    ( charFiringStratI
    , scrollCharMode
    , changeFuse
    ) where
import Dodge.Data
import Dodge.Default

import Control.Lens hiding ((|>),(<|))
import Data.Sequence
import Data.Maybe
import qualified Data.IntMap.Strict as IM


scrollCharMode
    :: Float    -- ^ Amount scrolled
    -> Creature
    -> Item
    -> Item
scrollCharMode x _ 
    | x > 0 = incCharMode
    | x < 0 = decCharMode
    | otherwise = id

incCharMode 
    :: Item 
    -> Item
incCharMode = itAttachment . itCharMode %~ cycleL
  where
    cycleL (x :<| xs) = xs |> x
    cycleL xs = xs

decCharMode 
    :: Item 
    -> Item
decCharMode = itAttachment . itCharMode %~ cycleR
  where
    cycleR (xs :|> x) = x <| xs
    cycleR xs = xs

type ChainEffect = 
    (Item -> Creature -> World -> World)
    -> Item
    -> Creature
    -> World 
    -> World
charFiringStratI
    :: [(Char, ChainEffect)] -- ^ Different firing effects for different characters
    -> ChainEffect
charFiringStratI strats eff item cr w = case w ^? creatures . ix cid . crInv 
        . ix (_crInvSel $ _creatures w IM.! cid) . itAttachment . itCharMode of
    Just (c :<| _) -> fromMaybe id (Prelude.lookup c strats) eff item cr w
    _ -> w
  where
    cid = _crID cr

changeFuse 
    :: Float  -- ^ Old fuse time
    -> Creature  -- ^ Inventory item reference (I believe)
    -> Item 
    -> Item
changeFuse scrollAmount _ it = it
        & ( itAttachment .~ ItFuse newTime )
        & ( itAimZoom .~ defaultItZoom{_itZoomMax = zm, _itZoomMin = zm} )
  where 
    oldTime = _itFuseTime $ _itAttachment it
    newTime = min 90 $ max 20 $ oldTime - round (5 * scrollAmount)
    zm = 50 / fromIntegral newTime

--removeItAttachment :: Int -> Int -> World -> World
--removeItAttachment i _ w = w & creatures . ix i . crInv . ix itRef . itAttachment .~ NoItAttachment
--  where 
--    cr = _creatures w IM.! i
--    itRef = _crInvSel cr
