{-# LANGUAGE FlexibleContexts, RankNTypes #-}

{-
  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 all methods for language intellection.
module Game.Antisplice.Lang (act,defVocab) where

import Text.Chatty.Printer
import Text.Chatty.Scanner
import Text.Chatty.Expansion
import Text.Chatty.Extended.Printer
import Game.Antisplice.Errors
import Game.Antisplice.Monad
import Game.Antisplice.Monad.Dungeon
import Game.Antisplice.Monad.Vocab
import Game.Antisplice.Utils.Graph
import Game.Antisplice.Utils.AVL
import Game.Antisplice.Utils.TST
import Game.Antisplice.Utils.None
import Game.Antisplice.Utils.ListBuilder
import Game.Antisplice.Rooms
import Game.Antisplice.Stats
import Game.Antisplice.Skills
import Game.Antisplice.Call
import Game.Antisplice.Action
import Control.Arrow
import Control.Monad.Error
import Text.Printf
import Data.Text (unpack)
import Data.Maybe
import Data.Char

aliases :: [(String,String)]
aliases = strictBuild $ do
  lit "l" "look"
  lit "n" "go north"
  lit "ne" "go northeast"
  lit "e"  "go east"
  lit "se" "go southeast"
  lit "s" "go south"
  lit "sw" "go southwest"
  lit "w" "go west"
  lit "nw" "go northwest"
  lit "u" "ascend"
  lit "d" "descend"
  lit "q" "quit"
  lit "i" "list inventory"
  lit "ex" "list exits"
  lit "get" "acquire"
  lit "take" "acquire"
  lit "show" "list"
  lit "sco" "list score"
  lit "eq" "list equipment"

defVocab :: TST Token
defVocab = foldr (\(k,v) -> tstInsert k (v k)) none $ strictBuild $ do
  lit "quit" Verb
  lit "acquire" Verb
  lit "drop" Verb
  lit "idiot" Noun
  lit "first" $ Ordn 1
  lit "next" $ Ordn 1
  lit "primary" $ Ordn 1
  lit "second" $ Ordn 2
  lit "third" $ Ordn 3
  lit "commit" Verb
  lit "suicide" Noun
  lit "go" Verb
  lit "ascend" Verb
  lit "descend" Verb
  lit "north" Fixe
  lit "south" Fixe
  lit "east" Fixe
  lit "west" Fixe
  lit "northeast" Fixe
  lit "northwest" Fixe
  lit "southeast" Fixe
  lit "southwest" Fixe
  lit "at" Prep
  lit "on" Prep
  lit "for" Prep
  lit "of" Prep
  lit "enter" Verb
  lit "list" Verb
  lit "exits" Fixe
  lit "inventory" Fixe
  lit "score" Fixe
  lit "main" Fixe
  lit "hand" Fixe
  lit "off" Fixe
  lit "chest" Fixe
  lit "feet" Fixe
  lit "wrists" Fixe
  lit "waist" Fixe
  lit "head" Fixe
  lit "legs" Fixe
  lit "back" Fixe
  lit "hands" Fixe
  lit "neck" Fixe
  lit "finger" Fixe
  lit "left" Fixe
  lit "right" Fixe
  lit "equipment" Fixe
  lit "equip" Verb

replaceAliases :: [String] -> [String]
replaceAliases [] = []
replaceAliases (s:ss) = words (aliasOf s) ++ replaceAliases ss
  where aliasOf x = case filter ((==x).fst) aliases of
          [] -> x
          (a:_) -> snd a

-- | Run a given input line.
act :: String -> ChattyDungeonM ()
act s = do
  let sp = replaceAliases $ words $ map toLower s
  ts <- mapM lookupVocab sp
  unless (null sp) $ do
    v1 <- lookupVocab (head sp)
    case v1 of
      Verb v -> runHandler $ runConsumer react sp
      Skilln n -> do
        ste <- totalStereo
        case stereoSkillBonus ste n of
          Nothing -> throwError CantCastThatNowError
          Just t -> runHandler $ t $ tail sp
      _ -> throwError VerbMustFirstError
  return ()

react :: Consumer
react = Nil #->> noneM
  #|| Verb "quit" #->> throwError QuitError
  #|| Verb "commit" :-: Noun "suicide" :-: Nil #->> (do
    eprintLn (Vivid Red) "You stab yourself in the chest, finally dying."
    throwError QuitError)
  #|| Verb "enter" :-: AvailableObject :-: Nil #-> objectTriggerOnEnterOf
  #|| Verb "ascend" #->> changeRoom Up
  #|| Verb "descend" #->> changeRoom Down
  #|| Verb "go" :-: CatchFixe :-: Nil #-> (\d -> changeRoom $ case d of
                                       "north" -> North
                                       "south" -> South
                                       "east" -> East
                                       "west" -> West
                                       "northeast" -> NorthEast
                                       "northwest" -> NorthWest
                                       "southeast" -> SouthEast
                                       "southwest" -> SouthWest
                                       "up" -> Up
                                       "down" -> Down)
  #|| Verb "list" :-: Fixe "exits" :-: Nil #->> (do
                                           s <- getDungeonState
                                           let (Just r) = currentRoomOf s
                                           forM_ (listEdges r $ roomsOf s) $ \(l,c,n) -> do
                                             b <- pathPrerequisiteOf c
                                             mprint "  " >> mprint (show l) >> mprint " -> "
                                             mprint =<< withRoom n getRoomTitle
                                             if not b then mprintLn " (locked)"
                                               else mprintLn "")
  #|| Verb "list" :-: Fixe "inventory" :-: Nil #->> (do
                                               ps <- getPlayerState
                                               mprintLn "Your inventory:"
                                               forM_ (avlInorder $ playerInventoryOf ps) $ \os -> mprintLn $ printf "  %s" $ unpack $ objectTitleOf os)
  #|| Verb "list" :-: Fixe "score" :-: Nil #->> (do
                                            [str, agi, sta, int, spi, arm, akp] <- mapM calcStat [Strength, Agility, Stamina, Intelligence, Spirit, Armor, AttackPower]
                                            [hst, gcd] <- mapM calcStat [Haste, CooldownDuration]
                                            mprintLn $ printf "Strength:     %5i   Agility: %5i" str agi
                                            mprintLn $ printf "Intelligence: %5i   Spirit:  %5i" int spi
                                            mprintLn $ printf "Stamina:      %5i   Armor:   %5i" sta arm
                                            mprintLn $ printf "Attack power: %5i   Haste:   %5i" akp hst
                                            mprintLn $ printf "Global cooldown:  %5i" gcd)
  #|| Verb "equip" :-: CarriedObject :-: Nil #-> (equipObject >=> \o -> case o of
                                             Nothing -> noneM
                                             Just o -> modifyPlayerState $ \s -> s{playerInventoryOf=avlInsert o $ playerInventoryOf s})
  #|| Verb "equip" :-: CarriedObject :-: Prep "at" :-: Remaining :-: Nil #-> (\(o,ps) -> do
                                                                                 k <- case ps of
                                                                                   ["main","hand"] -> return MainHand
                                                                                   ["off","hand"] -> return OffHand
                                                                                   ["chest"] -> return Chest
                                                                                   ["feet"] -> return Feet
                                                                                   ["wrists"] -> return Wrists
                                                                                   ["waist"] -> return Waist
                                                                                   ["head"] -> return Head
                                                                                   ["legs"] -> return Legs
                                                                                   ["back"] -> return Back
                                                                                   ["hands"] -> return Hands
                                                                                   ["neck"] -> return Neck
                                                                                   ["right","finger"] -> return Finger1
                                                                                   ["left","finger"] -> return Finger2
                                                                                   _ -> throwError UnintellegibleError
                                                                                 equipObjectAt k o >>= \o -> case o of
                                                                                   Nothing -> noneM
                                                                                   Just o -> modifyPlayerState $ \s -> s{playerInventoryOf=avlInsert o $ playerInventoryOf s})
  #|| Verb "list" :-: Fixe "equipment" :-: Nil #->> (
    let pr (Just k) s = mprintLn $ printf "%-15s%s" s $ unpack $ objectTitleOf k
        pr Nothing _ = noneM
        firstM = runKleisli . first . Kleisli
    in mapM_ (firstM getEquipment >=> uncurry pr) $ lazyBuild $ do
      lit MainHand "Main hand:"
      lit OffHand "Off hand:"
      lit Chest "Chest:"
      lit Feet "Feet:"
      lit Wrists "Wrists:"
      lit Waist "Waist:"
      lit Head "Head:"
      lit Legs "Legs:"
      lit Back "Back:"
      lit Hands "Hands:"
      lit Neck "Neck:"
      lit Finger1 "Right finger:"
      lit Finger2 "Left finger:")
  #|| Verb "acquire" :-: SeenObject :-: Nil #-> acquireObject . objectIdOf
  #|| Verb "drop" :-: CarriedObject :-: Nil #-> dropObject . objectIdOf