{-# LANGUAGE DeriveDataTypeable, TemplateHaskell, TypeFamilies, RecordWildCards, OverloadedStrings, QuasiQuotes #-}
module Clckwrks.Redirect.Acid
    ( -- * state
      RedirectState
    , initialRedirectState
      -- * events
    , GetRedirect(..)
--    , SetRedirect(..)
    ) where

import Clckwrks                (UserId(..))
-- import Clckwrks.Redirect.Types ()
import Control.Applicative     ((<$>))
import Control.Monad.Reader    (ask)
import Control.Monad.State     (get, modify, put)
import Control.Monad.Trans     (liftIO)
import Data.Acid               (AcidState, Query, Update, makeAcidic)
import Data.Data               (Data, Typeable)
import Data.IxSet              (Indexable, IxSet, (@=), Proxy(..), empty, fromList, getOne, ixSet, ixFun, insert, toList, toDescList, updateIx)
import           Data.Map      (Map)
import qualified Data.Map      as Map
import Data.Maybe              (fromJust)
import Data.SafeCopy           (Migrate(..), base, deriveSafeCopy, extension)
import Data.String             (fromString)
import Data.Text               (Text)
import qualified Data.Text     as Text
-- import           Data.UUID     (UUID)
-- import qualified Data.UUID     as UUID
-- import HSP.Google.Analytics (UACCT)

data RedirectState = RedirectState
    { RedirectState -> Map [Text] Text
redirects  :: Map [Text] Text
    }
    deriving (RedirectState -> RedirectState -> Bool
(RedirectState -> RedirectState -> Bool)
-> (RedirectState -> RedirectState -> Bool) -> Eq RedirectState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RedirectState -> RedirectState -> Bool
$c/= :: RedirectState -> RedirectState -> Bool
== :: RedirectState -> RedirectState -> Bool
$c== :: RedirectState -> RedirectState -> Bool
Eq, ReadPrec [RedirectState]
ReadPrec RedirectState
Int -> ReadS RedirectState
ReadS [RedirectState]
(Int -> ReadS RedirectState)
-> ReadS [RedirectState]
-> ReadPrec RedirectState
-> ReadPrec [RedirectState]
-> Read RedirectState
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RedirectState]
$creadListPrec :: ReadPrec [RedirectState]
readPrec :: ReadPrec RedirectState
$creadPrec :: ReadPrec RedirectState
readList :: ReadS [RedirectState]
$creadList :: ReadS [RedirectState]
readsPrec :: Int -> ReadS RedirectState
$creadsPrec :: Int -> ReadS RedirectState
Read, Int -> RedirectState -> ShowS
[RedirectState] -> ShowS
RedirectState -> String
(Int -> RedirectState -> ShowS)
-> (RedirectState -> String)
-> ([RedirectState] -> ShowS)
-> Show RedirectState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RedirectState] -> ShowS
$cshowList :: [RedirectState] -> ShowS
show :: RedirectState -> String
$cshow :: RedirectState -> String
showsPrec :: Int -> RedirectState -> ShowS
$cshowsPrec :: Int -> RedirectState -> ShowS
Show, Typeable RedirectState
DataType
Constr
Typeable RedirectState
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> RedirectState -> c RedirectState)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c RedirectState)
-> (RedirectState -> Constr)
-> (RedirectState -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c RedirectState))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c RedirectState))
-> ((forall b. Data b => b -> b) -> RedirectState -> RedirectState)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> RedirectState -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> RedirectState -> r)
-> (forall u. (forall d. Data d => d -> u) -> RedirectState -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> RedirectState -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> RedirectState -> m RedirectState)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> RedirectState -> m RedirectState)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> RedirectState -> m RedirectState)
-> Data RedirectState
RedirectState -> DataType
RedirectState -> Constr
(forall b. Data b => b -> b) -> RedirectState -> RedirectState
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RedirectState -> c RedirectState
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c RedirectState
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> RedirectState -> u
forall u. (forall d. Data d => d -> u) -> RedirectState -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RedirectState -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RedirectState -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> RedirectState -> m RedirectState
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> RedirectState -> m RedirectState
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c RedirectState
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RedirectState -> c RedirectState
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c RedirectState)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c RedirectState)
$cRedirectState :: Constr
$tRedirectState :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> RedirectState -> m RedirectState
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> RedirectState -> m RedirectState
gmapMp :: (forall d. Data d => d -> m d) -> RedirectState -> m RedirectState
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> RedirectState -> m RedirectState
gmapM :: (forall d. Data d => d -> m d) -> RedirectState -> m RedirectState
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> RedirectState -> m RedirectState
gmapQi :: Int -> (forall d. Data d => d -> u) -> RedirectState -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> RedirectState -> u
gmapQ :: (forall d. Data d => d -> u) -> RedirectState -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> RedirectState -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RedirectState -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RedirectState -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RedirectState -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RedirectState -> r
gmapT :: (forall b. Data b => b -> b) -> RedirectState -> RedirectState
$cgmapT :: (forall b. Data b => b -> b) -> RedirectState -> RedirectState
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c RedirectState)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c RedirectState)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c RedirectState)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c RedirectState)
dataTypeOf :: RedirectState -> DataType
$cdataTypeOf :: RedirectState -> DataType
toConstr :: RedirectState -> Constr
$ctoConstr :: RedirectState -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c RedirectState
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c RedirectState
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RedirectState -> c RedirectState
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RedirectState -> c RedirectState
$cp1Data :: Typeable RedirectState
Data, Typeable)
deriveSafeCopy 0 'base ''RedirectState

initialRedirectState :: IO RedirectState
initialRedirectState :: IO RedirectState
initialRedirectState =
  RedirectState -> IO RedirectState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RedirectState -> IO RedirectState)
-> RedirectState -> IO RedirectState
forall a b. (a -> b) -> a -> b
$ RedirectState :: Map [Text] Text -> RedirectState
RedirectState { redirects :: Map [Text] Text
redirects = Map [Text] Text
forall k a. Map k a
Map.empty }

getRedirect :: [Text]
            -> Query RedirectState (Maybe Text)
getRedirect :: [Text] -> Query RedirectState (Maybe Text)
getRedirect [Text]
paths =
  do RedirectState
s <- Query RedirectState RedirectState
forall r (m :: * -> *). MonadReader r m => m r
ask
     Maybe Text -> Query RedirectState (Maybe Text)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Text -> Query RedirectState (Maybe Text))
-> Maybe Text -> Query RedirectState (Maybe Text)
forall a b. (a -> b) -> a -> b
$ [Text] -> Map [Text] Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup [Text]
paths (RedirectState -> Map [Text] Text
redirects RedirectState
s)

makeAcidic ''RedirectState
  [ 'getRedirect
  ]