{-# LANGUAGE RankNTypes #-}
module Parameters where
import UI.Attributes
import Brick
import Brick.Widgets.Center
import Brick.Forms
import DeckHandling
import Data.Maybe
import Data.Char (isDigit)
import Data.Text (pack)
import Lens.Micro.Platform
import States
import Text.Read (readMaybe)
import UI.BrickHelpers
import qualified Data.Text as T
import qualified Graphics.Vty as V

mkParameterForm :: Int -> Parameters -> Form Parameters e Name
mkParameterForm :: Int -> Parameters -> Form Parameters e Name
mkParameterForm Int
n Parameters
ps =
  let label :: String -> Widget n -> Widget n
label String
s Widget n
w = Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padBottom (Int -> Padding
Pad Int
1) (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight (Int -> Padding
Pad Int
2) (String -> Widget n
forall n. String -> Widget n
strWrap String
s) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Widget n
w
      form :: Form Parameters e Name
form = [Parameters -> FormFieldState Parameters e Name]
-> Parameters -> Form Parameters e Name
forall s e n. [s -> FormFieldState s e n] -> s -> Form s e n
newForm
        [ Int
-> Lens' Parameters (Chunk, Int)
-> Parameters
-> FormFieldState Parameters e Name
forall s e.
Int -> Lens' s (Chunk, Int) -> s -> FormFieldState s e Name
chunkSubsetField Int
n (Int -> Lens' Parameters (Chunk, Int)
chunkSubsetLens Int
n)
        , String -> Widget Name -> Widget Name
forall n. String -> Widget n -> Widget n
label String
"Shuffle the deck?" (Widget Name -> Widget Name)
-> (Parameters -> FormFieldState Parameters e Name)
-> Parameters
-> FormFieldState Parameters e Name
forall n s e.
(Widget n -> Widget n)
-> (s -> FormFieldState s e n) -> s -> FormFieldState s e n
@@= Bool
-> Lens' Parameters Bool
-> Name
-> String
-> Parameters
-> FormFieldState Parameters e Name
forall n s e.
(Ord n, Show n) =>
Bool -> Lens' s Bool -> n -> String -> s -> FormFieldState s e n
yesnoField Bool
True Lens' Parameters Bool
pShuffle Name
ShuffleField String
""
        , String -> Widget Name -> Widget Name
forall n. String -> Widget n -> Widget n
label String
"Review mode?" (Widget Name -> Widget Name)
-> (Parameters -> FormFieldState Parameters e Name)
-> Parameters
-> FormFieldState Parameters e Name
forall n s e.
(Widget n -> Widget n)
-> (s -> FormFieldState s e n) -> s -> FormFieldState s e n
@@= Bool
-> Lens' Parameters Bool
-> Name
-> String
-> Parameters
-> FormFieldState Parameters e Name
forall n s e.
(Ord n, Show n) =>
Bool -> Lens' s Bool -> n -> String -> s -> FormFieldState s e n
yesnoField Bool
True Lens' Parameters Bool
pReviewMode Name
ReviewModeField String
""
        , Widget Name -> Widget Name
forall n. Widget n -> Widget n
hCenter (Widget Name -> Widget Name)
-> (Parameters -> FormFieldState Parameters e Name)
-> Parameters
-> FormFieldState Parameters e Name
forall n s e.
(Widget n -> Widget n)
-> (s -> FormFieldState s e n) -> s -> FormFieldState s e n
@@= Lens' Parameters Bool
-> Name -> String -> Parameters -> FormFieldState Parameters e Name
forall n s e.
(Ord n, Show n) =>
Lens' s Bool -> n -> String -> s -> FormFieldState s e n
okField Lens' Parameters Bool
pOk Name
ParametersOkField String
"Ok"
        ] Parameters
ps
  in Name -> Form Parameters e Name -> Form Parameters e Name
forall n s e. Eq n => n -> Form s e n -> Form s e n
setFormFocus Name
ParametersOkField Form Parameters e Name
forall e. Form Parameters e Name
form

chunkSubsetLens :: Int -> Lens' Parameters (Chunk, Int)
chunkSubsetLens :: Int -> Lens' Parameters (Chunk, Int)
chunkSubsetLens Int
n = (Parameters -> (Chunk, Int))
-> (Parameters -> (Chunk, Int) -> Parameters)
-> Lens' Parameters (Chunk, Int)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Parameters -> (Chunk, Int)
getter Parameters -> (Chunk, Int) -> Parameters
setter
  where getter :: Parameters -> (Chunk, Int)
getter Parameters
ps = (Parameters
psParameters -> Getting Chunk Parameters Chunk -> Chunk
forall s a. s -> Getting a s a -> a
^.Getting Chunk Parameters Chunk
Lens' Parameters Chunk
pChunk, Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
n (Parameters
psParameters
-> Getting (Maybe Int) Parameters (Maybe Int) -> Maybe Int
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Int) Parameters (Maybe Int)
Lens' Parameters (Maybe Int)
pSubset))
        setter :: Parameters -> (Chunk, Int) -> Parameters
setter Parameters
ps (Chunk
c, Int
int) = Parameters
ps Parameters -> (Parameters -> Parameters) -> Parameters
forall a b. a -> (a -> b) -> b
& (Chunk -> Identity Chunk) -> Parameters -> Identity Parameters
Lens' Parameters Chunk
pChunk((Chunk -> Identity Chunk) -> Parameters -> Identity Parameters)
-> Chunk -> Parameters -> Parameters
forall s t a b. ASetter s t a b -> b -> s -> t
.~Chunk
c Parameters -> (Parameters -> Parameters) -> Parameters
forall a b. a -> (a -> b) -> b
& (Maybe Int -> Identity (Maybe Int))
-> Parameters -> Identity Parameters
Lens' Parameters (Maybe Int)
pSubset ((Maybe Int -> Identity (Maybe Int))
 -> Parameters -> Identity Parameters)
-> Int -> Parameters -> Parameters
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
int

chunkSubsetField :: Int -> Lens' s (Chunk, Int) -> s -> FormFieldState s e Name
chunkSubsetField :: Int -> Lens' s (Chunk, Int) -> s -> FormFieldState s e Name
chunkSubsetField Int
capacity Lens' s (Chunk, Int)
stLens s
initialState = 
  let (Chunk
initChunk, Int
initInt) = s
initialState s -> Getting (Chunk, Int) s (Chunk, Int) -> (Chunk, Int)
forall s a. s -> Getting a s a -> a
^. Getting (Chunk, Int) s (Chunk, Int)
Lens' s (Chunk, Int)
stLens

      handleChunkEvent1 :: BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
      handleChunkEvent1 :: BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
handleChunkEvent1 (VtyEvent Event
ev) s :: (Chunk, Int)
s@(c :: Chunk
c@(Chunk Int
i Int
n), Int
int) = case Event
ev of
        V.EvKey (V.KChar Char
c) [] | Char -> Bool
isDigit Char
c -> 
          let i' :: Int
i' = String -> Int
forall a. Read a => String -> a
read (Int -> String
forall a. Show a => a -> String
show Int
i String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Char
c])
          in (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Chunk, Int) -> EventM n (Chunk, Int))
