-------------------------------------------------------------------------------
-- Print convenience functions
-- 2017 Francesco Ariis GPLv3
-------------------------------------------------------------------------------

-- Drawing primitives. If not stated otherwise (textbox, etc.), ' ' are
-- assumed to be opaque

module Terminal.Game.Draw (module Terminal.Game.Draw,
                           module DF) where

import Terminal.Game.Plane

import Text.LineBreak

import Data.Function as DF ( (&) )

-----------
-- TYPES --
-----------

type Draw = Plane -> Plane

(%) :: Coords -> Plane -> Draw
cds % p1 = \p2 -> pastePlane p1 p2 cds

    -- most of the drawing is done with % and &, e.g.
    --
    -- let d :: Plane
    --     d =          blankPlane (100, 100) &
    --         (3, 4) % box '_' Yellow (3, 5) &
    --         (a, b) % butCell '@' Red
    --

-------------
-- DRAWING --
-------------

box :: Char -> Width -> Height -> Plane
box chr w h = seqCellsDim w h cells
    where
          cells = [((r, c), chr) | r <- [1..h], c <- [1..w]]

cell :: Char -> Plane
cell ch = box ch 1 1

-- opaque :: Plane -> Plane
-- opaque p = pastePlane p (box ' ' White w h) (1, 1)
--     where
--           (w, h) = pSize p

-- assumes ' ' are transparent
textBox :: String -> Width -> Height -> Plane
textBox cs w h = transparent
    where
          -- hypenathion
          hyp = Nothing -- Just english_GB
          bf  = BreakFormat (fromIntegral w) 4 '-' hyp
          hcs = breakStringLn bf (take (fromIntegral $ w*h) cs)
          hl  = fromIntegral $ length hcs

          f :: [String] -> [(Coords, Char)]
          f css = concatMap (uncurry rf) (zip [1..] css)
              where rf :: Integer -> String -> [(Coords, Char)]
                    rf cr ln = zip (zip (repeat cr) [1..]) ln

          out         = seqCellsDim w h (f hcs)
          transparent = addVitrum ' ' out


-----------------
-- ANCILLARIES --
-----------------

seqCellsDim :: Width -> Height -> [(Coords, Char)] -> Plane
seqCellsDim w h cells = seqCells (blankPlane w h) cells

seqCells :: Plane -> [(Coords, Char)] -> Plane
seqCells p cells = updatePlane p (map f cells)
    where
          f (cds, chr) = (cds, creaCell chr)