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

{-
  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 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.Expansion.Vars
import Text.Chatty.Expansion.History
import Text.Chatty.Extended.HTML
import Text.Chatty.Extended.ANSI
import Text.Chatty.Channel.Printer
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
import System.IO

mkInteractor ''RecorderT mkScanner mkFinalizer mkExpander mkExpanderEnv mkHistoryEnv mkSpawn mkRandom mkClock
mkInteractor ''DeafT mkScanner mkFinalizer mkExpander mkExpanderEnv mkHistoryEnv mkSpawn mkRandom mkClock
mkInteractor ''OutRedirT mkScanner mkFinalizer mkExpander mkExpanderEnv mkHistoryEnv mkSpawn mkRandom mkClock
mkInteractor ''HandleCloserT mkScanner mkPrinter mkExpander mkExpanderEnv mkHistoryEnv mkSpawn mkRandom mkClock mkDefCP
mkInteractor ''ExpanderT mkScanner mkPrinter mkFinalizer mkSpawn mkRandom mkClock mkHistoryEnv mkDefCP
mkInteractor ''HereStringT mkPrinter mkExtendedPrinter mkExpander mkExpanderEnv mkHistoryEnv mkSpawn mkRandom mkClock mkDefCP
mkInteractor ''QuietT mkPrinter mkExtendedPrinter mkExpander mkExpanderEnv mkHistoryEnv mkSpawn mkRandom mkClock mkDefCP
mkInteractor ''InRedirT mkPrinter mkExtendedPrinter mkExpander mkExpanderEnv mkHistoryEnv mkSpawn mkRandom mkClock mkDefCP
mkInteractor ''SpawnOverlayT mkPrinter mkExtendedPrinter mkScanner mkExpander mkExpanderEnv mkHistoryEnv mkFinalizer mkRandom mkClock mkDefCP
mkInteractor ''HtmlPrinterT mkScanner mkExpanderEnv mkHistoryEnv mkFinalizer mkSpawn mkRandom mkClock mkDefCP
mkInteractor ''AnsiPrinterT mkScanner mkExpanderEnv mkHistoryEnv mkFinalizer mkSpawn mkRandom mkClock mkDefCP
mkInteractor ''NullExpanderT mkScanner mkPrinter mkExtendedPrinter mkFinalizer mkSpawn mkRandom mkClock mkDefCP
mkInteractor ''HistoryT  mkScanner mkPrinter mkExtendedPrinter mkFinalizer mkSpawn mkRandom mkClock mkExpanderEnv mkDefCP
mkInteractor ''IntArchiverT mkArchiver
mkInteractor ''BoolArchiverT mkArchiver
mkInteractor ''HandleArchiverT mkArchiver
mkInteractor ''IntFilterT mkArchiver
mkInteractor ''BoolFilterT mkArchiver
mkInteractor ''HandleFilterT mkArchiver
mkInteractor ''JoinerT mkArchiver

-- | 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