module Vimeta.UI.Term.Movie
( movieSearch,
)
where
import Byline.Menu
import Network.API.TheMovieDB
import Vimeta.Core
import Vimeta.UI.Common.Util
import Vimeta.UI.Term.Common
newtype MovieItem = MovieItem Movie
instance ToStylizedText MovieItem where
toStylizedText (MovieItem m) =
mconcat
[ text (movieTitle m),
text (parens $ dayAsYear $ movieReleaseDate m)
]
movieSearch :: MonadIO m => Text -> Vimeta m Movie
movieSearch initial = do
name <- askUntil searchPrompt (Just initial) (return . notBlank searchErr)
movies <-
tmdb (searchMovies name)
>>= ( nonEmpty >>> \case
Nothing -> throwError ("no matches for: " <> toString name)
Just ms -> pure (MovieItem <$> ms)
)
MovieItem movie <- askWithMenuRepeatedly (mkMenu movies) prompt eprompt
logID movie >> tmdb (fetchMovie (movieID movie))
where
mkMenu movies = menuBanner (text "Choose a movie:") (menu movies)
searchPrompt = text "search (movie name): "
searchErr = "please enter a valid search term" <> fg red
prompt = text "Which is the correct movie? "
eprompt = "please choose a valid movie" <> fg red
logID movie =
verbose $
"using movie ID: "
<> show (movieID movie)