-> (Chunk, Int) -> EventM n (Chunk, Int)
forall a b. (a -> b) -> a -> b
$ if Int
i' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n Bool -> Bool -> Bool
|| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then (Int -> Int -> Chunk
Chunk Int
i' Int
n, Chunk -> Int
getSizeOfChunk (Int -> Int -> Chunk
Chunk Int
i' Int
n)) else (Int -> Int -> Chunk
Chunk Int
n Int
n, Chunk -> Int
getSizeOfChunk (Int -> Int -> Chunk
Chunk Int
n Int
n))
        V.EvKey Key
V.KBS [] ->
          let calcNew :: a -> p
calcNew a
x = if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (a -> String
forall a. Show a => a -> String
show a
x) then p
0 else p -> Maybe p -> p
forall a. a -> Maybe a -> a
fromMaybe p
0 (String -> Maybe p
forall a. Read a => String -> Maybe a
readMaybe (String -> String
forall a. [a] -> [a]
init (a -> String
forall a. Show a => a -> String
show a
x)))
          in (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> Chunk
Chunk (Int -> Int
forall a p. (Show a, Num p, Read p) => a -> p
calcNew Int
i) Int
n, Int
int)
        Event
_ -> (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk, Int)
s
      handleChunkEvent1 BrickEvent n e
