--{-# LANGUAGE TupleSections #-}
module Dodge.LockAndKey where
import Dodge.Cleat
import Dodge.Data
import Dodge.Tree
--import Dodge.LevelGen.Data
import Dodge.Room
import RandomHelp
import Dodge.Creature
import Dodge.Item.Craftable
import Dodge.Item.Weapon.BulletGun.Rod
import Dodge.Item
--import LensHelp
--import Dodge.Item.Equipment


bossKeyItems :: RandomGen g => [ (State g (Tree Room), State g ItemBaseType) ]
bossKeyItems = [(return . cleatOnward <$> bossRoom autoCrit, takeOne [PISTOL]) ]

lockRoomMultiItems :: RandomGen g => [ ( State g (MetaTree Room String) , State g [ItemBaseType] ) ]
lockRoomMultiItems = 
    [ (blinkAcrossChallenge, takeOne [[BLINKERUNSAFE,AUTODETECTOR WALLDETECTOR]
                                    ,[BLINKERUNSAFE,CLICKDETECTOR WALLDETECTOR]
                                    ]
        )
    ]

lockRoomKeyItems :: RandomGen g => [ (Int -> State g (MetaTree Room String) , State g ItemBaseType ) ]
lockRoomKeyItems = 
    [(lasCenSensEdge, takeOne [LAUNCHER,LASGUN,SPARKGUN,FLATSHIELD,FORCEFIELDGUN] )
    ,(sensorRoomRunPast ELECTRICAL, takeOne [CRAFT STATICMODULE,SPARKGUN] )
    ,(sensorRoomRunPast FLAMING, takeOne [FLAMESPITTER, CRAFT INCENDIARYMODULE] )
    ,(sensorRoomRunPast LASERING, return LASGUN )
    ,(const slowDoorRoomRunPast, return $ MINIGUNX 3)
    ,(const longRoomRunPast, takeOne [SNIPERRIFLE,FLATSHIELD])
    ,(const glassLessonRunPast, takeOne [LASGUN])
    ,(const $ lasTunnelRunPast 400, takeOne [FLATSHIELD,FORCEFIELDGUN])
    ,(keyCardRoomRunPast 0, return (KEYCARD 0))
    ]
keyCardRunPastRand :: RandomGen g => [ (Int -> State g (MetaTree Room String) , State g ItemBaseType ) ]
keyCardRunPastRand = [(keyCardRoomRunPast 0, return (KEYCARD 0)) ]

itemRooms :: RandomGen g => [(ItemBaseType, State g (MetaTree Room String))]
itemRooms = 
    [ (LAUNCHER , join $ takeOne 
        [corridorBoss launcherCrit
        , rc $ map makeTypeCraft [TUBE,TUBE,HARDWARE]
        ] 
      )
    , (MINIGUNX 3  , join $ takeOne 
        [roomMiniIntro
        , rc $ map makeTypeCraft ([MOTOR,HARDWARE] ++ replicate 6 PIPE)
        ] )
    , (LASGUN   , join $ takeOne 
        [rc $ map makeTypeCraft [PRISM,TRANSFORMER,PIPE]
        ]
      )
    , (SPARKGUN   , join $ takeOne 
        [rc $ map makeTypeCraft [TRANSFORMER,CAN,CAN]
        ]
      )
    , (FLAMESPITTER   , join $ takeOne 
        [rc $ map makeTypeCraft [LIGHTER,PUMP,CAN]
        ]
      )
    , (FLATSHIELD   , join $ takeOne 
        [rc $ map makeTypeCraft [PLATE,PLATE]
        ,rc $ map makeTypeCraft [DRUM,DRUM,PLATE]
        ]
      )
    , (SNIPERRIFLE   , join $ takeOne 
        [rc [sniperRifle]
        ]
      )
    , (FORCEFIELDGUN   , join $ takeOne 
        [rc [forceFieldGun]
        ]
      )
    , (KEYCARD 0   , join $ takeOne 
        [rc [keyCard 0]
        ]
      )
    , (CRAFT INCENDIARYMODULE   , join $ takeOne 
        [rc [incendiaryModule]
        ]
      )
    , (CRAFT STATICMODULE   , join $ takeOne 
        [rc [makeTypeCraft STATICMODULE]
        ]
      )
    ]
  where
    rc its = do
        sc <- someCrits
        roomsContaining sc its

someCrits :: RandomGen g => State g [Creature]
someCrits = do
    nCrits <- state $ randomR (1,3)
    fmap (take nCrits) . shuffle $ [spreadGunCrit,pistolCrit,autoCrit,armourChaseCrit]
        ++ replicate 20 chaseCrit


--addcrits :: RandomGen g => [Item] -> State g (Tree Room)
--addcrits its = do
--    nCrits <- state $ randomR (1,3)
--    crits <- takeN nCrits <=< shuffle $ fmap PutCrit $ [spreadGunCrit,pistolCrit,autoCrit,armourChaseCrit]
--    return $ roomsContaining crits its


corridorBoss :: RandomGen g => Creature -> State g (MetaTree Room String)
corridorBoss cr = do
    endroom <- bossRoom cr
    rToOnward ("corridorBoss-"++_crName cr)
        $ treeFromPost (replicate 5 corridor) endroom
