{-# LANGUAGE TemplateHaskell #-}

-- | Command groups
module CalamityCommands.Group (Group (..)) where

import CalamityCommands.AliasType
import CalamityCommands.Check
import {-# SOURCE #-} CalamityCommands.Command
import Data.HashMap.Lazy qualified as LH
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty qualified as NE
import Data.Text qualified as T
import Optics
import TextShow qualified
import TextShow.TH (deriveTextShow)

-- | A group of commands
data Group m c a = Group
  { forall (m :: * -> *) c a. Group m c a -> NonEmpty Text
names :: NonEmpty T.Text
  , forall (m :: * -> *) c a. Group m c a -> Maybe (Group m c a)
parent :: Maybe (Group m c a)
  , forall (m :: * -> *) c a. Group m c a -> Bool
hidden :: Bool
  , forall (m :: * -> *) c a.
Group m c a -> HashMap Text (Command m c a, AliasType)
commands :: LH.HashMap T.Text (Command m c a, AliasType)
  -- ^ Any child commands of this group
  , forall (m :: * -> *) c a.
Group m c a -> HashMap Text (Group m c a, AliasType)
children :: LH.HashMap T.Text (Group m c a, AliasType)
  -- ^ Any child groups of this group
  , forall (m :: * -> *) c a. Group m c a -> c -> Text
help :: c -> T.Text
  -- ^ A function producing the \'help\' for the group
  , forall (m :: * -> *) c a. Group m c a -> [Check m c]
checks :: [Check m c]
  -- ^ A list of checks that must pass
  }

$(makeFieldLabelsNoPrefix ''Group)

data GroupS m c a = GroupS
  { forall (m :: * -> *) c a. GroupS m c a -> NonEmpty Text
names :: NonEmpty T.Text
  , forall (m :: * -> *) c a. GroupS m c a -> Maybe Text
parent :: Maybe T.Text
  , forall (m :: * -> *) c a.
GroupS m c a -> [(Text, (Command m c a, AliasType))]
commands :: [(T.Text, (Command m c a, AliasType))]
  , forall (m :: * -> *) c a.
GroupS m c a -> [(Text, (Group m c a, AliasType))]
children :: [(T.Text, (Group m c a, AliasType))]
  , forall (m :: * -> *) c a. GroupS m c a -> Bool
hidden :: Bool
  }
  deriving (Int -> GroupS m c a -> ShowS
[GroupS m c a] -> ShowS
GroupS m c a -> String
(Int -> GroupS m c a -> ShowS)
-> (GroupS m c a -> String)
-> ([GroupS m c a] -> ShowS)
-> Show (GroupS m c a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (m :: * -> *) c a.
(Show a, Show c) =>
Int -> GroupS m c a -> ShowS
forall (m :: * -> *) c a.
(Show a, Show c) =>
[GroupS m c a] -> ShowS
forall (m :: * -> *) c a.
(Show a, Show c) =>
GroupS m c a -> String
$cshowsPrec :: forall (m :: * -> *) c a.
(Show a, Show c) =>
Int -> GroupS m c a -> ShowS
showsPrec :: Int -> GroupS m c a -> ShowS
$cshow :: forall (m :: * -> *) c a.
(Show a, Show c) =>
GroupS m c a -> String
show :: GroupS m c a -> String
$cshowList :: forall (m :: * -> *) c a.
(Show a, Show c) =>
[GroupS m c a] -> ShowS
showList :: [GroupS m c a] -> ShowS
Show)

instance (Show a, Show c) => Show (Group m c a) where
  showsPrec :: Int -> Group m c a -> ShowS
showsPrec Int
d Group {NonEmpty Text
$sel:names:Group :: forall (m :: * -> *) c a. Group m c a -> NonEmpty Text
names :: NonEmpty Text
names, Maybe (Group m c a)
$sel:parent:Group :: forall (m :: * -> *) c a. Group m c a -> Maybe (Group m c a)
parent :: Maybe (Group m c a)
parent, HashMap Text (Command m c a, AliasType)
$sel:commands:Group :: forall (m :: * -> *) c a.
Group m c a -> HashMap Text (Command m c a, AliasType)
commands :: HashMap Text (Command m c a, AliasType)
commands, HashMap Text (Group m c a, AliasType)
$sel:children:Group :: forall (m :: * -> *) c a.
Group m c a -> HashMap Text (Group m c a, AliasType)
children :: HashMap Text (Group m c a, AliasType)
children, Bool
$sel:hidden:Group :: forall (m :: * -> *) c a. Group m c a -> Bool
hidden :: Bool
hidden} =
    Int -> GroupS m c a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
d (GroupS m c a -> ShowS) -> GroupS m c a -> ShowS
forall a b. (a -> b) -> a -> b
$ NonEmpty Text
-> Maybe Text
-> [(Text, (Command m c a, AliasType))]
-> [(Text, (Group m c a, AliasType))]
-> Bool
-> GroupS m c a
forall (m :: * -> *) c a.
NonEmpty Text
-> Maybe Text
-> [(Text, (Command m c a, AliasType))]
-> [(Text, (Group m c a, AliasType))]
-> Bool
-> GroupS m c a
GroupS NonEmpty Text
names (NonEmpty Text -> Text
forall a. NonEmpty a -> a
NE.head (NonEmpty Text -> Text) -> Maybe (NonEmpty Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Group m c a)
parent Maybe (Group m c a)
-> Optic'
     An_AffineTraversal NoIx (Maybe (Group m c a)) (NonEmpty Text)
-> Maybe (NonEmpty Text)
forall k s (is :: IxList) a.
Is k An_AffineFold =>
s -> Optic' k is s a -> Maybe a
^? Prism
  (Maybe (Group m c a))
  (Maybe (Group m c a))
  (Group m c a)
  (Group m c a)
forall a b. Prism (Maybe a) (Maybe b) a b
_Just Prism
  (Maybe (Group m c a))
  (Maybe (Group m c a))
  (Group m c a)
  (Group m c a)
-> Optic
     A_Lens
     NoIx
     (Group m c a)
     (Group m c a)
     (NonEmpty Text)
     (NonEmpty Text)
-> Optic'
     An_AffineTraversal NoIx (Maybe (Group m c a)) (NonEmpty Text)
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic
  A_Lens
  NoIx
  (Group m c a)
  (Group m c a)
  (NonEmpty Text)
  (NonEmpty Text)
#names) (HashMap Text (Command m c a, AliasType)
-> [(Text, (Command m c a, AliasType))]
forall k v. HashMap k v -> [(k, v)]
LH.toList HashMap Text (Command m c a, AliasType)
commands) (HashMap Text (Group m c a, AliasType)
-> [(Text, (Group m c a, AliasType))]
forall k v. HashMap k v -> [(k, v)]
LH.toList HashMap Text (Group m c a, AliasType)
children) Bool
hidden

$(deriveTextShow ''GroupS)

instance (TextShow.TextShow a, TextShow.TextShow c) => TextShow.TextShow (Group m c a) where
  showbPrec :: Int -> Group m c a -> Builder
showbPrec Int
d Group {NonEmpty Text
$sel:names:Group :: forall (m :: * -> *) c a. Group m c a -> NonEmpty Text
names :: NonEmpty Text
names, Maybe (Group m c a)
$sel:parent:Group :: forall (m :: * -> *) c a. Group m c a -> Maybe (Group m c a)
parent :: Maybe (Group m c a)
parent, HashMap Text (Command m c a, AliasType)
$sel:commands:Group :: forall (m :: * -> *) c a.
Group m c a -> HashMap Text (Command m c a, AliasType)
commands :: HashMap Text (Command m c a, AliasType)
commands, HashMap Text (Group m c a, AliasType)
$sel:children:Group :: forall (m :: * -> *) c a.
Group m c a -> HashMap Text (Group m c a, AliasType)
children :: HashMap Text (Group m c a, AliasType)
children, Bool
$sel:hidden:Group :: forall (m :: * -> *) c a. Group m c a -> Bool
hidden :: Bool
hidden} =
    Int -> GroupS m c a -> Builder
forall a. TextShow a => Int -> a -> Builder
TextShow.showbPrec Int
d (GroupS m c a -> Builder) -> GroupS m c a -> Builder
forall a b. (a -> b) -> a -> b
$ NonEmpty Text
-> Maybe Text
-> [(Text, (Command m c a, AliasType))]
-> [(Text, (Group m c a, AliasType))]
-> Bool
-> GroupS m c a
forall (m :: * -> *) c a.
NonEmpty Text
-> Maybe Text
-> [(Text, (Command m c a, AliasType))]
-> [(Text, (Group m c a, AliasType))]
-> Bool
-> GroupS m c a
GroupS NonEmpty Text
names (NonEmpty Text -> Text
forall a. NonEmpty a -> a
NE.head (NonEmpty Text -> Text) -> Maybe (NonEmpty Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Group m c a)
parent Maybe (Group m c a)
-> Optic'
     An_AffineTraversal NoIx (Maybe (Group m c a)) (NonEmpty Text)
-> Maybe (NonEmpty Text)
forall k s (is :: IxList) a.
Is k An_AffineFold =>
s -> Optic' k is s a -> Maybe a
^? Prism
  (Maybe (Group m c a))
  (Maybe (Group m c a))
  (Group m c a)
  (Group m c a)
forall a b. Prism (Maybe a) (Maybe b) a b
_Just Prism
  (Maybe (Group m c a))
  (Maybe (Group m c a))
  (Group m c a)
  (Group m c a)
-> Optic
     A_Lens
     NoIx
     (Group m c a)
     (Group m c a)
     (NonEmpty Text)
     (NonEmpty Text)
-> Optic'
     An_AffineTraversal NoIx (Maybe (Group m c a)) (NonEmpty Text)
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic
  A_Lens
  NoIx
  (Group m c a)
  (Group m c a)
  (NonEmpty Text)
  (NonEmpty Text)
#names) (HashMap Text (Command m c a, AliasType)
-> [(Text, (Command m c a, AliasType))]
forall k v. HashMap k v -> [(k, v)]
LH.toList HashMap Text (Command m c a, AliasType)
commands) (HashMap Text (Group m c a, AliasType)
-> [(Text, (Group m c a, AliasType))]
forall k v. HashMap k v -> [(k, v)]
LH.toList HashMap Text (Group m c a, AliasType)
children) Bool
hidden