{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE RecordWildCards #-}

module SnelstartImport
  ( main,
  )
where

import qualified Data.ByteString.Lazy as BS
import SnelstartImport.ING
import SnelstartImport.N26
import Data.Vector(toList)
import SnelstartImport.Options
import Paths_snelstart_import (version)
import           Options.Applicative
import Text.Printf
import Data.Version (showVersion)
import SnelstartImport.Web
import SnelstartImport.Convert

currentVersion :: String
currentVersion :: String
currentVersion = Version -> String
showVersion Version
version

readSettings :: IO (ProgramOptions)
readSettings :: IO ProgramOptions
readSettings = ParserPrefs -> ParserInfo ProgramOptions -> IO ProgramOptions
forall a. ParserPrefs -> ParserInfo a -> IO a
customExecParser (PrefsMod -> ParserPrefs
prefs PrefsMod
showHelpOnError) (ParserInfo ProgramOptions -> IO ProgramOptions)
-> ParserInfo ProgramOptions -> IO ProgramOptions
forall a b. (a -> b) -> a -> b
$ Parser ProgramOptions
-> InfoMod ProgramOptions -> ParserInfo ProgramOptions
forall a. Parser a -> InfoMod a -> ParserInfo a
info
  (Parser (ProgramOptions -> ProgramOptions)
forall a. Parser (a -> a)
helper Parser (ProgramOptions -> ProgramOptions)
-> Parser ProgramOptions -> Parser ProgramOptions
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ProgramOptions
parseProgram)
  (InfoMod ProgramOptions
forall a. InfoMod a
fullDesc InfoMod ProgramOptions
-> InfoMod ProgramOptions -> InfoMod ProgramOptions
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod ProgramOptions
forall a. String -> InfoMod a
header (String -> String -> String
forall r. PrintfType r => String -> r
printf String
"Snelstart importer %s" String
currentVersion) InfoMod ProgramOptions
-> InfoMod ProgramOptions -> InfoMod ProgramOptions
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod ProgramOptions
forall a. String -> InfoMod a
progDesc
    String
"Converts various banks and programs to something snelstart understands"
  )


main :: IO ()
main :: IO ()
main = do
  String -> IO ()
putStrLn String
""
  String -> IO ()
putStrLn String
"starting snelstart import"
  String -> IO ()
putStrLn String
""

  ProgramOptions
settings <- IO ProgramOptions
readSettings
  case ProgramOptions
settings of
    Convert CliOptions
cli -> CliOptions -> IO ()
convertCli CliOptions
cli
    Webserver WebOptions
options -> WebOptions -> IO ()
webMain WebOptions
options

convertCli :: CliOptions -> IO ()
convertCli :: CliOptions -> IO ()
convertCli CliOptions
options = do
  Either String (Vector N26)
result <- String -> IO (Either String (Vector N26))
readN26 (CliOptions -> String
cliInputFile CliOptions
options)
  case Either String (Vector N26)
result of
    Left String
x -> String -> IO ()
forall a. HasCallStack => String -> a
error String
x
    Right Vector N26
n26Vec -> String -> ByteString -> IO ()
BS.writeFile (CliOptions -> String
cliOutputFile CliOptions
options) (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$ let
        n26 :: [ING]
        n26 :: [ING]
n26 = Text -> N26 -> ING
n26ToING (CliOptions -> Text
cliOwnAccount CliOptions
options) (N26 -> ING) -> [N26] -> [ING]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Vector N26 -> [N26]
forall a. Vector a -> [a]
toList Vector N26
n26Vec
      in
        [ING] -> ByteString
writeCsv [ING]
n26