module Dodge.Particle.TeslaArc
    ( aTeslaArcAt
    ) where
import Dodge.Data
import Dodge.Damage
--import Dodge.WorldEvent.Damage
--import Dodge.Base
--import Dodge.Zone
--import Dodge.Base.Collide
import Dodge.Particle.Spark
--import Dodge.WorldEvent.ThingsHit
import RandomHelp
import Picture
import Geometry
--import qualified IntMapHelp as IM
import LensHelp

--import Data.Function (on)
--import Data.List
--import Data.Maybe

aTeslaArcAt :: Color -> [ArcStep] -> Particle
aTeslaArcAt col thearc = LinearParticle
    { _ptPoints = map (^. asPos) thearc
    , _ptDraw = drawTeslaArc
    , _ptUpdate = moveTeslaArc thearc
    , _ptTimer = 2
    , _ptColor = brightX 100 1.5 col
    }

drawTeslaArc :: Particle -> Picture
drawTeslaArc pt = setLayer BloomNoZWrite $ pictures 
    [ setDepth 20.5 $ color (brightX 2 1 $ _ptColor pt) $ thickLine  3 ps
    , setDepth 20   $ color (V4 0 0 0 0.5)              $ thickLine 10 ps
    ]
  where
    ps = _ptPoints pt
moveTeslaArc :: [ArcStep]
    -> World
    -> Particle
    -> (World,Maybe Particle)
moveTeslaArc thearc w pt
    | _ptTimer pt == 2  = (makesparks $ foldr damthings w thearc, Just $ pt & ptTimer -~ 1)
    | _ptTimer pt < 1   = (w, Nothing)
    | otherwise         = (w, Just $ pt & ptTimer -~ 1)
  where
    rcol = brightX 100 1.5 <$> takeOne [white,azure,blue,cyan]
    rdir = state (randomR (-0.7,0.7)) <&> (+ ld)
    rtime = state $ randomR (5,8)
    makeaspark = randColDirTimeSpark rcol rdir rtime lp
    makesparks = makeaspark . makeaspark . makeaspark
    (lp,ld) = case last thearc of
        ArcStep lp' ld' Nothing -> (lp',ld')
        ArcStep lp' ld' (Just (Left cr)) -> (lp' -.- (_crRad cr + 1) *.* unitVectorAtAngle ld',ld'+pi)
        ArcStep lp' ld' (Just (Right _)) -> (lp' -.- 2 *.* unitVectorAtAngle ld',ld'+pi)
    damthings (ArcStep _ _ Nothing) = id
    damthings (ArcStep p dir (Just crwl)) = damageCrWall (thedamage p dir) crwl
    thedamage p dir = Damage ELECTRICAL 50 (p -.- q) p (p +.+ q) NoDamageEffect
      where
        q = 5 *.* unitVectorAtAngle dir
