module Dodge.Initialisation where
import Dodge.Default.World
import Dodge.Save
import Dodge.Data
import Dodge.Data.SoundOrigin
import Dodge.Creature
import Dodge.Zone
import Dodge.Floor
import Dodge.Layout
import Dodge.Story
import Dodge.WorldEvent.Cloud
import Dodge.SoundLogic
import Dodge.SoundLogic.LoadSound
--import Dodge.Creature.Perception
import Geometry.Data
--import Shape.Data
--import Dodge.Render.Shape
--import Picture
--import Dodge.GameRoom
--import Geometry

import System.Random
import qualified Data.Set as S
import qualified Data.IntMap.Strict as IM
import qualified Data.Map as M
import Data.Maybe
import Control.Lens
import Data.List (partition)
--import Data.List (sortOn)

firstWorld :: IO World
firstWorld = do
--    i <- randomRIO (0,5000)
    let i = 2
    putStrLn $ "Seed for level generation: " ++ show ( i :: Int)
    return $ generateLevelFromRoomList levx $ initialWorld {_randGen = mkStdGen i}

initialWorld :: World
initialWorld = defaultWorld
    { _keys = S.empty
    , _cameraCenter = V2 0 0
    , _cameraRot = 0
    , _cameraZoom = 10
    , _creatures = IM.fromList [(0,startCr)]
    , _projectiles = IM.empty
    , _walls = IM.empty
    , _forceFields = IM.empty
    , _floorItems = IM.empty
    , _randGen = mkStdGen 2
    , _mousePos = V2 0 0
    , _testString = testStringInit
    , _yourID = 0
    , _worldEvents = storeLevel . soundStart BackgroundSound (V2 0 0) foamSprayFadeOutS Nothing 
        . foldr ((.) . makeStartCloudAt) id [V3 x y 5 | x <- [-5,-3..5] , y <- [-5,-3..5]]
    , _pressPlates = IM.empty
    , _buttons = IM.empty
    , _sounds = M.empty
    , _decorations = IM.empty
    , _storedLevel = Nothing
    , _menuLayers = [TerminalScreen 300 rezText']
    , _worldState = M.empty
    }
-- TODO delete this duplicate
wallsAndWindows
    :: World
    -> ( [((Point2,Point2),Point4)]  ,[((Point2,Point2),Point4)] )
wallsAndWindows w
    = (map f wls, map f $ filter (fromMaybe True . (^? blVisible)) wins)
  where
    f wl = (_wlLine wl, _wlColor wl)
    (wins,wls) = partition _wlIsSeeThrough . IM.elems $ wallsDoubleScreen w
testStringInit :: World -> [String]
testStringInit _ = []
--testStringInit w = 
--    [show $ _worldMinX w
--    ,show $ _worldMaxX w
--    ,show $ _worldMinY w
--    ,show $ _worldMaxY w
--    ]
--testStringInit = mapMaybe f . IM.elems . wallsDoubleScreen
--  where
--    f (dr@Door{}) = Just . show $ _wlLine dr
--    f _ = Nothing
--testStringInit = map show . filter ((== col) . snd) . fst . wallsAndWindows
--  where
--    col = dim $ dim $ bright red
--testStringInit = mapMaybe f . IM.elems . flattenIMIMIM . _wallsZone
--  where
--    f (dr@Door{}) = Just . show $ _wlLine dr
--    f _ = Nothing
--testStringInit _ = []
--testStringInit w = [show . length $ newSounds w]
--testStringInit w = (show . _crPos $ _creatures w IM.! 0) 
--    : (map show . _grBound . last $ sortOn _grName grs) 
--    ++ closeRooms
--  where
--    closeRooms = map _grName $ filter (pointInOrOnPolygon p . _grBound) grs
--    grs = _gameRooms w
--    p = _cameraViewFrom w
