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

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

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

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

  Antisplice 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 Antisplice. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides some class instances. Nothing really interesting here.
module Game.Antisplice.Monad where

import Game.Antisplice.Templates
import Game.Antisplice.Utils.AVL
import Game.Antisplice.Utils.None
import Game.Antisplice.Utils.Fail
import Game.Antisplice.Utils.Counter
import Game.Antisplice.Monad.Dungeon
import Game.Antisplice.Monad.Vocab
import Game.Antisplice.Utils.Atoms
import Game.Antisplice.Utils.BST
import Game.Antisplice.Errors
import Control.Monad
import Control.Monad.Trans.Class
import Text.Chatty.Expansion
import Text.Chatty.Expansion.Vars
import Text.Chatty.Templates
import Text.Chatty.Interactor
import Text.Chatty.Interactor.Templates
import Text.Chatty.Channel.Printer
import Text.Chatty.Channel.Broadcast
import Text.Chatty.Extended.HTML
import Text.Chatty.Extended.ANSI
import Text.Chatty.Printer
--import Language.Haskell.TH
import System.IO

mkInteractor ''SplErrT mkChatty mkRoom mkDungeon mkObject mkCounter mkPlayer mkIO mkVocab mkAtoms (mkChannelPrinter ''PlayerId)
mkInteractor ''DungeonT mkPrinter mkDefCP mkExtendedPrinter mkScanner mkFinalizer mkClock mkExpanderEnv mkHistoryEnv mkRandom (mkFail ''SplErr) mkCounter mkIO  mkVocab mkAtoms (mkChannelPrinter ''PlayerId)
mkInteractor ''RoomT mkChatty (mkFail ''SplErr) mkCounter mkIO mkVocab mkAtoms (mkChannelPrinter ''PlayerId)
mkInteractor ''ObjectT mkChatty (mkFail ''SplErr) mkCounter mkIO mkVocab mkAtoms (mkChannelPrinter ''PlayerId)
mkInteractor ''CounterT mkChatty (mkFail ''SplErr) mkRoom mkObject mkDungeon mkPlayer mkIO mkVocab mkAtoms (mkChannelPrinter ''PlayerId)
mkInteractor ''PlayerT mkChatty (mkFail ''SplErr) mkCounter mkIO mkVocab mkAtoms (mkChannelPrinter ''PlayerId)
mkInteractor ''VocabT mkChatty (mkFail ''SplErr) mkCounter mkDungeon mkRoom mkAtoms mkPlayer (mkChannelPrinter ''PlayerId)
mkInteractor ''AtomStoreT mkChatty (mkFail ''SplErr) mkDungeon mkRoom mkVocab mkPlayer (mkChannelPrinter ''PlayerId)
mkInteractor ''PlayerFilterT mkArchiver mkVocab mkAtoms mkCounter
liftM concat $ forM [''AnsiPrinterT,''HtmlPrinterT,''ExpanderT,''NullExpanderT,''RecorderT] $ \s ->
  mkInteractor s (mkChannelPrinter ''PlayerId) mkDungeon mkPlayer mkRoom (mkFail ''SplErr)

instance ChannelPrinter PlayerId m => Broadcaster PlayerId (DungeonT m) where
  bprint c str = do
    s <- getDungeonState
    forM_ (anyBstInorder $ playersOf s) $ \p -> do
      b <- c $ indexOf p
      when b $ cprint (indexOf p) str