module Dodge.Render.Walls
    ( wallsToDraw
    ) where
import Dodge.Data
import Dodge.Zone
import Dodge.Base.Wall
import Geometry
import ShapePicture

import Control.Lens
--import Data.Maybe
import qualified Control.Foldl as L
import qualified Streaming.Prelude as S
--import Data.List
--import qualified Data.IntMap.Strict as IM
wallsToDraw
    :: World
    -> ( [((Point2,Point2),Point4)]  ,[((Point2,Point2),Point4)], SPic )
wallsToDraw w
    = ( wls, wins,spic)
  where
    f wl = (_wlLine wl, _wlColor wl)
    (wls, wins, spic) = runIdentity $ L.purely S.fold_ 
        ((,,)
        <$> L.prefilter wlOpaqueDraw (L.premap f L.list)
        <*> L.prefilter wlSeeThroughDraw (L.premap f L.list)
        <*> L.premap getWallSPic L.mconcat
        ) (S.concat $ S.mapMaybe g $ zoneOfSight wlZoneSize w)
    g (V2 i j) = w ^? wlZoning . znObjects . ix i . ix j
    
wlOpaqueDraw :: Wall -> Bool
wlOpaqueDraw wl = wlIsOpaque wl && _wlDraw wl
wlSeeThroughDraw :: Wall -> Bool
wlSeeThroughDraw wl = wlIsSeeThrough wl && _wlDraw wl

getWallSPic :: Wall -> SPic
getWallSPic wl = case wl ^? wlOpacity . opDraw of
    Nothing -> mempty
    Just f  -> f wl

--    (wins,wls) = partition theTest . IM.elems . IM.filter _wlDraw $ wallsDoubleScreen cfig w
--    theTest wl = case _wlOpacity wl of
--        Opaque -> False
--        _ -> True

