module Dodge.LevelGen.StaticWalls.Deprecated
    where
import Geometry
--import Geometry.ConvexPoly
--import FoldableHelp
import Dodge.LevelGen.StaticWalls

import Data.List.Extra
import Data.Maybe
-- the following is (while typing) a duplicate
type WallP = (Point2,Point2)
-- | Test whether lines are near parallel.
-- Assumes non-zero length lines.
nearParallel :: WallP -> WallP -> Bool
nearParallel (a,b) (x,y)
    = dotV (a -.- b) (vNormal (x -.- y)) < 0.01
-- | Test for near collinearity.
-- Assumes non-zero length lines.
nearCollinear :: WallP -> WallP -> Bool
nearCollinear (a,b) (x,y)
    | a /= x = nearParallel (a,b) (x,y) && nearParallel (a,b) (a,x)
    | otherwise = nearParallel (a,b) (x,y) && nearParallel (a,b) (a,y)

-- | Given a specific wall and list of walls, checks that the number of walls leaving the
-- first point is the same as the number of walls entering the first point of
-- the specific wall.
-- On success returns Nothing, on failure returns Just the specific wall and the
-- list of walls leaving the first point.
checkWallLeft :: WallP -> [WallP] -> Maybe (WallP,[WallP])
checkWallLeft (x,y) wls
    | length ins == length outs = Nothing
    | otherwise                 = Just ((x,y), outs)
  where
    ins     = filter (\(a,_) -> a == x) wls
    outs    = filter (\(_,b) -> b == x) wls
-- | Cut out a polygon from a set of walls, and check for errors in the
-- created walls.
-- Give error if created walls are not consistent.
cutWalls'' :: [Point2] -> [WallP] -> [WallP]
cutWalls'' ps wls = case mapMaybe (`checkWallRight` newWalls) newWalls of
    [] -> newWalls
    errs -> error $ "during level generation function cutWalls: when cutting poly:\n" ++ show ps 
        ++ "\nRight corner errors:\n"
        ++ unlines (map show errs)
        ++ "\nLeft corner errors:\n"
        ++ unlines (map show errsL)
        ++ "\nWalls before cut:\n"
        ++ unlines (map show wls)
        ++ "\nWalls after cut:\n"
        ++ unlines (map show newWalls)
  where
    newWalls = cutPoly ps wls
    errsL = mapMaybe (`checkWallLeft` newWalls) newWalls
-- | Given a specific wall and list of walls, checks that the number of walls leaving the
-- second point is the same as the number of walls entering the second point of
-- the specific wall.
-- On success returns Nothing, on failure returns Just the specific wall and the
-- list of walls leaving the second point.
checkWallRight :: WallP -> [WallP] -> Maybe (WallP,[WallP])
checkWallRight (x,y) wls 
    | length ins == length outs = Nothing
    | otherwise                 = Just ((x,y), outs)
  where
    ins     = filter (\(_,b) -> b == y) wls
    outs    = filter (\(a,_) -> a == y) wls
-- | Given a list of points and a point, returns the point if none in the list
-- is close to the point.
pointIfNotClose :: [Point2] -> Point2 -> Maybe Point2
pointIfNotClose ps p = case findClosePoint ps p of
    Nothing -> Just p
    _       -> Nothing
-- | Given a list of points and a point, returns a point in the list if any is close
-- to the point.
findClosePoint :: [Point2] -> Point2 -> Maybe Point2
findClosePoint ps p = find (\q -> dist p q < 5) ps
-----------------------DEBUG CODE:
-- | Given a polygon and list of walls, finds walls inside the polygon.
findWallsInPolygon :: [Point2] -> [WallP] -> [WallP]
findWallsInPolygon ps = filter cond
  where 
    cond wall = pointInOrOnPolygon (0.5 *.* uncurry (+.+) wall) ps

expandToSquare :: (Point2,Point2) -> [(Point2,Point2)]
expandToSquare (a,b) = [(a,b),(b,c),(c,d),(d,a)]
  where
    v = a -.- b
    c = b +.+ vNormal v
    d = a +.+ vNormal v
