-- |
--
-- Copyright:
--   This file is part of the package vimeta. It is subject to the
--   license terms in the LICENSE file found in the top-level
--   directory of this distribution and at:
--
--     https://github.com/pjones/vimeta
--
--   No part of this package, including this file, may be copied,
--   modified, propagated, or distributed except according to the terms
--   contained in the LICENSE file.
--
-- License: BSD-2-Clause
module Vimeta.UI.CommandLine.Movie
  ( Options,
    optionsParser,
    run,
  )
where

import Network.API.TheMovieDB
import Options.Applicative
import System.FilePath
import Vimeta.Core
import Vimeta.UI.CommandLine.Common
import Vimeta.UI.Common.Movie
import Vimeta.UI.Term.Movie

data Options = Options
  { Options -> Maybe ItemID
optsMovieID :: Maybe ItemID,
    Options -> FilePath
optsFile :: FilePath,
    Options -> CommonOptions
optsCommon :: CommonOptions
  }

optionsParser :: Parser Options
optionsParser :: Parser Options
optionsParser =
  Maybe ItemID -> FilePath -> CommonOptions -> Options
Options (Maybe ItemID -> FilePath -> CommonOptions -> Options)
-> Parser (Maybe ItemID)
-> Parser (FilePath -> CommonOptions -> Options)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ItemID -> Parser (Maybe ItemID)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM ItemID -> Mod OptionFields ItemID -> Parser ItemID
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM ItemID
forall a. Read a => ReadM a
auto Mod OptionFields ItemID
forall a. Mod OptionFields a
getMovieID)
    Parser (FilePath -> CommonOptions -> Options)
-> Parser FilePath -> Parser (CommonOptions -> Options)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE")
    Parser (CommonOptions -> Options)
-> Parser CommonOptions -> Parser Options
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser CommonOptions
commonOptions
  where
    -- Parser options for @optsMovieID@.
    getMovieID :: Mod OptionFields a
getMovieID =
      [Mod OptionFields a] -> Mod OptionFields a
forall a. Monoid a => [a] -> a
mconcat
        [ Char -> Mod OptionFields a
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'i',
          FilePath -> Mod OptionFields a
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"id",
          FilePath -> Mod OptionFields a
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"ID",
          FilePath -> Mod OptionFields a
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"Movie ID assigned by TheMovieDB.org"
        ]

run :: Options -> IO (Either String ())
run :: Options -> IO (Either FilePath ())
run Options
opts = (Config -> Config) -> Vimeta IO () -> IO (Either FilePath ())
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
(Config -> Config) -> Vimeta m a -> m (Either FilePath a)
execVimeta (CommonOptions -> Config -> Config
updateConfig (CommonOptions -> Config -> Config)
-> CommonOptions -> Config -> Config
forall a b. (a -> b) -> a -> b
$ Options -> CommonOptions
optsCommon Options
opts) (Vimeta IO () -> IO (Either FilePath ()))
-> Vimeta IO () -> IO (Either FilePath ())
forall a b. (a -> b) -> a -> b
$
  case Options -> Maybe ItemID
optsMovieID Options
opts of
    Just ItemID
mid -> do
      Movie
movie <- TheMovieDB Movie -> Vimeta IO Movie
forall (m :: * -> *) a. MonadIO m => TheMovieDB a -> Vimeta m a
tmdb (ItemID -> TheMovieDB Movie
fetchMovie ItemID
mid)
      FilePath -> Movie -> Vimeta IO ()
forall (m :: * -> *). MonadIO m => FilePath -> Movie -> Vimeta m ()
tagMovie (Options -> FilePath
optsFile Options
opts) Movie
movie
    Maybe ItemID
Nothing -> do
      Movie
movie <- Text -> Vimeta IO Movie
forall (m :: * -> *). MonadIO m => Text -> Vimeta m Movie
movieSearch Text
initialTitle
      FilePath -> Movie -> Vimeta IO ()
forall (m :: * -> *). MonadIO m => FilePath -> Movie -> Vimeta m ()
tagMovie (Options -> FilePath
optsFile Options
opts) Movie
movie
  where
    -- Calculate an initial search title from the file name.
    initialTitle :: Text
    initialTitle :: Text
initialTitle = FilePath -> Text
forall a. ToText a => a -> Text
toText (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
dropExtension (FilePath -> FilePath
takeFileName (FilePath -> FilePath) -> FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ Options -> FilePath
optsFile Options
opts)