_ (Chunk, Int)
s = (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk, Int)
s

      handleChunkEvent2 :: BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
      handleChunkEvent2 :: BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
handleChunkEvent2 (VtyEvent Event
ev) s :: (Chunk, Int)
s@(c :: Chunk
c@(Chunk Int
i Int
n), Int
int) = case Event
ev of
        V.EvKey (V.KChar Char
c) [] | Char -> Bool
isDigit Char
c -> 
          let n' :: Int
n' = String -> Int
forall a. Read a => String -> a
read (Int -> String
forall a. Show a => a -> String
show Int
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Char
c])
              i' :: Int
i' = if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n' Bool -> Bool -> Bool
|| Int
n' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then Int
i else Int
n'
          in (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Chunk, Int) -> EventM n (Chunk, Int))
-> (Chunk, Int) -> EventM n (Chunk, Int)
forall a b. (a -> b) -> a -> b
$ if Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
capacity then (Int -> Int -> Chunk
Chunk Int
i' Int
n', Chunk -> Int
getSizeOfChunk (Int -> Int -> Chunk
Chunk Int
i' Int
n')) else (Int -> Int -> Chunk
Chunk Int
i Int
capacity, Chunk -> Int
getSizeOfChunk (Int -> Int -> Chunk
Chunk Int
i Int
capacity))
        V.EvKey Key
V.KBS [] ->
          let calcNew :: a -> p
calcNew a
x = if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (a -> String
forall a. Show a => a -> String
show a
x) then p
0 else p -> Maybe p -> p
forall a. a -> Maybe a -> a
fromMaybe p
0 (String -> Maybe p
forall a. Read a => String -> Maybe a
readMaybe (String -> String
forall a. [a] -> [a]
init (a -> String
forall a. Show a => a -> String
show a
x)))
          in (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Chunk, Int) -> EventM n (Chunk, Int))
-> (Chunk, Int) -> EventM n (Chunk, Int)
forall a b. (a -> b) -> a -> b
$
              let newN :: Int
newN = Int -> Int
forall a p. (Show a, Num p, Read p) => a -> p
calcNew Int
n
                  newI :: Int
newI = if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
newN Bool -> Bool -> Bool
|| Int
newN Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then Int
i else Int
newN
              in (Int -> Int -> Chunk
Chunk Int
newI Int
newN, Int
int)
        Event
_ -> (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk, Int)
s
      handleChunkEvent2 BrickEvent n e
_ (Chunk, Int)
s = (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk, Int)
s

      handleSubsetEvent :: BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
      handleSubsetEvent :: BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
handleSubsetEvent (VtyEvent Event
ev) s :: (Chunk, Int)
s@(ch :: Chunk
ch@(Chunk Int
i Int
n), Int
int) = 
        let bound :: Int
bound = Chunk -> Int
getSizeOfChunk Chunk
ch in
          case Event
ev of
          V.EvKey (V.KChar Char
c) [] | Char -> Bool
isDigit Char
c -> 
            let newValue :: Int
newValue = String -> Int
forall a. Read a => String -> a
read (Int -> String
forall a. Show a => a -> String
show Int
int String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Char
c])
                int' :: Int
