module Dodge.Room.RunPast where
import Dodge.LevelGen.Data
import Dodge.RoomLink
import Dodge.Data
import Dodge.Default
--import Dodge.Layout.Tree.Either
import Dodge.RandomHelp
import Dodge.Room.Door
--import Dodge.Room.Room
import Dodge.Room.Link
import Dodge.Room.Procedural
import Dodge.Room.Corridor
--import Dodge.Room.Foreground
--import Dodge.Room.RoadBlock
import Dodge.PlacementSpot
import Dodge.Placement.Instance
import Dodge.Tree
import Dodge.Item.Random
--import Dodge.Item.Weapon.SprayGuns
--import Dodge.Item.Weapon.Grenade
--import Dodge.LevelGen.Data
import Geometry.Data
--import Padding
import Color
--import Shape

import Data.Tree
import Control.Monad.State
import Control.Lens
import System.Random
--import qualified Data.IntMap.Strict as IM

horEdgeTest :: (Float -> Bool) -> ((Point2,Float) -> Bool)
horEdgeTest t (V2 x _,_) = t x


roomCritLS :: RandomGen g => State g LightSource
roomCritLS = do
    col <- takeOne [0.5, 0.6]
    h   <- takeOne [30,35]
    rad <- takeOne [300]
    return defaultLS
        { _lsPos = V3 0 0 h
        , _lsIntensity = col
        , _lsRad = rad
        }

critRoom :: Room
critRoom = corridorWallN
    { _rmRandPSs = [psRandRanges (15,25) (30,45) (pi,2*pi)]
    }

runPastRoom :: RandomGen g => Int -> State g (SubCompTree Room)
runPastRoom i = do
    h <- state $ randomR (200,400::Float)
    theedgetest <- takeOne [horEdgeTest (<1), horEdgeTest  (>39)]
    thels <- roomCritLS
    --theweapon <- join $ takeOne [randBlockBreakWeapon,return grenade]
    theweapon <- randBlockBreakWeapon
    cenroom <- changeLinkFrom theedgetest
                $ roomRectAutoLinks 40 h & rmPmnts .~ [plRRpt 0 (PutFlIt theweapon)]
    --let cenroom = filterSortOutLinksOn theedgetest ((\(V2 a b) -> (a,b)) . fst)
    let linkcor = critRoom {_rmPmnts = 
                    [ spanLS thels (V2 0 65) (V2 40 65)
                    ]
                    }
        critroom = linkcor & rmPmnts %~ ( plRRpt 0 randC1 :)
        aswitchroom = corridorWallN
            {_rmExtPmnt = Just $ putLitButOnPosExtTrig red useLnkRoomPos
            ,_rmLabel = Just i
            ,_rmPmnts = []
            }
        switchdoor = switchDoorRoom & rmTakeFrom ?~ i
        n = length $ filter theedgetest $ map lnkPosDir $ _rmLinks cenroom
        controom = treeFromPost [PassDown switchdoor,PassDown linkcor] (UseAll door)
        critrooms :: [SubCompTree Room]
        critrooms = treeFromPost [PassDown switchdoor] (PassDown critroom) : 
            replicate (n-2) (treeFromPost [PassDown switchdoor] (PassDown linkcor))
    return $ Node (PassDown cenroom) (controom : critrooms ++ [return $ PassDown aswitchroom])
