{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Reaktor
    ( module Exports
    , privmsg
    , run
    ) where

import Blessings
import Control.Concurrent.Extended
import Control.Exception
import Data.Attoparsec.ByteString.Char8 (feed,parse)
import Data.Attoparsec.ByteString.Char8 (IResult(Done,Fail,Partial))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8.Extended as BS
import Data.Foldable (toList)
import Data.Time.Clock.System
import Data.Time.Format
import qualified Network.Simple.TCP as TCP
import qualified Network.Simple.TCP.TLS as TLS
import Network.Socket as Exports (HostName,ServiceName)
import Prelude.Extended
import Reaktor.Internal
import Reaktor.Internal as Exports (Actions(..))
import Reaktor.Internal as Exports (Message(Message,Start))
import Reaktor.Internal as Exports (formatMessage)
import Reaktor.Nick as Exports
import Reaktor.Nick as Nick
import qualified Reaktor.Parser as Parser
import qualified System.IO
import System.IO (BufferMode(LineBuffering),hSetBuffering)
import System.IO (hIsTerminalDevice)
import System.Posix.Signals


run :: Config -> (Actions -> IO [Message -> IO ()]) -> IO ()
run Config{..} getPlugins =
    if cUseTLS then do
      s <- TLS.getDefaultClientSettings (cHostName, BS.pack cServiceName)
      TLS.connect s cHostName cServiceName $ \(ctx, sockAddr) ->
        withSocket sockAddr (TLS.send ctx) (TLS.recv ctx)
    else do
      TCP.connect cHostName cServiceName $ \(sock, sockAddr) ->
        withSocket sockAddr (TCP.send sock) (TCP.recv sock 512)
  where
    withSocket _sockAddr sockSend sockRecv = do

      hSetBuffering cLogHandle LineBuffering -- TODO reset
      logToTTY <- hIsTerminalDevice cLogHandle
      (putLog, takeLog0) <- newChan
      let
          takeLog1 = if cLogTime then takeLog0 >>= prefixTimestamp else takeLog0
          takeLog2 = if logToTTY then takeLog1 else stripSGR <$> takeLog1
          takeLog = takeLog2

      (putInMsg, takeInMsg) <- newChan
      (putOutMsg, takeOutMsg) <- newChan
      (shutdown, awaitShutdown) <- newSemaphore
      (aSetNick,aGetNick) <- newRef =<< maybe Nick.getRandom return cNick

      let actions = Actions{..}
          aIsSecure = cUseTLS
          aLog = putLog
          aSend msg = logMsg msg >> putOutMsg msg

          logMsg msg =
              case logMsgFilter msg of
                Just msg' -> do
                  let bs = formatMessage msg'
                  aLog $ SGR [38,5,235] "> " <> SGR [35,1] (Plain bs)
                Nothing -> return ()

      mapM_ (\(s, f) -> installHandler s (Catch f) Nothing) [
          (sigINT, shutdown)
        ]

      plugins <- getPlugins actions

      threads <- mapM (\f -> forkIO $ f `finally` shutdown) [
          receiver actions putInMsg sockRecv,
          logger cLogHandle takeLog,
          pinger aSend,
          sender takeOutMsg sockSend,
          splitter plugins takeInMsg
        ]

      putInMsg Start

      awaitShutdown
      mapM_ killThread threads
      putStrLn ""


logger :: System.IO.Handle -> IO (Blessings ByteString) -> IO ()
logger h takeLog = forever $ do
    s <- takeLog
    let s' = if lastChar s == '\n' then s else s <> Plain "\n"
    System.IO.hPutStr h $ pp $ fmap BS.unpack s'

pinger :: (Message -> IO ()) -> IO ()
pinger aSend = forever $ do
    threadDelay time
    aSend (Message Nothing "PING" ["heartbeat"])
  where
    time = 300 * 1000000

receiver :: Actions -> (Message -> IO ()) -> IO (Maybe ByteString) -> IO ()
receiver Actions{..} putInMsg sockRecv =
    receive ""
  where
    receive "" =
        sockRecv >>= \case
          Nothing -> logErr "EOL"
          Just buf -> receive buf

    receive buf =
        go (parse Parser.message buf)
      where
        go :: IResult ByteString Message -> IO ()
        go = \case
            Done rest msg -> do
              logMsg msg
              putInMsg msg
              receive rest

            p@(Partial _) ->
              sockRecv >>= \case
                Nothing -> logErr ("EOF with partial " <> Plain (BS.show p))
                Just msg -> go (feed p msg)

            f@(Fail _i _errorContexts _errMessage) ->
              logErr ("failed to parse message: " <> Plain (BS.show f))

    logErr s = aLog $ SGR [31,1] ("! receive: " <> s)

    logMsg msg =
        case logMsgFilter msg of
          Just msg' -> do
            let bs = formatMessage msg'
            aLog $ SGR [38,5,235] "< " <> SGR [38,5,244] (Plain bs)
          Nothing -> return ()


sender :: IO Message -> (ByteString -> IO ()) -> IO ()
sender takeOutMsg sockSend =
    forever $ takeOutMsg >>= sockSend . formatMessage

splitter :: [Message -> IO ()] -> IO Message -> IO ()
splitter plugins takeInMsg =
    forever $ do
      msg <- takeInMsg
      mapM_ (\f -> forkIO (f msg)) plugins


logMsgFilter :: Message -> Maybe Message
logMsgFilter = \case
    Message _ "PING" _ -> Nothing
    Message _ "PONG" _ -> Nothing
    Message p "PRIVMSG" ["NickServ",xs] | check -> do
        Just (Message p "PRIVMSG" ["NickServ",xs'])
      where
        check = elem cmd ["IDENTIFY","RELEASE"] && length ws > 2
        ws = BS.words xs
        (cmd:ws') = ws
        (nick:_) = ws'
        xs' = BS.unwords [cmd, nick, "<password>"]
    msg -> Just msg


privmsg :: ByteString -> [ByteString] -> Message
privmsg msgtarget xs =
    Message Nothing "PRIVMSG" (msgtarget:BS.intercalate " " xs:[])


lastChar :: Blessings ByteString -> Char
lastChar = BS.last . last . toList

prefixTimestamp :: Blessings ByteString -> IO (Blessings ByteString)
prefixTimestamp s = do
    t <- SGR [38,5,239] . Plain . BS.pack <$> getTimestamp
    return (t <> " " <> s)

stripSGR :: Blessings a -> Blessings a
stripSGR = \case
    Append t1 t2 -> Append (stripSGR t1) (stripSGR t2)
    SGR _ t -> stripSGR t
    Plain x -> Plain x
    Empty -> Empty


getTimestamp :: IO String
getTimestamp =
    formatTime defaultTimeLocale (iso8601DateFormat $ Just "%H:%M:%SZ")
    . systemToUTCTime <$> getSystemTime