{-
  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 a monad that collects vocab during dungeon construction and
-- reproduces it later.
module Game.Antisplice.Monad.Vocab where

import Control.Arrow
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Game.Antisplice.Utils.TST

-- | Algebraic type for primitive and complex tokens.
data Token = Unintellegible String
           | Verb String
           | Prep String
           | Noun String
           | Adj String
           | Ordn Int String
           | Fixe String
           | Skilln String
           | Nounc [String] (Maybe Int) String
             -- ^ Complex noun, with attributes and maybe ordinal index.
             deriving Show

-- | The vocab monad. Carries the currently noun vocab as its state.
newtype VocabT m a = Vocab { runVocabT :: TST Token -> m (a,TST Token) }

instance Functor m => Functor (VocabT m) where
  fmap f a = Vocab $ \s -> fmap (first f) $ runVocabT a s

instance Monad m => Monad (VocabT m) where
  return a = Vocab $ \s -> return (a,s)
  m >>= f = Vocab $ \s -> do (a,s') <- runVocabT m s; runVocabT (f a) s'

instance MonadTrans VocabT where
  lift m = Vocab $ \s -> do a <- m; return (a,s)

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

-- | Tyepclass for all vocab-memorizing monads.
class Monad m => MonadVocab m where
  -- | Lookup a word and return its token.
  lookupVocab :: String -> m Token
  -- | Learn a new word.
  insertVocab :: String -> (String -> Token) -> m ()
  -- | Check if a word is already known.
  vocabKnown :: String -> m Bool

instance Monad m => MonadVocab (VocabT m) where
  lookupVocab k = Vocab $ \s -> case tstLookup k s of
    Just t -> return (t,s)
    Nothing -> return (Unintellegible k,s)
  insertVocab k tc = Vocab $ \s -> return ((),tstInsert k (tc k) s)
  vocabKnown k = Vocab $ \s -> return (tstContains k s,s)