module Dodge.Room.Tanks where
import Dodge.Data
import Dodge.Placement.Instance.Pipe
import Dodge.LevelGen.Data
import RandomHelp
import Dodge.Room.Procedural
import Dodge.Room.Foreground
import Dodge.Room.Modify.Girder
--import Dodge.Room.RoadBlock
import Dodge.Placement.Instance
import Dodge.Placement.TopDecoration
import Dodge.PlacementSpot
import Dodge.Base.CardinalPoint
--import Padding
import Color
import Shape
import ShapePicture
import LensHelp
import Geometry

--import Data.Maybe
--import Data.Tree
--import qualified Data.Set as S

randomTank :: RandomGen g => State g Placement
randomTank = takeOne $ map (\f -> f (dim orange) orange)
    [ tankSquareDec fourEmbossDecoration
    , tankSquareDec midBarDecoration
    , tankSquareDec squareDecoration
    , roundTank
    , roundTankCross
    , tankSquareDec plusDecoration
    ]

randEdgeTank :: RandomGen g => State g Placement
randEdgeTank = do
    edge <- takeOne cardList
    basetank <- randomTank
    return $ basetank 
        & plType . putBlock . blDraw 
            %~ fmap (<> noPic ( colorSH orange (verticalPipe 80
                <>  horPipe 80 0 (70 *.* cardVec edge))))
                    -- 70 is a guess, the true value depends on the distance to the wall
        & plSpot .~ rprBool (\rp _ -> _rpPlacementUse rp == 0
                && rpOffPathFromEdge (PathFromEdge edge 0) rp)

randEdgeTanks :: RandomGen g => Int -> State g [Placement]
randEdgeTanks i = do
    edge <- takeOne cardList
    basetank <- randomTank
    return $ replicate i $ basetank 
        & plType . putBlock . blDraw 
            %~ fmap (<> noPic ( colorSH orange (verticalPipe 80
                <>  horPipe 80 0 (70 *.* cardVec edge))))
                    -- 70 is a guess, the true value depends on the distance to the wall
        & plSpot .~ rprBool (\rp _ -> _rpPlacementUse rp == 0
                && rpOffPathFromEdge (PathFromEdge edge 0) rp)

tanksPipesRoom :: RandomGen g => State g Room
tanksPipesRoom = do
    w <- state $ randomR (200,300)
    h <- state $ randomR (200,300)
    i <- state $ randomR (2,5)
    tanks <- randEdgeTanks i
    return $ roomRectAutoLinks w h & rmPmnts .++~ tanks

tanksRoom :: RandomGen g => [Creature] -> [Item] -> State g Room
tanksRoom crs its = do
    w <- state $ randomR (100,400)
    h <- state $ randomR (200,400)
    ntanks  <- state $ randomR (3,6)
    thetank <- randomTank <&> plSpot .~ unusedOffPathAwayFromLink 50
    let room = roomRectAutoLinks w h
    let plmnts = 
            map (\it -> sps0 (PutFlIt it) & plSpot .~ anyUnusedSpot) its
            ++ map (\cr -> sps0 (PutCrit cr) & plSpot .~ unusedSpotAwayFromLink 50) crs
            ++ replicate ntanks thetank
            ++ [mntLightLnkCond $ resetPLUse $ rprBool $ \rp _ -> isOutLnk rp]
--            , sps0 $ PutShape $ colorSH orange $ pipePP 2 (V3 50 50 25) (V3 50 120 25)
    hgshape <- takeOne [girder 96 20 10, girderZ 96 20 10, girderV 96 20 10]
    addhighgirds <- takeOne $
        [ addGirderEW hgshape black >=> addGirderEW hgshape black
        , addGirderEW hgshape black
        , addGirderNS hgshape black >=> addGirderNS hgshape black
        , addGirderNS hgshape black
        ] ++ replicate 4 return
    lgshape <- takeOne [girder 60 20 10, girderZ 60 20 10, girderV 60 20 10]
    addlowgirds  <- takeOne $ addGirderNS lgshape red : replicate 4 return
    (addlowgirds >=> addhighgirds) $ room & rmPmnts .++~ plmnts
        & rmName .~ "tanksRoom"
