{- | mtlstats Copyright (C) Rhéal Lamothe This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . -} module Mtlstats.Control.CreateGoalie (createGoalieC) where import Brick.Widgets.Core (str) import Control.Monad.State.Class (gets, modify) import Lens.Micro ((^.), (.~), (?~), (%~), to) import Lens.Micro.Mtl ((.=)) import Mtlstats.Actions import Mtlstats.Format import Mtlstats.Handlers import Mtlstats.Prompt import Mtlstats.Types import Mtlstats.Util -- | Handles goalie creation createGoalieC :: CreateGoalieState -> Controller createGoalieC cgs | null $ cgs^.cgsNumber = getGoalieNumC | null $ cgs^.cgsName = getGoalieNameC | null $ cgs^.cgsRookieFlag = getRookieFlagC | null $ cgs^.cgsActiveFlag = getActiveFlagC | otherwise = confirmCreateGoalieC getGoalieNumC :: Controller getGoalieNumC = promptController goalieNumPrompt getGoalieNameC :: Controller getGoalieNameC = promptController goalieNamePrompt getRookieFlagC :: Controller getRookieFlagC = Controller { drawController = const $ str "Is this goalie a rookie? (Y/N)" , handleController = \e -> modify $ case ynHandler e of Just True -> progMode.createGoalieStateL %~ (cgsRookieFlag ?~ True) . (cgsActiveFlag ?~ True) rf -> progMode.createGoalieStateL.cgsRookieFlag .~ rf } getActiveFlagC :: Controller getActiveFlagC = Controller { drawController = const $ str "Is this goalie active? (Y/N)" , handleController = \e -> progMode.createGoalieStateL.cgsActiveFlag .= ynHandler e } confirmCreateGoalieC :: Controller confirmCreateGoalieC = Controller { drawController = \s -> let cgs = s^.progMode.createGoalieStateL in linesToWidget $ labelTable [ ( "Goalie number", maybe "?" show $ cgs^.cgsNumber ) , ( "Goalie name", cgs^.cgsName ) , ( "Rookie", maybe "?" show $ cgs^.cgsRookieFlag ) , ( "Active", maybe "?" show $ cgs^.cgsActiveFlag ) ] ++ [ "" , "Create goalie: are you sure? (Y/N)" ] , handleController = \e -> do cgs <- gets (^.progMode.createGoalieStateL) let success = cgs^.cgsSuccessCallback failure = cgs^.cgsFailureCallback case ynHandler e of Just True -> do gid <- gets (^.database.dbGoalies.to length) let rookie = cgs^.cgsRookieFlag == Just True modify addGoalie if rookie then success else modify $ progMode.editGoalieStateL %~ (egsSelectedGoalie ?~ gid) . (egsMode .~ EGLtGames True) . (egsCallback .~ success) Just False -> failure Nothing -> return () }