module Dodge.LightSource
    ( tlsTimeRadFunPos
    , tlsTimeRadColPos
    , lsRadCol
    , lsPosRadCol
    , lsColPosRad
    , lsPosColRad 
    , lsColPos
    , lsPosCol
    , lsPosRad
    , lsColPosID
    , makeTlsTimeRadColPos 
    ) where
import Dodge.Data
import Geometry.Data
import Dodge.Default.LightSource
import LensHelp

lsPosRad :: Point3 -> Float -> LightSource
lsPosRad = lsColPosRad 0.75

tlsTimeRadFunPos :: Int -> Float -> (Int -> Float) -> Point3 -> TempLightSource
tlsTimeRadFunPos t rmax intensityF p = TLS 
    { _tlsParam = LSParam
        { _lsPos = p
        , _lsRad = rmax
        , _lsCol = f $ intensityF t
        }
    , _tlsUpdate = upF
    , _tlsTime = t
    }
  where
    upF _ tls 
        | _tlsTime tls <= 0 = Nothing
        | otherwise         = Just $ tls    
            & tlsTime -~ 1
            & tlsParam . lsCol .~ f (intensityF (_tlsTime tls) )
    f x' = V3 x' x' x'

lsPosColRad :: Point3 -> Point3 -> Float -> LightSource
lsPosColRad p col = lsColPosRad col p

lsPosRadCol :: Point3 -> Float -> Point3 -> LightSource
lsPosRadCol p r col = defaultLS 
    & lsParam . lsPos .~ p
    & lsParam . lsRad .~ r 
    & lsParam . lsCol .~ col

lsColPosID :: Point3 -> Point3 -> Int -> LightSource
lsColPosID col p i  = lsColPos col p & lsID .~ i

lsColPos :: Point3 -> Point3 -> LightSource
lsColPos col pos = lsColPosRad col pos 700

lsPosCol :: Point3 -> Point3 -> LightSource
lsPosCol pos col = lsColPos col pos

lsColPosRad :: Point3 -> Point3 -> Float -> LightSource
lsColPosRad col pos r = lsPosRadCol pos r col

lsRadCol :: Float -> Point3 -> LightSource
lsRadCol r col = defaultLS & lsParam . lsRad .~ r 
    & lsParam . lsCol .~ col

tlsTimeRadColPos
    :: Int 
    -> Float -- ^ maximal radius
    -> Point3
    -> Point3 
    -> TempLightSource
tlsTimeRadColPos t rmax col (V3 x y z) = TLS 
    { _tlsParam = LSParam
        { _lsPos = V3 x y z
        , _lsRad = rmax
        , _lsCol = col
        }
    , _tlsUpdate = upF
    , _tlsTime = t
    }
  where
    upF _ tls 
        | _tlsTime tls <= 0 = Nothing
        | otherwise         = Just $ tls    
            & tlsTime -~ 1

makeTlsTimeRadColPos :: Int -> Float -> Point3 -> Point3 -> World -> World
makeTlsTimeRadColPos i rad (V3 r g b) p = tempLightSources .:~ tlsTimeRadColPos i rad (V3 r g b) p
