-- * Dates 


-- | Read the journal file again, before applying Changes (to not
-- overwrite possible changes, that were made in the mean time)
-- saveChanges :: String -- ^ journal path
--                -> (Journal-> (Journal, Integer))  
--                -> IO Journal
saveChanges
  :: (MonadReader (Options User config env) m, MonadIO m)
  => Maybe Journal
  -- this journal will be also changed and then returned
  ->  (Journal -> (Journal, Integer))
  -- ^ modifier, returning number of changed
  -> m Journal
saveChanges journal change = do
  journalPath <- absolute =<< readLedger imported
  liftIO $ do
    ej <- readJournalFile definputopts -- ignore balance assertions
          journalPath
    -- print $ length todos
    -- putStr $ unlines $ show <$> todos
    -- either error (print.length.jtxns) ej
    let (j, n) = either error change ej
    if n == 0 then putStrLn "\nNo transactions were changed!\n"
      else do let res = showTransactions j
              writeFile journalPath res
              putStrLn $ "\n"++ show n ++" Transactions were changed"
    return $ maybe j (\j -> 
      let (j2, m) = change j
      in if (n == m) then j2
         else error $ printf
              "Error 123, see source code. Solution: Use a proper database instead of a file. read: %d passed: %d" n m
            ) journal
saveChanges
  :: (MonadReader (Options User config env) m, MonadIO m)
  => Maybe Journal
  -- this journal will be also changed and then returned
  ->  (Journal -> (Journal, Integer))
  -- ^ modifier, returning number of changed
  -> m Journal
saveChanges journal change = do
  journalPath <- absolute =<< readLedger imported
  liftIO $ do
    ej <- readJournalFile definputopts -- ignore balance assertions
          journalPath
    -- print $ length todos
    -- putStr $ unlines $ show <$> todos
    -- either error (print.length.jtxns) ej
    let (j, n) = either error change ej
    if n == 0 then putStrLn "\nNo transactions were changed!\n"
      else do let res = showTransactions j
              writeFile journalPath res
              putStrLn $ "\n"++ show n ++" Transactions were changed"
    return $ maybe j (\j -> 
      let (j2, m) = change j
      in if (n == m) then j2
         else error $ printf
              "Error 123, see source code. Solution: Use a proper database instead of a file. read: %d passed: %d" n m
            ) journal
saveChanges journal change = do
  journalPath <- absolute =<< readLedger imported
  liftIO $ do
    ej <- readJournalFile definputopts -- ignore balance assertions
          journalPath
    -- print $ length todos
    -- putStr $ unlines $ show <$> todos
    -- either error (print.length.jtxns) ej
    let (j, n) = either error change ej
    if n == 0 then putStrLn "\nNo transactions were changed!\n"
      else do let res = showTransactions j
              writeFile journalPath res
              putStrLn $ "\n"++ show n ++" Transactions were changed"
    return $ maybe j (\j -> 
      let (j2, m) = change j
      in if (n == m) then j2
         else error $ printf
              "Error 123, see source code. Solution: Use a proper database instead of a file. read: %d passed: %d" n m
            ) journal