module Dodge.Placement.Instance.LightSource.Flicker where
import Dodge.LevelGen.Data
import Dodge.Data
import qualified Data.IntMap.Strict as IM
import Data.Maybe
import Control.Lens
import System.Random
-- Supposes the orginal placement is a light source, adds a flicker
flickerMod :: Placement -> Maybe Placement
flickerMod pl = Just $ sps0 $ PutMod $ ModIDTimerPoint3Bool
    { _mdID = 0
    , _mdExternalID = fromJust $ _plMID pl
    , _mdUpdate = flickerUpdate
    , _mdTimer  = 10
    , _mdPoint3  = 0.8
    , _mdBool = True
    }

flickerUpdate :: Modification -> World -> World
flickerUpdate md w 
    | _mdTimer md > 0 = w & modifications . ix mdid . mdTimer -~ 1
    | otherwise = w & lightSources . ix lsid . lsIntensity .~ mdcol
        & modifications . ix mdid 
            %~ ( (mdTimer .~ newtime) . (mdPoint3 .~ lscol) . (mdBool %~ not) )
  where
    mdcol = _mdPoint3 md
    lscol = _lsIntensity $ _lightSources w IM.! lsid
    lsid = _mdExternalID md
    mdid = _mdID md
    timerange 
        | _mdBool md = (2,10)
        | otherwise  = (2,30)
    (newtime,_) = randomR timerange $ _randGen w
