module Dodge.Combine.Module where
import Dodge.Data
import Dodge.Tesla
import Dodge.Particle.HitEffect.ExpireAndDamage
import Dodge.WorldEvent.SpawnParticle
import Dodge.Particle.Damage
import Dodge.Item.Weapon.ExtraEffect
import Dodge.Item.Weapon.Launcher
import Dodge.Item.Weapon.TriggerType
import LensHelp
import Geometry
import Dodge.Beam
import Dodge.Base.Coordinate

import Data.Maybe

moduleModification :: ItemModuleType -> Item -> Item
moduleModification imt = case imt of
    EMPTYMODULE -> id
    DRUMMAG     -> itConsumption . laMax .~ 45
    BELTMAG     -> itConsumption . laMax .~ 150
    MAGNETMAG   -> itUse . useDelay . rateMax .~ 4
    INCENDBUL   -> f $ expireAndDamage $ spawnAtBulDams incBall
    BOUNCEBUL   -> f $ expireAndDamage bounceBulDams
    STATICBUL   -> f $ expireAndDamage $ spawnAtBulDams aStaticBall
    CONCUSBUL   -> f $ expireAndDamage $ spawnAtBulDams concBall
    TARGCR      -> itTargeting .~ targetRBCreature
    TARGLAS     -> itTargeting .~ targetLaser
    TARGPOS     -> itTargeting .~ targetRBPress
    MAGNETTRAJ  -> (itConsumption . laAmmoType . amBulTraj .~ MagnetTrajectory) 
                . (itConsumption . laAmmoType . amBulVel  .~ V2 10 0)
    FLECHETRAJ  -> itConsumption . laAmmoType . amBulTraj .~ FlechetteTrajectory
    BEZIERTRAJ  -> itConsumption . laAmmoType . amBulTraj .~ BezierTrajectory
    INCENDLAS   -> itParams . lasBeam .~ BeamCombine flameBeamCombine
    SPLITLAS    -> itParams . lasBeam .~ BeamCombine splitBeamCombine
    STATICLAS   -> (itParams . lasBeam .~ BeamCombine teslaBeamCombine)
                . (itParams . subParams ?~ teslaParams)
    WEPTELE     -> makeDirectedTele
    LAUNCHHOME  -> itConsumption . laAmmoType . amPjCreation .~ fireTrackingShell
    EXTRABATTERY -> itConsumption . laMax +~ 1000
  where
    f ameff = itConsumption . laAmmoType . amBulEff .~ ameff
    makeDirectedTele it = it 
        & itTargeting .~ targetRBPress
        & itUse . useMods .:~ withPosDirWallCheck directedTelPos
-- for the camera: the simplest option is to remove all zoom/offset
        & itUse . useAim . aimZoom . itZoomFac .~ 1
        & itUse . useAim . aimRange .~ 0
-- a better option would be to involve a "scope" centered on the firing
-- position
    directedTelPos it cr w = (p,a)
      where
        p = fromMaybe (_crPos cr) $ it ^? itTargeting . tgPos . _Just
        a = argV (mouseWorldPos w -.- p)

applyModules :: Item -> Item
applyModules it = foldr moduleModification it (_iyModules (_itType it))
