{-# LANGUAGE Safe #-}

{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides an extended printer class that supports colours.
module Text.Chatty.Extended.Printer (ChExtendedPrinter(..),Tone(..),Colour(..),expandClr) where

import Text.Chatty.Printer

-- | Colour tone.
data Tone = Green | Red | Yellow | Blue | Black | White | Cyan | Magenta
-- | Colour brightness
data Colour = Dull Tone | Vivid Tone

-- | Typeclass for all printers that support colourized output.
class ChPrinter m => ChExtendedPrinter m where
  -- | Run the function with the given colour.
  ebracket :: Colour -> m a -> m a
  ebracket c m = do estart c; a <- m; efin; return a
  -- | Print the string in the given colour.
  eprint :: Colour -> String -> m ()
  eprint c = ebracket c . mprint
  -- | Print the string in the given colour and terminate the line.
  eprintLn :: Colour -> String -> m ()
  eprintLn c s = eprint c s >> mprintLn ""
  -- | Print the string in the given colour without masking.
  enomask :: Colour -> String -> m ()
  enomask c = ebracket c . mnomask
  -- | Print the string in the given colour without masking and terminate the line.
  enomaskLn :: Colour -> String -> m ()
  enomaskLn c s = enomask c s >> mprintLn ""
  -- | Start using the specified colour.
  estart :: Colour -> m ()
  -- | Reset colour.
  efin :: m ()


takeBrace 0 ('}':ss) = ""
takeBrace n ('}':ss) = '}' : takeBrace (n-1) ss
takeBrace n ('{':ss) = '{' : takeBrace (n+1) ss
takeBrace n (s:ss) = s : takeBrace n ss
takeBrace n [] = ""

splitBrace ss = let nm = takeBrace 0 ss in (nm, drop (length nm + 1) ss)

procClr c ss = let (nm,rm) = splitBrace ss in ebracket c (expandClr nm) >> expandClr rm

expandClr :: ChExtendedPrinter m => String -> m ()
expandClr ('%':'{':'V':'0':';':ss) = procClr (Vivid Black) ss
expandClr ('%':'{':'V':'1':';':ss) = procClr (Vivid Red) ss
expandClr ('%':'{':'V':'2':';':ss) = procClr (Vivid Green) ss
expandClr ('%':'{':'V':'3':';':ss) = procClr (Vivid Yellow) ss
expandClr ('%':'{':'V':'4':';':ss) = procClr (Vivid Blue) ss
expandClr ('%':'{':'V':'5':';':ss) = procClr (Vivid Magenta) ss
expandClr ('%':'{':'V':'6':';':ss) = procClr (Vivid Cyan) ss
expandClr ('%':'{':'V':'7':';':ss) = procClr (Vivid White) ss
expandClr ('%':'{':'D':'0':';':ss) = procClr (Dull Black) ss
expandClr ('%':'{':'D':'1':';':ss) = procClr (Dull Red) ss
expandClr ('%':'{':'D':'2':';':ss) = procClr (Dull Green) ss
expandClr ('%':'{':'D':'3':';':ss) = procClr (Dull Yellow) ss
expandClr ('%':'{':'D':'4':';':ss) = procClr (Dull Blue) ss
expandClr ('%':'{':'D':'5':';':ss) = procClr (Dull Magenta) ss
expandClr ('%':'{':'D':'6':';':ss) = procClr (Dull Cyan) ss
expandClr ('%':'{':'D':'7':';':ss) = procClr (Dull White) ss
expandClr (s:ss) = mprint [s] >> expandClr ss
expandClr [] = return ()