-- | Command groups
module Calamity.Commands.Group
    ( Group(..) ) where

import           Calamity.Commands.Check
import {-# SOURCE #-} Calamity.Commands.Command
import {-# SOURCE #-} Calamity.Commands.Context

import           Control.Lens              hiding ( (<.>), Context )

import qualified Data.HashMap.Lazy         as LH
import qualified Data.Text                 as S
import qualified Data.Text.Lazy            as L

import           GHC.Generics

import           TextShow
import qualified TextShow.Generic          as TSG

-- | A group of commands
data Group = Group
  { Group -> Text
name     :: S.Text
  , Group -> Maybe Group
parent   :: Maybe Group
  , Group -> HashMap Text Command
commands :: LH.HashMap S.Text Command
    -- ^ Any child commands of this group
  , Group -> HashMap Text Group
children :: LH.HashMap S.Text Group
    -- ^ Any child groups of this group
  , Group -> Context -> Text
help     :: Context -> L.Text
    -- ^ A function producing the \'help' for the group
  , Group -> [Check]
checks   :: [Check]
    -- -- ^ A list of checks that must pass
  }
  deriving ( (forall x. Group -> Rep Group x)
-> (forall x. Rep Group x -> Group) -> Generic Group
forall x. Rep Group x -> Group
forall x. Group -> Rep Group x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Group x -> Group
$cfrom :: forall x. Group -> Rep Group x
Generic )

data GroupS = GroupS
  { GroupS -> Text
name     :: S.Text
  , GroupS -> Maybe Text
parent   :: Maybe S.Text
  , GroupS -> HashMap Text Command
commands :: LH.HashMap S.Text Command
  , GroupS -> HashMap Text Group
children :: LH.HashMap S.Text Group
  }
  deriving ( (forall x. GroupS -> Rep GroupS x)
-> (forall x. Rep GroupS x -> GroupS) -> Generic GroupS
forall x. Rep GroupS x -> GroupS
forall x. GroupS -> Rep GroupS x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GroupS x -> GroupS
$cfrom :: forall x. GroupS -> Rep GroupS x
Generic, Int -> GroupS -> ShowS
[GroupS] -> ShowS
GroupS -> String
(Int -> GroupS -> ShowS)
-> (GroupS -> String) -> ([GroupS] -> ShowS) -> Show GroupS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GroupS] -> ShowS
$cshowList :: [GroupS] -> ShowS
show :: GroupS -> String
$cshow :: GroupS -> String
showsPrec :: Int -> GroupS -> ShowS
$cshowsPrec :: Int -> GroupS -> ShowS
Show )
  deriving ( Int -> GroupS -> Builder
Int -> GroupS -> Text
Int -> GroupS -> Text
[GroupS] -> Builder
[GroupS] -> Text
[GroupS] -> Text
GroupS -> Builder
GroupS -> Text
GroupS -> Text
(Int -> GroupS -> Builder)
-> (GroupS -> Builder)
-> ([GroupS] -> Builder)
-> (Int -> GroupS -> Text)
-> (GroupS -> Text)
-> ([GroupS] -> Text)
-> (Int -> GroupS -> Text)
-> (GroupS -> Text)
-> ([GroupS] -> Text)
-> TextShow GroupS
forall a.
(Int -> a -> Builder)
-> (a -> Builder)
-> ([a] -> Builder)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> TextShow a
showtlList :: [GroupS] -> Text
$cshowtlList :: [GroupS] -> Text
showtl :: GroupS -> Text
$cshowtl :: GroupS -> Text
showtlPrec :: Int -> GroupS -> Text
$cshowtlPrec :: Int -> GroupS -> Text
showtList :: [GroupS] -> Text
$cshowtList :: [GroupS] -> Text
showt :: GroupS -> Text
$cshowt :: GroupS -> Text
showtPrec :: Int -> GroupS -> Text
$cshowtPrec :: Int -> GroupS -> Text
showbList :: [GroupS] -> Builder
$cshowbList :: [GroupS] -> Builder
showb :: GroupS -> Builder
$cshowb :: GroupS -> Builder
showbPrec :: Int -> GroupS -> Builder
$cshowbPrec :: Int -> GroupS -> Builder
TextShow ) via TSG.FromGeneric GroupS

instance Show Group where
  showsPrec :: Int -> Group -> ShowS
showsPrec d :: Int
d Group { Text
name :: Text
$sel:name:Group :: Group -> Text
name, Maybe Group
parent :: Maybe Group
$sel:parent:Group :: Group -> Maybe Group
parent, HashMap Text Command
commands :: HashMap Text Command
$sel:commands:Group :: Group -> HashMap Text Command
commands, HashMap Text Group
children :: HashMap Text Group
$sel:children:Group :: Group -> HashMap Text Group
children } = Int -> GroupS -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
d (GroupS -> ShowS) -> GroupS -> ShowS
forall a b. (a -> b) -> a -> b
$ Text
-> Maybe Text
-> HashMap Text Command
-> HashMap Text Group
-> GroupS
GroupS Text
name (Maybe Group
parent Maybe Group
-> Getting (First Text) (Maybe Group) Text -> Maybe Text
forall s a. s -> Getting (First a) s a -> Maybe a
^? (Group -> Const (First Text) Group)
-> Maybe Group -> Const (First Text) (Maybe Group)
forall a b. Prism (Maybe a) (Maybe b) a b
_Just ((Group -> Const (First Text) Group)
 -> Maybe Group -> Const (First Text) (Maybe Group))
-> ((Text -> Const (First Text) Text)
    -> Group -> Const (First Text) Group)
-> Getting (First Text) (Maybe Group) Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IsLabel
  "name"
  ((Text -> Const (First Text) Text)
   -> Group -> Const (First Text) Group)
(Text -> Const (First Text) Text)
-> Group -> Const (First Text) Group
#name) HashMap Text Command
commands HashMap Text Group
children

instance TextShow Group where
  showbPrec :: Int -> Group -> Builder
showbPrec d :: Int
d Group { Text
name :: Text
$sel:name:Group :: Group -> Text
name, Maybe Group
parent :: Maybe Group
$sel:parent:Group :: Group -> Maybe Group
parent, HashMap Text Command
commands :: HashMap Text Command
$sel:commands:Group :: Group -> HashMap Text Command
commands, HashMap Text Group
children :: HashMap Text Group
$sel:children:Group :: Group -> HashMap Text Group
children } = Int -> GroupS -> Builder
forall a. TextShow a => Int -> a -> Builder
showbPrec Int
d (GroupS -> Builder) -> GroupS -> Builder
forall a b. (a -> b) -> a -> b
$ Text
-> Maybe Text
-> HashMap Text Command
-> HashMap Text Group
-> GroupS
GroupS Text
name (Maybe Group
parent Maybe Group
-> Getting (First Text) (Maybe Group) Text -> Maybe Text
forall s a. s -> Getting (First a) s a -> Maybe a
^? (Group -> Const (First Text) Group)
-> Maybe Group -> Const (First Text) (Maybe Group)
forall a b. Prism (Maybe a) (Maybe b) a b
_Just ((Group -> Const (First Text) Group)
 -> Maybe Group -> Const (First Text) (Maybe Group))
-> ((Text -> Const (First Text) Text)
    -> Group -> Const (First Text) Group)
-> Getting (First Text) (Maybe Group) Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IsLabel
  "name"
  ((Text -> Const (First Text) Text)
   -> Group -> Const (First Text) Group)
(Text -> Const (First Text) Text)
-> Group -> Const (First Text) Group
#name) HashMap Text Command
commands HashMap Text Group
children