{-# LANGUAGE OverloadedLabels #-}

module WikiMusic.SSR.Servant.SongRoutes
  ( songsRoute,
    songRoute,
    songCreateRoute,
  )
where

import Data.UUID (UUID)
import Free.AlaCarte
import Optics
import Relude
import Text.Blaze.Html as Html
import WikiMusic.Model.Other
import WikiMusic.SSR.Backend.Rest ()
import WikiMusic.SSR.Free.Backend
import WikiMusic.SSR.Free.View
import WikiMusic.SSR.Model.Api
import WikiMusic.SSR.Model.Env
import WikiMusic.SSR.Servant.Utilities
import WikiMusic.SSR.View.Html ()

songsRoute :: (MonadIO m) => Env -> Maybe Text -> Maybe Text -> Maybe Int -> Maybe Int -> m Html
songsRoute :: forall (m :: * -> *).
MonadIO m =>
Env -> Maybe Text -> Maybe Text -> Maybe Int -> Maybe Int -> m Html
songsRoute Env
env Maybe Text
cookie Maybe Text
givenSortOrder Maybe Int
limit Maybe Int
offset = do
  Either Text GetSongsQueryResponse
maybeSongs <-
    IO (Either Text GetSongsQueryResponse)
-> m (Either Text GetSongsQueryResponse)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
      (IO (Either Text GetSongsQueryResponse)
 -> m (Either Text GetSongsQueryResponse))
-> IO (Either Text GetSongsQueryResponse)
-> m (Either Text GetSongsQueryResponse)
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @Backend
        ( Env
-> AuthToken
-> Limit
-> Offset
-> SortOrder
-> Include
-> Free Backend (Either Text GetSongsQueryResponse)
forall (f :: * -> *).
(Backend :<: f) =>
Env
-> AuthToken
-> Limit
-> Offset
-> SortOrder
-> Include
-> Free f (Either Text GetSongsQueryResponse)
getSongs
            Env
env
            (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars AuthToken -> AuthToken
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars AuthToken
#authToken)
            (Limit -> (Int -> Limit) -> Maybe Int -> Limit
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> Limit
Limit Int
50) Int -> Limit
Limit Maybe Int
limit)
            (Offset -> (Int -> Offset) -> Maybe Int -> Offset
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> Offset
Offset Int
0) Int -> Offset
Offset Maybe Int
offset)
            SortOrder
sortOrder
            (Include {$sel:value:Include :: Text
value = Text
"artworks,comments,opinions"})
        )
  Env
-> UiMode
-> Language
-> Palette
-> Either Text GetSongsQueryResponse
-> (GetSongsQueryResponse -> IO Html)
-> m Html
forall (m :: * -> *) t.
MonadIO m =>
Env
-> UiMode
-> Language
-> Palette
-> Either Text t
-> (t -> IO Html)
-> m Html
eitherView
    Env
env
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode)
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale)
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette)
    Either Text GetSongsQueryResponse
maybeSongs
    (forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @View (Free View Html -> IO Html)
-> (GetSongsQueryResponse -> Free View Html)
-> GetSongsQueryResponse
-> IO Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Env
-> UiMode
-> Language
-> Palette
-> SortOrder
-> GetSongsQueryResponse
-> Free View Html
forall (f :: * -> *).
(View :<: f) =>
Env
-> UiMode
-> Language
-> Palette
-> SortOrder
-> GetSongsQueryResponse
-> Free f Html
songListPage Env
env (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette) SortOrder
sortOrder)
  where
    viewVars :: ViewVars
viewVars = Maybe Text -> ViewVars
viewVarsFromCookies Maybe Text
cookie
    sortOrder :: SortOrder
sortOrder = SortOrder -> (Text -> SortOrder) -> Maybe Text -> SortOrder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars SortOrder -> SortOrder
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars SortOrder
#songSorting) Text -> SortOrder
SortOrder Maybe Text
givenSortOrder

songRoute :: (MonadIO m) => Env -> Maybe Text -> UUID -> m Html
songRoute :: forall (m :: * -> *).
MonadIO m =>
Env -> Maybe Text -> UUID -> m Html
songRoute Env
env Maybe Text
cookie UUID
identifier = do
  Either Text GetSongsQueryResponse
maybeSongs <-
    IO (Either Text GetSongsQueryResponse)
-> m (Either Text GetSongsQueryResponse)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
      (IO (Either Text GetSongsQueryResponse)
 -> m (Either Text GetSongsQueryResponse))
-> IO (Either Text GetSongsQueryResponse)
-> m (Either Text GetSongsQueryResponse)
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @Backend
        ( Env
-> AuthToken
-> UUID
-> Include
-> Free Backend (Either Text GetSongsQueryResponse)
forall (f :: * -> *).
(Backend :<: f) =>
Env
-> AuthToken
-> UUID
-> Include
-> Free f (Either Text GetSongsQueryResponse)
getSong
            Env
env
            (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars AuthToken -> AuthToken
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars AuthToken
#authToken)
            UUID
identifier
            (Include {$sel:value:Include :: Text
value = Text
"artworks,comments,opinions,contents"})
        )
  Env
-> UiMode
-> Language
-> Palette
-> Either Text GetSongsQueryResponse
-> (GetSongsQueryResponse -> IO Html)
-> m Html
forall (m :: * -> *) t.
MonadIO m =>
Env
-> UiMode
-> Language
-> Palette
-> Either Text t
-> (t -> IO Html)
-> m Html
eitherView
    Env
env
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode)
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale)
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette)
    Either Text GetSongsQueryResponse
maybeSongs
    (forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @View (Free View Html -> IO Html)
-> (GetSongsQueryResponse -> Free View Html)
-> GetSongsQueryResponse
-> IO Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Env
-> UiMode
-> Language
-> Palette
-> SongAsciiSize
-> GetSongsQueryResponse
-> Free View Html
forall (f :: * -> *).
(View :<: f) =>
Env
-> UiMode
-> Language
-> Palette
-> SongAsciiSize
-> GetSongsQueryResponse
-> Free f Html
songDetailPage Env
env (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette) (ViewVars
viewVars ViewVars
-> Optic' A_Lens NoIx ViewVars SongAsciiSize -> SongAsciiSize
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars SongAsciiSize
#songAsciiSize))
  where
    viewVars :: ViewVars
viewVars = Maybe Text -> ViewVars
viewVarsFromCookies Maybe Text
cookie

songCreateRoute :: (MonadIO m) => Env -> Maybe Text -> m Html
songCreateRoute :: forall (m :: * -> *). MonadIO m => Env -> Maybe Text -> m Html
songCreateRoute Env
env Maybe Text
cookie = do
  IO Html -> m Html
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Html -> m Html) -> IO Html -> m Html
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @View (Env -> UiMode -> Language -> Palette -> Free View Html
forall (f :: * -> *).
(View :<: f) =>
Env -> UiMode -> Language -> Palette -> Free f Html
songCreatePage Env
env (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette))
  where
    viewVars :: ViewVars
viewVars = Maybe Text -> ViewVars
viewVarsFromCookies Maybe Text
cookie