{-
  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 ExtendedPrinter that handles colours using standardized ANSI codes.
module Text.Chatty.Extended.ANSI where

import qualified System.Console.ANSI as A
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.IO.Class
import Text.Chatty.Printer
import Text.Chatty.Extended.Printer
import Text.Chatty.Expansion

-- | An ExtendedPrinter that uses ANSI colour codes.
newtype AnsiPrinterT m a = AnsiPrinter { runAnsiPrinterT :: [Colour] -> m (a,[Colour]) }

instance Monad m => Monad (AnsiPrinterT m) where
  return a = AnsiPrinter $ \s -> return (a,s)
  (AnsiPrinter p) >>= f = AnsiPrinter $ \s -> do (a,s') <- p s; runAnsiPrinterT (f a) s'

instance MonadTrans AnsiPrinterT where
  lift m = AnsiPrinter $ \c -> do a <- m; return (a,c)

instance Monad m => Functor (AnsiPrinterT m) where
  fmap f a = AnsiPrinter $ \s -> do (a',s') <- runAnsiPrinterT a s; return (f a',s')

instance MonadIO m => MonadIO (AnsiPrinterT m) where
  liftIO = lift . liftIO

instance MonadPrinter m => MonadPrinter (AnsiPrinterT m) where
  mprint = lift . mprint
  mnoecho = lift . mnoecho
  mflush = lift mflush
  mnomask = lift . mnomask

instance MonadPrinter m => ExtendedPrinter (AnsiPrinterT m) where
  estart c = AnsiPrinter $ \c1 -> do
    mprint $ A.setSGRCode [A.SetColor A.Foreground (mkColourInt c) (mkColourCode c)]
    return ((),c:c1)
  efin = AnsiPrinter $ \c1 ->
    case c1 of
      (_:c:cx) -> do
        mprint $ A.setSGRCode [A.SetColor A.Foreground (mkColourInt c) (mkColourCode c)]
        return ((),c:cx)
      _ -> do
        mprint $ A.setSGRCode [A.Reset]
        return ((),[])

instance (Functor m,MonadExpand m) => MonadExpand (AnsiPrinterT m) where
  expand s = AnsiPrinter $ \cx -> do
    s1 <- (expand =<<) $ liftM (replay.snd) $ runRecorderT $ liftM fst $ flip runAnsiPrinterT cx $ expandClr s
    return (s1, cx)

-- | Convert Chatty's colour intensity to ansi-terminal's one
mkColourInt (Dull _) = A.Dull
mkColourInt (Vivid _) = A.Vivid

-- | Convert Chatty's colour tone to ansi-terminal's one
mkColourCode (Dull c) = mkColourCode (Vivid c)
mkColourCode (Vivid c) = case c of
  Green -> A.Green
  Red -> A.Red
  Yellow -> A.Yellow
  Blue -> A.Blue
  Black -> A.Black
  White -> A.White
  Cyan -> A.Cyan
  Magenta -> A.Magenta