int' = if Int
newValue Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
bound then Int
newValue else Int
bound
            in (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk
ch, Int
int')
          V.EvKey Key
V.KBS [] -> 
            let int' :: Int
int' = case Int -> String
forall a. Show a => a -> String
show Int
int of
                            String
"" -> Int
0
                            String
xs -> Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 (String -> Maybe Int
forall a. Read a => String -> Maybe a
readMaybe (String -> String
forall a. [a] -> [a]
init String
xs))
            in (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk
ch, Int
int')
          Event
_ -> (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk, Int)
s
      handleSubsetEvent BrickEvent n e
_ (Chunk, Int)
s = (Chunk, Int) -> EventM n (Chunk, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk, Int)
s

      renderChunk1 :: Bool -> (Chunk, Int) -> Widget Name
      renderChunk1 :: Bool -> (Chunk, Int) -> Widget Name
renderChunk1 Bool
foc (Chunk Int
i Int
n, Int
_) = 
        let addAttr :: Widget n -> Widget n
addAttr = if Bool
foc then AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
focusedFormInputAttr else Widget n -> Widget n
forall a. a -> a
id
            csr :: t a -> Widget Name -> Widget Name
csr t a
x = if Bool
foc then Name -> Location -> Widget Name -> Widget Name
forall n. n -> Location -> Widget n -> Widget n
showCursor Name
ChunkField1 ((Int, Int) -> Location
Location (t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
x,Int
0)) else Widget Name -> Widget Name
forall a. a -> a
id
            val' :: a -> String
val' a
0 = String
""
            val' a
x = a -> String
forall a. Show a => a -> String
show a
x
          in Widget Name -> Widget Name
forall n. Widget n -> Widget n
addAttr (String -> Widget Name -> Widget Name
forall (t :: * -> *) a.
Foldable t =>
t a -> Widget Name -> Widget Name
csr (Int -> String
forall a. (Eq a, Num a, Show a) => a -> String
val' Int
i) (String -> Widget Name
forall n. String -> Widget n
str (Int -> String
forall a. (Eq a, Num a, Show a) => a -> String
val' Int
i))) Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> String -> Widget Name
forall n. String -> Widget n
str String
"/"

      renderChunk2 :: Bool -> (Chunk, Int) -> Widget Name
      renderChunk2 :: Bool -> (Chunk, Int) -> Widget Name
renderChunk2 Bool
foc (Chunk Int
i Int
n, Int
_) = 
        let addAttr :: Widget n -> Widget n
addAttr = if Bool
foc then AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
focusedFormInputAttr else Widget n -> Widget n
forall a. a -> a
id
            csr :: t a -> Widget Name -> Widget Name
csr t a
x = if Bool
foc then Name -> Location -> Widget Name -> Widget Name
forall n. n -> Location -> Widget n -> Widget n
showCursor Name
ChunkField2 ((Int, Int) -> Location
Location (t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
x,Int
0)) else Widget Name -> Widget Name
forall a. a -> a
id
            val' :: a -> String
val' a
0 = String
""
            val' a
x = a -> String
forall a. Show a => a -> String
show a
x
          in Widget Name -> Widget Name
forall n. Widget n -> Widget n
addAttr (String -> Widget Name -> Widget Name
forall (t :: * -> *) a.
Foldable t =>
t a -> Widget Name -> Widget Name
csr (Int -> String
forall a. (Eq a, Num a, Show a) => a -> String
val' Int
n) (String -> Widget Name
forall n. String -> Widget n
str (Int -> String
forall a. (Eq a, Num a, Show a) => a -> String
val' Int
n)))

      customConcat :: [Widget Name] -> Widget Name
      customConcat :: [Widget Name] -> Widget Name
customConcat [Widget Name
chunk1, Widget Name
chunk2, Widget Name
subset] = 
        (String -> Widget Name
forall n. String -> Widget n
str String
"Select chunk:" Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> Char -> Widget Name
forall n. Char -> Widget n
hFill Char
' ' Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> Widget Name
chunk1 Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> Widget Name
chunk2) 
        Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<=>
        String -> Widget Name
forall n. String -> Widget n
str String
" "
        Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<=>
        (String -> Widget Name
forall n. String -> Widget n
str String
"Number of cards:" Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> Char -> Widget Name
forall n. Char -> Widget n
hFill Char
' ' Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> Widget Name
subset)
        Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<=>
        String -> Widget Name
forall n. String -> Widget n
str String
" "
      customConcat [Widget Name]
_ = String -> Widget Name
forall a. HasCallStack => String -> a
error String
"chunkSubsetField form field concatenation has gone wrong"

      getSizeOfChunk :: Chunk -> Int
      getSizeOfChunk :: Chunk -> Int
getSizeOfChunk (Chunk Int
i Int
n) = 
        if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1 Bool -> Bool -> Bool
&& Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1 Bool -> Bool -> Bool
&& Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n
          then [Int] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Int -> [Int] -> [[Int]]
forall a. Int -> [a] -> [[a]]
splitIntoNChunks Int
n [Int
1..Int
capacity] [[Int]] -> Int -> [Int]
forall a. [a] -> Int -> a
!! (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
          else Int
capacity

      renderSubset :: Bool -> (Chunk, Int) -> Widget Name
      renderSubset :: Bool -> (Chunk, Int) -> Widget Name
renderSubset Bool
foc (Chunk
c, Int
value) = 
        let cardsInChunk :: Int
cardsInChunk = Chunk -> Int
getSizeOfChunk Chunk
c
        in Int -> String -> Name -> Bool -> Int -> Widget Name
forall n. Int -> String -> n -> Bool -> Int -> Widget n
renderNaturalNumber Int
cardsInChunk (String
"/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
cardsInChunk) Name
SubsetField Bool
foc Int
value
      
      validateChunk :: (Chunk, b) -> Maybe (Chunk, b)
validateChunk (c :: Chunk
c@(Chunk Int
i Int
n), b
int) = if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1 Bool -> Bool -> Bool
&& Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1 Bool -> Bool -> Bool
&& Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n then (Chunk, b) -> Maybe (Chunk, b)
forall a. a -> Maybe a
Just (Chunk
c, b
int) else Maybe (Chunk, b)
forall a. Maybe a
Nothing
      validateSubset :: a -> Maybe a
validateSubset = a -> Maybe a
forall a. a -> Maybe a
Just

  in FormFieldState :: forall b s a e n.
b
-> Lens' s a
-> (a -> b -> b)
-> [FormField a b e n]
-> (Widget n -> Widget n)
-> ([Widget n] -> Widget n)
-> FormFieldState s e n
FormFieldState { formFieldState :: (Chunk, Int)
formFieldState = (Chunk
initChunk, Int
initInt)
                    , formFields :: [FormField (Chunk, Int) (Chunk, Int) e Name]
formFields = [ 
                                     Name
-> ((Chunk, Int) -> Maybe (Chunk, Int))
-> Bool
-> (Bool -> (Chunk, Int) -> Widget Name)
-> (BrickEvent Name e -> (Chunk, Int) -> EventM Name (Chunk, Int))
-> FormField (Chunk, Int) (Chunk, Int) e Name
forall a b e n.
n
-> (b -> Maybe a)
-> Bool
-> (Bool -> b -> Widget n)
-> (BrickEvent n e -> b -> EventM n b)
-> FormField a b e n
FormField Name
ChunkField1 (Chunk, Int) -> Maybe (Chunk, Int)
forall b. (Chunk, b) -> Maybe (Chunk, b)
validateChunk Bool
True
                                       Bool -> (Chunk, Int) -> Widget Name
renderChunk1
                                       BrickEvent Name e -> (Chunk, Int) -> EventM Name (Chunk, Int)
forall n e. BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
handleChunkEvent1,
                                     Name
-> ((Chunk, Int) -> Maybe (Chunk, Int))
-> Bool
-> (Bool -> (Chunk, Int) -> Widget Name)
-> (BrickEvent Name e -> (Chunk, Int) -> EventM Name (Chunk, Int))
-> FormField (Chunk, Int) (Chunk, Int) e Name
forall a b e n.
n
-> (b -> Maybe a)
-> Bool
-> (Bool -> b -> Widget n)
-> (BrickEvent n e -> b -> EventM n b)
-> FormField a b e n
FormField Name
ChunkField2 (Chunk, Int) -> Maybe (Chunk, Int)
forall b. (Chunk, b) -> Maybe (Chunk, b)
validateChunk Bool
True
                                       Bool -> (Chunk, Int) -> Widget Name
renderChunk2
                                       BrickEvent Name e -> (Chunk, Int) -> EventM Name (Chunk, Int)
forall n e. BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
handleChunkEvent2,
                                     Name
-> ((Chunk, Int) -> Maybe (Chunk, Int))
-> Bool
-> (Bool -> (Chunk, Int) -> Widget Name)
-> (BrickEvent Name e -> (Chunk, Int) -> EventM Name (Chunk, Int))
-> FormField (Chunk, Int) (Chunk, Int) e Name
forall a b e n.
n
-> (b -> Maybe a)
-> Bool
-> (Bool -> b -> Widget n)
-> (BrickEvent n e -> b -> EventM n b)
-> FormField a b e n
FormField Name
SubsetField (Chunk, Int) -> Maybe (Chunk, Int)
forall a. a -> Maybe a
validateSubset Bool
True
                                       Bool -> (Chunk, Int) -> Widget Name
renderSubset
                                       BrickEvent Name e -> (Chunk, Int) -> EventM Name (Chunk, Int)
forall n e. BrickEvent n e -> (Chunk, Int) -> EventM n (Chunk, Int)
handleSubsetEvent
                                   ]
                    , formFieldLens :: Lens' s (Chunk, Int)
formFieldLens = Lens' s (Chunk, Int)
stLens
                    , formFieldRenderHelper :: Widget Name -> Widget Name
formFieldRenderHelper = Widget Name -> Widget Name
forall a. a -> a
id
                    , formFieldConcat :: [Widget Name] -> Widget Name
formFieldConcat = [Widget Name] -> Widget Name
customConcat }

okField :: (Ord n, Show n) => Lens' s Bool -> n -> String -> s -> FormFieldState s e n
okField :: Lens' s Bool -> n -> String -> s -> FormFieldState s e n
okField Lens' s Bool
stLens n
name String
label s
initialState =
  let initVal :: Bool
initVal = s
initialState s -> Getting Bool s Bool -> Bool
forall s a. s -> Getting a s a -> a
^. Getting Bool s Bool
Lens' s Bool
stLens

      handleEvent :: BrickEvent n e -> Bool -> m Bool
handleEvent (VtyEvent (V.EvKey Key
V.KEnter [])) Bool
_  = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
      handleEvent BrickEvent n e
_ Bool
s = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
s
  
  in FormFieldState :: forall b s a e n.
b
-> Lens' s a
-> (a -> b -> b)
-> [FormField a b e n]
-> (Widget n -> Widget n)
-> ([Widget n] -> Widget n)
-> FormFieldState s e n
FormFieldState { formFieldState :: Bool
formFieldState = Bool
initVal
                    , formFields :: [FormField Bool Bool e n]
formFields = [ n
-> (Bool -> Maybe Bool)
-> Bool
-> (Bool -> Bool -> Widget n)
-> (BrickEvent n e -> Bool -> EventM n Bool)
-> FormField Bool Bool e n
forall a b e n.
n
-> (b -> Maybe a)
-> Bool
-> (Bool -> b -> Widget n)
-> (BrickEvent n e -> b -> EventM n b)
-> FormField a b e n
FormField n
name Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True 
                                       (String -> n -> Bool -> Bool -> Widget n
forall n. String -> n -> Bool -> Bool -> Widget n
renderOk String
label n
name)
                                       BrickEvent n e -> Bool -> EventM n Bool
forall (m :: * -> *) n e.
Monad m =>
BrickEvent n e -> Bool -> m Bool
handleEvent ]
                    , formFieldLens :: Lens' s Bool
formFieldLens = Lens' s Bool
stLens
                    , formFieldRenderHelper :: Widget n -> Widget n
formFieldRenderHelper = Widget n -> Widget n
forall a. a -> a
id
                    , formFieldConcat :: [Widget n] -> Widget n
formFieldConcat = [Widget n] -> Widget n
forall n. [Widget n] -> Widget n
vBox }

renderOk :: String -> n -> Bool -> Bool -> Widget n
renderOk :: String -> n -> Bool -> Bool -> Widget n
renderOk String
label n
_ Bool
focus Bool
_ =
  (if Bool
focus then AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
selectedAttr else Widget n -> Widget n
forall a. a -> a
id) (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ String -> Widget n
forall n. String -> Widget n
str String
"Ok"