{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies, TemplateHaskell #-}

-- | Provides a bunch of derived instances for the various typeclasses.
module Text.Chatty.Interactor where

import Text.Chatty.Printer
import Text.Chatty.Scanner
import Text.Chatty.Finalizer
import Text.Chatty.Expansion
import Text.Chatty.Extended.HTML
import Text.Chatty.Extended.ANSI
import System.Chatty.Misc
import Text.Chatty.Interactor.Templates
import System.Chatty.Spawn
import System.Chatty.Spawn.Overlay
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Identity

mkInteractor ''RecorderT mkScanner mkFinalizer mkExpander mkSpawn mkRandom mkClock
mkInteractor ''DeafT mkScanner mkFinalizer mkExpander mkSpawn mkRandom mkClock
mkInteractor ''OutRedirT mkScanner mkFinalizer mkExpander mkSpawn mkRandom mkClock
mkInteractor ''HandleCloserT mkScanner mkPrinter mkExpander mkSpawn mkRandom mkClock
mkInteractor ''ExpanderT mkScanner mkPrinter mkFinalizer mkSpawn mkRandom mkClock
mkInteractor ''HereStringT mkPrinter mkExpander mkSpawn mkRandom mkClock
mkInteractor ''QuietT mkPrinter mkExpander mkSpawn mkRandom mkClock
mkInteractor ''InRedirT mkPrinter mkExpander mkSpawn mkRandom mkClock
mkInteractor ''SpawnOverlayT mkPrinter mkScanner mkExpander mkFinalizer mkRandom mkClock
mkInteractor ''HtmlPrinterT mkScanner mkExpander mkFinalizer mkSpawn mkRandom mkClock
mkInteractor ''AnsiPrinterT mkScanner mkExpander mkFinalizer mkSpawn mkRandom mkClock

-- | IgnorantT ignores all output and does not provide any input.
type IgnorantT m = QuietT (DeafT m)
-- | Ignorant is IgnorantT on the identity
type Ignorant = IgnorantT Identity
-- | ChattyT simulates a console, actually taking input as a string and recording output.
type ChattyT m = HereStringT (RecorderT m)
-- | Chatty is ChattyT on the identity
type Chatty = ChattyT Identity

-- | Run IgnorantT (does not take anything)
runIgnorantT :: Monad m => IgnorantT m a -> m a
runIgnorantT = runDeafT . runQuietT

-- | Run Ignorant (does not take anything)
runIgnorant :: Ignorant a -> a
runIgnorant = runIdentity . runIgnorantT

-- | Run ChattyT. Takes input as a string and returns (result, remaining input, output).
runChattyT :: (Monad m,Functor m) => ChattyT m a -> String -> m (a,String,Replayable)
runChattyT m input = fmap (\((a,u),r) -> (a,u,r)) $ runRecorderT $ runHereStringT m input

-- | Run Chatty. Takes input as a string and returns (result, remaining input, output).
runChatty :: Chatty a -> String -> (a,String,Replayable)
runChatty m = runIdentity . runChattyT m

-- Shell-like syntax
-- | Connect the output of some function to the input of another one. Compare with a pipe (cmd1 | cmd2).
(.|.) :: (Monad m,Functor m) => RecorderT m a -> HereStringT m b -> m b
m1 .|. m2 = do
  (_,r) <- runRecorderT m1
  fmap fst $ runHereStringT m2 (replay r)

-- | Runs the second function and feeds its output as an argument to the first one. Compare with process expansion ($(cmd)).
(.<$.) :: (Functor m,Monad m) => (String -> m b) -> RecorderT m a -> m b
m1 .<$. m2 = do
  (_,r) <- runRecorderT m2
  m1 $ replay r