module Dodge.Placement.Instance.Analyser 
    ( analyser
    ) where
--import Dodge.LevelGen.Data
--import Dodge.PlacementSpot
import Dodge.Data
import Dodge.Base.You
import Dodge.Default
import Dodge.Terminal
--import Dodge.Tree
--import Dodge.RoomLink
--import Dodge.Room.Door
--import Dodge.Room.Corridor
--import Dodge.Room.Link
--import Dodge.Room.Ngon
--import Dodge.Room.Procedural
--import Dodge.Room.Foreground
--import Dodge.Room.RoadBlock
import Dodge.Placement.Instance
--import Dodge.Placement.Shift
import Dodge.SoundLogic
--import Dodge.Default.Room
--import Dodge.Item.Weapon.BulletGuns
--import Dodge.Item.Weapon.Utility
--import Dodge.LevelGen.Data
--import Geometry.Data
import Geometry
--import Padding
import Color
--import Shape
--import ShapePicture
import LensHelp
--import Dodge.RandomHelp

--import qualified Data.Set as S
import Data.Maybe
--import Data.Tree
--import Control.Monad.State
--import System.Random

analyser 
    :: ProximityRequirement
    -> PlacementSpot
    -> PlacementSpot
    -> Placement
analyser proxreq pslight psmc = extTrigLitPos pslight $ \tp ->
    Just $ plSpot .~ psmc $ putTerminal'' themachine tparams (linksensortotrigger tp)
  where
    tparams = basicTerminal & tmScrollCommands .:~ sensorCommand
    linksensortotrigger tp _ mc 
        = triggers . ix (fromJust $ _plMID tp) .~ const (_sensToggle $ _mcSensor mc)
    themachine = defaultMachine & mcColor .~ aquamarine
        & mcUpdate .~ mcProximitySensorUpdate
        & mcDraw .~ terminalSPic
        & mcHP .~ 100
        & mcSensor .~ defaultProximitySensor {_proxRequirement = proxreq}

mcProximitySensorUpdate :: Machine -> World -> World
mcProximitySensorUpdate mc w = case 
        (_proxStatus sens
        , _sensToggle sens
        , mcProxTest mc w 
        , dist (_crPos ycr) (_mcPos mc) < _proxDist sens) of
    (_,True,_,_) -> w
    (_,False,True,True) -> w 
            & machines . ix (_mcID mc) . mcSensor . sensToggle .~ True
            & playsound dedaS
            & machines . ix (_mcID mc) . mcSensor . proxStatus .~ IsClose
    (NotClose,_,False,True) -> w & playsound dedumS
            & machines . ix (_mcID mc) . mcSensor . proxStatus .~ IsClose
    (_,_,_,False) -> w & machines . ix (_mcID mc) . mcSensor . proxStatus .~ NotClose
    _ -> w
 where
    playsound sid = soundContinue (MachineAltSound (_mcID mc)) (_mcPos mc) sid Nothing
    ycr = you w
    sens = _mcSensor mc

mcProxTest :: Machine -> World -> Bool
mcProxTest mc w = case mc ^? mcSensor . proxRequirement of
    Just (RequireHealth x)      -> _crHP cr >= x
    Just (RequireEquipment ct)  -> any (\itm -> _iyBase (_itType itm) == ct) (_crInv cr)
    _ -> False
  where
    cr = you w
