module Dodge.Room.Start where
import Dodge.LevelGen.Data
--import Dodge.PlacementSpot
import Dodge.Room.RunPast
--import Dodge.RoomLink
import Dodge.Room.LasTurret
--import Dodge.Data
--import Dodge.Default
--import Dodge.RoomLink
import Dodge.Tree
import Dodge.RandomHelp
import Dodge.Room.Door
import Dodge.Room.RezBox
import Dodge.Room.Corridor
import Dodge.Room.Room
import Dodge.Room.Link
import Dodge.Room.Procedural
import Dodge.Room.Foreground
--import Dodge.Room.RoadBlock
import Dodge.Placement.Instance
--import Dodge.Item.Random
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 Data.Maybe
--import Data.Tree
import Control.Monad.State
import Control.Lens
import System.Random
--import qualified Data.IntMap.Strict as IM

minigunFakeout :: RandomGen g => State g (SubCompTree Room)
minigunFakeout = do
    rcol <- rezColor
    ncor <- state $ randomR (0,2)
    roomwithmini <- randomiseAllLinks $ roomRectAutoLinks 150 150
        & rmPmnts %~ (plRRpt 0 (PutFlIt miniGun):)
    randcors <- replicateM ncor $ (fmap PassDown . randomiseOutLinks) corridor
    return $ ([PassDown $ rezBox rcol
                ,PassDown door
                ,PassDown roomwithmini 
                ,PassDown door
                ] 
            ++ randcors
            ++ [PassDown $ corridor & rmPmnts %~ ( plRRpt 0 (PutFlIt shrinkGun) :)
                ,PassDown keyholeCorridor,PassDown corridor])
        `treeFromPost` UseAll door


rezThenLasTurret :: RandomGen g => State g (SubCompTree Room)
rezThenLasTurret = do
    rbox <- rezBoxStart
    cenroom <- randomiseOutLinks $ (lightSensInsideDoor cenLasTur) {_rmLabel = Just 0}
    let doorroom = switchDoorRoom {_rmTakeFrom = Just 0}
        contTree = treeFromPost [PassDown cenroom,PassDown doorroom] (UseAll door)
    return $ rbox `passUntilUseAll` [contTree]

room2 :: RandomGen g => Int -> State g (SubCompTree Room)
room2 = lasCenSensEdge    

startRoom :: RandomGen g => Int -> State g (SubCompTree Room)
--startRoom i = join $ takeOne 
startRoom _ = join $ takeOne 
    [-- roomMiniIntro
    --minigunfakeout
--     rezBoxesWp
     rezBoxesThenWeaponRoom
--    , rezBoxThenWeaponRoom
--     rezBoxesWpCrit
--    , runPastStart i
    ]

runPastStart :: RandomGen g => Int -> State g (SubCompTree Room)
runPastStart i = do
    s <- rezBoxStart
    rp <- runPastRoom i
    return $ s `passUntilUseAll` [rp]

rezBoxStart :: RandomGen g => State g (SubCompTree Room)
rezBoxStart = do
    ls <- rezColor
    return $ treeFromPost [PassDown $ rezBox ls] (UseAll door)


rezBoxesThenWeaponRoom :: RandomGen g => State g (SubCompTree Room)
rezBoxesThenWeaponRoom = do
    rboxes <- rezBoxes
    wroom  <- weaponRoom
    return $ rboxes `passUntilUseAll` [wroom]

rezBoxThenWeaponRoom :: RandomGen g => State g (SubCompTree Room)
rezBoxThenWeaponRoom = do
    rcol <- rezColor
    treeFromTrunk [PassDown $ rezBox rcol,PassDown door] <$> weaponRoom



startRoom' :: RandomGen g => State g (SubCompTree Room)
startRoom' = do
    w <- state $ randomR (100,400)
    h <- state $ randomR (200,400)
    let plmnts = 
            [ sPS (V2 0 0) 0 $ PutShape $ girderV 40 20 10 (V2 0 (h/2)) (V2 w (h/2))
            --, mntLS jShape (V2 0 (h/3)) (V3 40 (h/3) 70)
            , tankSquareEmboss4 (dim orange) 50 (h-60)
            , tankSquare (dim orange) 50 50
            , tankSquare (dim orange) 50 120
            , lightSensor 10 (const id) (PS (V2 (0.8*w) (0.25*h)) 0)
            , putLasTurret 0.005 & plSpot .~ PS (V2 (0.8*w) (0.8*h)) 0
            , sps0 $ PutShape $ colorSH orange $ pipePP 2 (V3 50 50 25) (V3 50 120 25)
            ]
    thecol <- rezColor
    treeFromPost [PassDown $ rezBox thecol, PassDown door] . UseAll 
        <$> randomiseOutLinks 
            (shiftRoomBy (V2 (-20) (-20),0) 
            ( roomRectAutoLinks w h & rmPmnts %~ (plmnts ++) 
            ) & rmShift .~ (0,0) )
