module Dodge.Particle.Spark
    ( colSpark
    , colSparkRandDir
    , createBarrelSpark
    , randColDirTimeSpark
    ) where
import Dodge.Data
import Dodge.Particle.HitEffect.ExpireAndDamage
import Dodge.Particle.Bullet.Draw
import Dodge.Particle.Damage
--import Dodge.Particle.Bullet.HitEffect
import Dodge.Particle.Bullet.Update
--import Dodge.WorldEvent.HitEffect
import Color
import Geometry
import LensHelp

import Control.Monad.State
import System.Random
--import Control.Lens
-- TODO remove/simplify this function
createBarrelSpark :: Point2 -> Float -> Maybe Int -> Int -> Int -> World -> World
createBarrelSpark pos dir maycid time colid = instantParticles .:~ BulletPt  
    { _ptDraw = drawBul
    , _ptUpdate = mvBullet
    , _ptVel = rotateV dir (V2 5 0)
    , _btDrag = 0.9
    , _ptColor = numColor colid
    , _ptTrail = [pos]
    , _ptCrIgnore = maycid
    , _ptWidth = 1
    , _ptTimer = time
    , _ptHitEff = expireAndDamage basicSparkDams
    }
colSpark :: Int -> Color -> Point2 -> Float -> World -> World
colSpark = colSparkRandDir 0.7

randColDirTimeSpark 
    :: State StdGen Color 
    -> State StdGen Float 
    -> State StdGen Int 
    -> Point2
    -> World
    -> World
randColDirTimeSpark randcol randdir randtime pos w = w
    & instantParticles .:~ spark
    & randGen .~ g
  where
    ((col,dir,time),g) = (`runState` _randGen w) $ do
        c <- randcol
        d <- randdir
        t <- randtime
        return (c,d,t)
    spark = BulletPt
        { _ptDraw   = drawBul
        , _ptUpdate = mvBullet
        , _btDrag   = 0.9
        , _ptVel    = rotateV dir (V2 5 0)
        , _ptColor  = col
        , _ptTrail  = [pos]
        , _ptCrIgnore = Nothing
        , _ptWidth = 1
        , _ptTimer = time
        , _ptHitEff = expireAndDamage basicSparkDams
        }

colSparkRandDir :: Float -> Int -> Color -> Point2 -> Float -> World -> World
colSparkRandDir randDir time col pos baseDir w = w 
    & instantParticles .:~ spark
    & randGen .~ g
  where
    (a,g)  = randomR (-randDir,randDir) $ _randGen w
    dir = a + baseDir
    spark = BulletPt
        { _ptDraw = drawBul
        , _ptUpdate = mvBullet
        , _btDrag   = 0.9
        , _ptVel = rotateV dir (V2 5 0)
        , _ptColor = col
        , _ptTrail = [pos]
        , _ptCrIgnore = Nothing
        , _ptWidth = 1
        , _ptTimer = time
        , _ptHitEff = expireAndDamage basicSparkDams
        }
